Jtemplate usage notes

Source: Internet
Author: User

Jtemplate is used in recent projects.

 

<Script type = "text/javascript" src = "Scripts/jquery-jtemplates.js"> </script>

 

<! -- Result container -->
<Div id = "result_container"> </div>

 

<! -- Template content -->
<Textarea id = "template-items" is the jtemplate format. The template content is placed in textarea, and keywords and data are wrapped in braces, $ T indicates the data, and the keyword # indicates the start.

<Script type = "text/javascript">
Var data = [{name: 'Anne ', birthday: '2017-01-01', mail: 'Anne @ domain.com '},
{Name: 'amelie', birthday: '2017-02-02 ', mail: 'amelie @ domain.com '},
{Name: 'polil', birthday: '2017-03-03 ', mail: 'Polly @ domain.com '},
{Name: 'Alice ', birthday: '2017-04-04', mail: 'Alice @ domain.com '},
{Name: 'martha ', birthday: '2017-05-05', mail: 'martha @ domain.com '}]
// Set the Template
$ ("# Result_container"). setTemplateElement ("template-items ");

// Fill in and display data
$ ("# Result_container"). processTemplate (data );

// You can also write $ ("# result_container"). setTemplateElement ("template-items"). processTemplate (data) here );
</Script>

After this is done, a data list is displayed:

 

5. Call javascript in the template

In {}, you can write javascript scripts at will. For example, if you want to display your birthday in another format

Of course, you can also package it into a method for calling, such as defining the js method first:

Function formatDate (date ){
Return date. replace (/-/g ,'/');
}

Change the template to {formatDate ($ T. item. birthday)} to achieve the same effect.

 

6. How to include the textarea input box in the template

The jtemplate content is stored in textarea, but sometimes we need to include textarea in the template. There are two ways to achieve this:

1) comment out the content in the template. For example, change the template:

<! -- Template content -->
<Textarea id = "template-items" <input id = "d11" type = "text"

Its parameter is a json object with braces. If you put it directly in the template like this, you will not get the desired effect, but there are also flexible methods, such as writing The onclick event outside the template.

 

7. The preceding example shows how to bind a simple data list using jtemplate. However, jtemplate can bind any json object and has many usage methods. You can refer to http://jtemplates.tpython.com/to learn more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.