Template7 使用


In the downloaded/installed package we need JavaScript files (.js) from the dist/ folder.

And include required script in our HTML page:

  1. <html>
  2.     <head>
  3.         ...
  4.         <script src="path/to/template7.min.js"></script>
  5.     </head>
  6.     <body>
  7.         ...
  8.     </body>
  9. </html>

Templates

Template7 templates looks like Handlebars templates, it is like regular HTML but with embedded handlebars expressions:

  1. <div class="list-block">
  2.   <ul>
  3.     {{#each items}}
  4.     <li class="item-content">
  5.       <div class="item-inner">
  6.         <div class="item-title">{{title}}</div>
  7.       </div>
  8.     </li>
  9.     {{/each}}
  10.   </ul>
  11. </div>

Expressions syntax

Template7 support expressions with the following syntax:

Variables:

  • {{title}} - plain variable. Outputs "title" variable in current context

  • {{../title}} - plain variable. Outputs "title" variable in parent context

  • {{../../title}} - plain variable. Outputs "title" variable in parent context of parent context

  • {{this}} - plain variable. Outputs variable equals to current context

  • {{person.name}} - plain variable. Outputs variable equals to "name" property of "person" variable in current context

  • {{../person.name}} - plain variable. The same but for parent context

  • {{@index}} - access to additional data variable. Such data variables could be used in helpers

Block expressions

  • {{#each}} - begin of block expression

  • {{else}} - begin of block inverse expression (where supported)

  • {{/each}} - end of block expression

  • {{#each reverse="true"}} - begin of block expression with passed reverse:true hash arguments

Helpers

Helpers could be plain expressions and block expressions:

  • {{join myArray delimiter=", "}} - execute "join" helper and pass there "myArray" variable of current context and delimiter:', ' hash argument

Compilation and Rendering

Template7 is a globally available Window function.

First of all we need to deliver string template. For example, we can store in script tag:

  1. <script id="template" type="text/template7">
  2.     <p>Hello, my name is {{firstName}} {{lastName}}</p>
  3. </script>

Now we need to compile it in JavaScript. Template7 will convert our template string to plain JavaScript function:

  1. var template = $$('#template').html();
  2.  
  3. // compile it with Template7
  4. var compiledTemplate = Template7.compile(template);
  5.  
  6. // Now we may render our compiled template by passing required context
  7. var context = {
  8.     firstName: 'John',
  9.     lastName: 'Doe'
  10. };
  11. var html = compiledTemplate(context);

Now, html variable will contain:

  1. <p>Hello, my name is John Doe</p>

实例预览

以上这个是单独使用Template7.js的实例。

下面这个是在Framework7中使用Template7实例

  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8">
  5.         <title>Template7 实例教程
  6.         </title>
  7.     </head>
  8.     <body>
  9.         <div class="views">
  10.             <div class="view view-main">
  11.                 <div class="pages">
  12.                     <script id="homeTemplate" type="text/template7">
  13.                         <div data-page="home" class="page">
  14.                             <div class="page-content">
  15.                                 <div class="content-block">
  16.                                     <p>Hello, my name is {{firstName}} {{lastName}}</p>
  17.                                 </div>
  18.                             </div>
  19.                         </div>    
  20.                     </script>
  21.                 </div>
  22.             </div>
  23.         </div>
  24.         <script type="text/javascript" src="//ku.shouce.ren/libs/fk7/1.4.2/js/framework7.min.js">
  25.         </script>
  26.         <script>// 声明Dom7
  27.             var $$ = Dom7;
  28.             // 初始化 App
  29.             var myApp = new Framework7({
  30.                 precompileTemplates: true
  31.                 });
  32.             var mainView = myApp.addView('.view-main');
  33.             // Now we may render our compiled template by passing required context
  34.             var context = {
  35.                 firstName: 'John',
  36.                 lastName: 'Doe'
  37.             };
  38.             mainView.router.load({
  39.                 template: Template7.templates.homeTemplate,
  40.                 context: context
  41.             })
  42.         </script>
  43.     </body>
  44. </html>

实例预览