Use of the "JS" Jsrender template plugin

Source: Internet
Author: User

New company project, colleagues recommended my Jsrender template plug-in, with the feeling is very useful, at least more convenient than stitching strings, and support binding and function judgment, as well as some other advanced usage, I used to have, data binding, if else judgment, and method binding, and so on. Don't say much nonsense and start introducing. (Not Jsrender study, only individual project summary)


Look at the code first:

<script type= "Text/x-jsrender"  id= "Lettersenduserinfotemplate" >        <div class=" Info_warp ">         <div class= "Info_inner" >             <span class= "username" >{{:name}}</span>             <span class= "UserInfo" >{{:title1string}}</span >            <span class= "UserInfo" > {{:title2string}}</span>        </div>         <p class= "Micro_text" >             {{:micromotto}}        </p>    </ Div></script>

Yes, the above is the template, {{}} is the model data to be bound. Home, page reference jsrender.js not much to say, if binding the above template, the method is as follows:

var tabhtml = $ ("#letterSendUserInfoTemplate"). Render (data);  Letter.options.leftlettertabcontainer.html (tabhtml); var data=[{headphotos: ', Name: ', title1string: ', title2string: ', Micromotto: '}, {headphotos: ', Name: ', Title1string: ', title2string: ', Micromotto: '}];

In fact, it is very simple, if it is an AJAX request, as long as the returned data items are bound to the corresponding HTML, and then use the template render method to get the corresponding HTML, assigned to the corresponding Div or other elements, OK, save the trouble of spelling HTML and difficult to maintain.


Use function one: For loop

{{for #data}}html code {{/for}}

Use function two: if else to determine the value of a different class for the element according to different conditions

Class= "{{if placement==0}}other {{else}}self{{/if}}} {{if isread}}{{else}}notread{{/if}} clearfix"

Use function Three: Use JS method, convert time to fixed format, notice Shorttimeformatter method must be put in $.views.helpers inside

$.views.helpers ({    timeformatter: function  (time)  {         var t = time.split (' t ');         var result = t[0].substr (0, 10)  +  '   '  + t[1].substr (0 ,  5);        return result;    },     ShortTimeFormatter: function  (Time)  {         var t = time.split (' t ');        var  Result = t[0].substr (2, 8)  +  '   '  + t[1].substr (0, 5);         return result;    },     lasttimeformatter: function  (Time)  {        return  $.getdatediff (time);     }}); 
<div class= "Release_time" > {{>~shorttimeformatter (addtime)}} </div>

Well, there are a few simple ways to use the project, but JsRender should be more powerful, but I'm not using it, I'm interested in surfing the Internet, yes, JsRender

This article is from the "PZ Technical training base" blog, please be sure to keep this source http://panzi.blog.51cto.com/4738203/1603983

Use of the "JS" Jsrender template plugin

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.