Home > Web Front-end > JS Tutorial > body text

JavaScript lightweight template engine juicer usage guide_javascript skills

WBOY
Release: 2016-05-16 16:43:34
Original
2280 people have browsed it

How to use

Compile the template and render the result immediately based on the data

juicer(tpl, data);
Copy after login

Only compile the template without rendering it, and return a reusable compiled function

 var compiled_tpl = juicer(tpl);
Copy after login

Render the previously compiled template based on the given data

 var complied_tpl = juicer(tpl);
 var html = complied_tpl.render(data);
Copy after login

Register/Unregister custom function (object)

juicer.register(‘function_name', function);
juicer.unregister(‘function_name');
Copy after login

Default parameter configuration

 {
   cache: true [false];
   script: true [false];
   error handling: true [false];
   detection: true [false];
 }
Copy after login

Modify the default configuration and modify it item by item

 juicer.set('cache', false);
Copy after login

Modify default configuration and batch modification

 juicer.set({
      'script': false,
      'cache': false
 })
Copy after login

Juicer will cache the compiled template by default, thereby avoiding the time spent on repeated compilation when the same template is rendered multiple times. If there is no special need, it is strongly not recommended to turn off the cache in the default parameters. Doing so will cause Juicer cache invalidation reduces performance.

Grammar

* ${Variable} 

- Use ${} to output variables, where _ is a reference to the data source (${_}). Supports the use of custom functions.

${name}
${name|function}
${name|function, arg1, arg2} 
Copy after login
 var = links: [{href: 'http://juicer.name', alt: 'Juicer'},
            {href: 'http://benben.cc', alt: 'Benben'},
            {href: 'http://ued.taobao.com', alt: 'Taobao UED'}  
           ]};
 var tpl = [ '{@each links as item}',
         '${item|links_build} <br />',  
         '{@/each}'].join('');
 var links = function(data) {    
   return '<a href="' + data.href + '" alt="' + data.alt + '" />';
};
juicer.register('links_build', links); //注册自定义函数
juicer(tpl, json);
Copy after login

* Escape/Avoid escaping

- ${Variable} will escape its content before outputting it. If you don’t want the output result to be escaped, you can use $${Variable} to avoid this situation.

 var json = {
    value: '<strong>juicer</strong>'
 };
 var escape_tpl='${value}';
 var unescape_tpl='$${value}';
 juicer(escape_tpl, json); //输出 '<strong>juicer</strong>'
 juicer(unescape_tpl, json); //输出 '<strong>juicer</strong>' 
Copy after login

* Loop through {@each} ... {@/each}   

- Traverse the array, ${index}current index

 {@each list as item, index}
     ${item.prop}
     ${index} //当前索引
 {@/each}
Copy after login

*Judge{@if} ... {@else if} ... {@else} ... {@ /if}

*Comments {# Comment content}

{# Here is the comment content}
*Auxiliary loop {@each i in range(m, n)}

 {@each i in range(5, 10)}
     ${i}; //输出 5;6;7;8;9;
 {@/each}
Copy after login

*Sub-template nesting {@include tpl, data}

- In addition to introducing subtemplates specified in the data, subtemplate nesting can also use the template code written in the `script` tag by specifying the string `#id`.

- HTML code:

<script type="text/juicer" id="subTpl">
   I'm sub content, ${name}
</script>
Copy after login

- Javascript code:

var tpl = 'Hi, {@include "#subTpl", subData}, End.';

juicer(tpl, {
subData: {
name: 'juicer'
}
});

//输出 Hi, I'm sub content, juicer, End.
 //或者通过数据引入子模板,下述代码也将会有相同的渲染结果:
 
 var tpl = 'Hi, {@include subTpl, subData}, End.';
 
 juicer(tpl, {
     subTpl: "I'm sub content, ${name}",
     subData: {
        name: 'juicer'
     }
 });
Copy after login

A complete example

HTML code:

 <script id="tpl" type="text/template">
   <ul>
     {@each list as it,index}
       <li>${it.name} (index: ${index})</li>
     {@/each}
     {@each blah as it}
       <li>
         num: ${it.num} <br />
         {@if it.num==3}
           {@each it.inner as it2}
             ${it2.time} <br />
           {@/each}
         {@/if}
       </li>
     {@/each}
   </ul>
 </script>
Copy after login

Javascript code:

 var data = {
   list: [
     {name:' guokai', show: true},
     {name:' benben', show: false},
     {name:' dierbaby', show: true}
   ],
   blah: [
     {num: 1},
     {num: 2},
     {num: 3, inner:[
       {'time': '15:00'},
       {'time': '16:00'},
       {'time': '17:00'},
       {'time': '18:00'}
     ]},
     {num: 4}
   ]
 };
 
 var tpl = document.getElementById('tpl').innerHTML;
 var html = juicer(tpl, data);
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template