{{#each}}...{{else}}...{{/each}}
{{#each}}
is a block expression, that iterates through items of passed Array or through properties of passed Object.
The following additional variables are available inside of this helper:
-
@index
- index number of the item. For arrays only -
@first
- equal to true for the first item in array. For arrays only -
@last
- equal to true for the last item in array. For arrays only -
@key
- name of current object property. For objects only
Iterate through Array items
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Template7 实例教程
- </title>
- </head>
- <body>
- <div class="views">
- <div class="view view-main">
- <div class="pages">
- <script id="homeTemplate" type="text/template7">
- <div data-page="home" class="page">
- <div class="page-content">
- <div class="content-block">
- <p>Here are the list of people i know:</p>
- <ul>
- {{#each people}}
- <li>{{firstName}} {{lastName}}</li>
- {{/each}}
- </ul>
- </div>
- </div>
- </div>
- </script>
- </div>
- </div>
- </div>
- <script type="text/javascript" src="//ku.shouce.ren/libs/fk7/1.4.2/js/framework7.min.js">
- </script>
- <script>// 声明Dom7
- var $$ = Dom7;
- // 初始化 App
- var myApp = new Framework7({
- precompileTemplates: true
- });
- var mainView = myApp.addView('.view-main');
- // Now we may render our compiled template by passing required context
- var context = {
- people : [
- {
- firstName: 'John',
- lastName: 'Doe'
- },
- {
- firstName: 'Mark',
- lastName: 'Johnson'
- },
- ]
- } ;
- mainView.router.load({
- template: Template7.templates.homeTemplate,
- context: context
- })
- </script>
- </body>
- </html>
- <p>Here are the list of people i know:</p>
- <ul>
- {{#each people}}
- <li>{{@index}}. {{this}}
- </li>
- {{/each}}
- </ul>
- <script>
- var context = {
- people : ['John Doe', 'Mark Johnson']
- }
- </script>
Iterate through Object properties
- <p>Car properties:</p>
- <ul>
- {{#each props}}
- <li>{{@key}}: {{this}}
- </li>
- {{/each}}
- </ul>
- <script>
- var context = {
- props: {
- power: '150 hp',
- speed: '200 km/h',
- }
- };
- </script>
{{else}} expression
- <p>Car properties:</p>
- <ul>
- {{#each props}}
- <li>{{@key}}: {{this}}</li>
- {{else}}
- <li>No properties</li>
- {{/each}}
- </ul>
- <script>
- var context = {
- props: {
- power: '150 hp',
- speed: '200 km/h',
- }
- };
- </script>
{{#if}}...{{else}}...{{/if}}
{{#if}} helper renders content if passed context is not "false" (or "undefined" or "null" or "" or "0") , otherwise it renders inverse content that optionally could be passed to {{else}} expression inside of helper:
- <a href="#" {{#if active}}class="active"{{/if}}>{{title}}</a>
- <script>
- var context ={
- active: true,
- title: 'Link',
- };
- </script>
{{else}} expression
- <p>Hello, my name is {{name}}.</p>
- {{#if hobby}}
- <p>I have hobby</p>
- {{else}}
- <p>I don't have hobby</p>
- {{/if}}
- <script>
- var context ={
- name: 'John Doe',
- hobby: false
- };
- </script>
{{#unless}}...{{else}}...{{/unless}}
{{#unless}} helper renders content if passed context is "false" (or "undefined" or "null" or "" or "0") , otherwise it renders inverse content that optionally could be passed to {{else}} expression inside of helper:
- <a href="#" {{#unless active}}class="active"{{/unless}}>{{title}}</a>
- <ul>
- {{#each props}}
- <li>{{@key}}: {{this}}
- </li>
- {{/each}}
- </ul>
- <script>
- var context={
- active: true,
- title: 'Link',
- };
- </script>
{{else}} expression
- <p>Hello, my name is {{name}}.</p>
- {{#unless hobby}}
- <p>I have hobby</p>
- {{else}}
- <p>I don't have hobby</p>
- {{/unless}}
- <ul>
- {{#each props}}
- <li>{{@key}}: {{this}}</li>
- {{/each}}
- </ul>
- <script>
- var context={
- name: 'John Doe',
- hobby: false
- };
- </script>
{{#with}}...{{/with}}
{{#with}} helper changes rendering context to the passed context:
- {{#with props}}
- <p>Car has {{power}} power and {{speed}} maximum speed</p>
- {{/with}}
- <script>
- var context={
- props: {
- power: '150 hp',
- speed: '200 km/h',
- }
- };
- </script>
{{#variableName}}...{{/variableName}}
If you pass a block expression with helper name that is in the expression context, then it will work like {{#each}} helper for this context if it is an Array, and will work like {{#with}} helper if it is an Object:
- <ul>
- {{#people}}
- <li>{{name}} - {{age}} years old</li>
- {{/people}}
- </ul>
- <script>
- var context={
- people: [
- {
- name: 'John Doe',
- age: 18
- }
- ,
- {
- name: 'Mark Johnson',
- age: 21
- }
- ]
- };
- </script>
- {{#props}}
- <p>Car has {{power}} power and {{speed}} maximum speed</p>
- {{/props}}
- <script>
- var context={
- props: {
- power: '150 hp',
- speed: '200 km/h',
- }
- };
- </script>
{{join delimiter=""}}
This plain helper will join Array items to single string with passed delimiter
- <h3>"{{title}}" TV Show</h3>
- <p>Was released in year {{year}}</p>
- <p>Genres: {{join genres delimiter=", "}}</p>
- <script>
- var context={
- title: 'Friends',
- year: 2001,
- genres: ['comedy', 'drama']
- };
- </script>
{{escape}}
This plain helper returns escaped HTML string. It escapes only the following characters: < > " &
- <h1>{{title}}</h1>
- <p>{{escape body}}</p>
- <script>
- var context={
- title: 'Paragraphs',
- body: 'We need to use <p> tags to add paragraphs in HTML',
- };
- </script>
{{js "expression"}}
This inline helper allows to execute some simple JavaScript directly in template to modify/check context on the fly or for some JS calculations
- <h3>{{title}}</h3>
- <p>Price: ${{js "this.price * 1.2"}}</p>
- <p>{{js "this.inStock ? 'In Stock' : 'Not in stock'"}}</p>
- <script>
- var context={
- title: 'iPhone 6 Plus',
- price: 1000,
- inStock: true
- };
- </script>
{{#js_compare "expression"}}...{{/js_compare}}
Block helper for easier compares of context variables. It renders content if JavaScript expression is not "false" (or "undefined" or "null" or "" or "0") , otherwise it renders inverse content that optionally could be passed to {{else}} expression inside of helper
- <h3>{{title}}</h3>
- <p>Price: ${{price}</p>
- <p>{{#js_compare "this.color === 'white' && this.memory > 16"}}Not in stock{{else}}In stock{{/js_compare}}</p>
- <script>
- var context={
- title: 'iPhone 6 Plus',
- price: 1000,
- color: 'white',
- memory: 32
- };
- </script>
Using Custom Helpers
Template7 allows to register custom helpers with the following method:
Template7.registerHelper(name, helper)
- name - string - helper name
- helper - function - helper function to handle passed context
Helper function could accepts as many arguments as required, arguments could be context, strings and hash data.
Let's look how to register helper on example of simple {{#if}} helper:
- Template7.registerHelper('if', function (condition, options) {
- // "this" in function context is equal to the expression execution context
- // "condition" argument contains passed context/condition
- /*
- @options contains object with the wollowing properties and methods:
- "hash" - contains passed hash object with parameters
- "fn" - method to pass helper block content further to compilier
- "inverse" - method to pass helper block inverse ({{else}}) content further to compilier
- "data" - contains additional expression data, like @index for arrays or @key for object
- */
- // First we need to check is the passed context is function
- if (typeof condition === 'function') condition = condition.call(this);
- // If context condition
- if (condition) {
- // We need to pass block content further to compilier with the same context and the same data:
- options.fn(this, options.data);
- }
- else {
- // We need to pass block inverse ({{else}}) content further to compilier with the same context and the same data:
- options.inverse(this, options.data);
- }
- });
Or on example of plain {{join}} helper:
- Template7.registerHelper('join', function (arr, options) {
- // First we need to check is the passed arr argument is function
- if (typeof arr === 'function') arr = arr.call(this);
- /*
- Passed delimiter is in the options.hash object:
- console.log(options.hash) -> {delimiter: ', '}
- */
- // And return joined array
- return arr.join(options.hash.delimiter);
- });
Or we can create helper to create Framework7's list-block link to work with this syntax:
- {{link url title target="_blank"}}
- Template7.registerHelper('link', function (url, title, options){
- var ret = '<li>' +
- '<a href="' + url + '" class="item-content item-link" target="' + options.hash.target + '">' +
- '<div class="item-inner">' +
- '<div class="item-title">' + title + '</div>' +
- '</div>' +
- '</a>' +
- '</li>';
- return ret;
- });
- <div class="list-block">
- <ul>
- {{#each links}}
- {{link url title target="_blank"}}
- {{/each}}
- </ul></div>
- {
- links: [
- {
- url: 'http://google.com',
- title: 'Google'
- },
- {
- url: 'http://idangero.us',
- title: 'iDangero.us'
- },
- ]
- }
输出
- <div class="list-block">
- <ul>
- <li>
- <a href="http://google.com" target="_blank" class="item-link item-content">
- <div class="item-inner">
- <div class="item-title">Google</div>
- </div>
- </a>
- </li>
- <li>
- <a href="http://idangero.us" target="_blank" class="item-link item-content">
- <div class="item-inner">
- <div class="item-title">iDangero.us</div>
- </div>
- </a>
- </li>
- </ul>
- </div>
Note, that all custom helpers should be registered before you compile templates with these helpers!
Remove Custom Helpers
Template7 allows to remove custom helpers with the following method:
Template7.unregisterHelper(name)
- name - string - helper name
Global Context
Template7 also supports global context which is accessible from any context.
We can specify it in Template7.global
property:
- Template7.global = {
- os: 'iOS',
- browser: 'Chrome',
- username: 'johndoe',
- email: 'john@doe.com'
- };
To access it in templates we need to use {{@global}}
variable:
- <p>Hello, {{@global.username}}. Your email is {{@global.email}}</p>
Access To Root Context
Sometimes we may need to access to initially passed root context in our templates. For this case we need to use {{@root}}
variable. This is especially helpful when we are deep in context:
- {
- persons: [
- {
- name: 'John',
- hobby: ['Cars', 'Food']
- },
- {
- name: 'Kyle',
- hobby: ['Travel', 'Puzzles']
- },
- ],
- showHobby: true
- }
- {{#each persons}}
- <h2>{{name}}</h2>
- <h3>Hobby:</h3>
- {{#if @root.showHobby}}
- <ul>
- {{#each hobby}}
- <li>{{this}}</li>
- {{/each}}
- </ul>
- {{/if}}
- {{/each}}
Partials
Template7 allows to reuse template using through partials. Partials are normal usual Template7 templates that may be called by other templates.
We can register and unregister partials using the following methods:
Template7.registerPartial(name, template) - register partial
- name - string - partial name
- helper - string - partial template
Template7.unregisterPartial(name) - unregister partial
- name - string - partial name
Then we can use our partials using special helper {{> "partialName"}}
Template:
- <ul class="users">
- {{#each users}}
- {{> "user"}}
- {{/each}}
- </ul>
- <ul class="admins">
- {{#each admins}}
- {{> "user"}}
- {{/each}}
- </ul>
Register partial:
- Template7.registerPartial('user', '<li><h2>{{firstName}} {{lastName}}</h2><p>{{bio}}</p></li>')
Apply to the template this context:
- {
- users: [
- {
- firstName: 'John',
- lastName: 'Doe',
- bio: 'Lorem ipsum dolor'
- },
- {
- firstName: 'Jane',
- lastName: 'Doe',
- bio: 'Donec sodales euismod augue'
- }
- ],
- admins: [
- {
- firstName: 'Mike',
- lastName: 'Doe',
- bio: 'Lorem ipsum dolor'
- },
- {
- firstName: 'Kate',
- lastName: 'Doe',
- bio: 'Donec sodales euismod augue'
- }
- ]
- }
And we will get the following output:
- <ul class="users">
- <li>
- <h2>John Doe</h2>
- <p>Lorem ipsum dolor</p>
- </li>
- <li>
- <h2>Jane Doe</h2>
- <p>Donec sodales euismod augue</p>
- </li>
- </ul>
- <ul class="admins">
- <li>
- <h2>Mike Doe</h2>
- <p>Lorem ipsum dolor</p>
- </li>
- <li>
- <h2>Kate Doe</h2>
- <p>Donec sodales euismod augue</p>
- </li>
- </ul>
Recursive Partials
We can even use partials to make recursive templates, like nested comments:
- // Simple template with just a partial
- var template = '{{> "comments"}}'
- // Register partial
- Template7.registerPartial(
- 'comments',
- '<ul>' +
- '{{#each comments}}' +
- '<li>' +
- '<h2>{{author}}</h2>' +
- '<p>{{text}}</p>' +
- '{{#if comments}}{{> "comments"}}{{/if}}' +
- '</li>' +
- '{{/each}}' +
- '</ul>'
- );
- // Compile template
- var compiledTemplate = Template7.compile(template);
- // Render template
- var output = compiledTemplate({
- comments: [
- {
- author: 'John Doe',
- text: 'Lorem ipsum dolor',
- comments: [
- {
- author: 'Mike Doe',
- text: 'Aliquam erat volutpat'
- },
- {
- author: 'Kate Doe',
- text: 'Donec eget fringilla turpis'
- }
- ]
- },
- {
- author: 'Jane Doe',
- text: 'Donec sodales euismod augue'
- }
- ]
- })
And the output will be:
- <ul class="comments">
- <li>
- <h2>John Doe</h2>
- <p>Lorem ipsum dolor</p>
- <ul class="comments">
- <li>
- <h2>Mike Doe</h2>
- <p>Aliquam erat volutpat</p>
- </li>
- <li>
- <h2>Kate Doe</h2>
- <p>Donec eget fringilla turpis</p>
- </li>
- </ul>
- </li>
- <li>
- <h2>Jane Doe</h2>
- <p>Donec sodales euismod augue</p>
- </li>
- </ul>
Performance Tips
Template7 is fast and you can make it even faster in your apps. The slowest part (but still very fast in T7) in compilation/rendering process is the compilation from string to pure JS function when you do Template7.compile()
. So don't compile the same templates multiple times, one time will be enough:
- // Begin of your app
- // Compile templates once on app load/init
- var searchTemplate = $('script#search-template').html();
- var compiledSearchTemplate = Template7.compile(searchTemplate);
- var listTemplate = $('script#list-template').html();
- var compiledListTemplate = Template7.compile(listTemplate);
- // That is all, now and further just execute compiled templates with required context
- // Just execute compiled search template with required content:
- var html = compiledSearchTemplate({/*...some data...*/});
- // Do something with html...
相关推荐
2. 引入Framework7:在项目中引入Framework7和Framework7 Vue插件,这通常通过npm完成,然后在main.js文件中注册它们。 3. 配置路由:利用Vue Router与Framework7的路由系统配合,定义应用的导航结构。 4. 创建组件...
从"框架学习代码"这个压缩包中,我们可以期待找到一些示例代码、练习项目或者教程,这些资源将有助于深入理解和掌握框架的使用方法。例如,可能会有Spring Boot的Hello World程序,或者AngularJS的简单CRUD应用,...
Python-iOS template 运行 cookiecutter:$ cookiecutter https://github.com/pybee/Python-iOS-template下载 Python iOS 支持包,提取出:ffi.frameworkPython.framework项目目录架构示例:iOS/ app/ ...
uni-app拥有庞大的开发者社区,提供了丰富的教程、示例和问题解答。同时,uni-app插件市场的不断更新,使得开发者可以找到各种解决方案和功能模块,进一步提高了开发效率。 总的来说,uni-app凭借其强大的跨平台...
在移动开发领域,特别是针对iOS平台的应用开发过程中,签名机制对于确保应用的安全性和合法性至关重要。本文档旨在介绍一种适用于iOS设备的真实机签名解决方案,该方案结合了FridaRPC、Theos Tweak以及GCDWebServer...
本文将详细介绍如何从零开始搭建Ionic开发环境,直至创建出第一个iOS和Android应用。 #### 二、基础环境需求 **操作系统:** - **Windows:** 7及以上64位操作系统。 **开发工具:** - **Node.js:** 版本要求为...
具有机移动应用程序和具有样式和组件的Web Apps(“移动优先”)与Angular共享相同的代码! 该模板使用默认的Angular导航,不建议使用Ionic 3的导航来开发网站,因此不建议使用需要Ionic导航的Ionic组件。 但是,...
标签中的"android", "ios"表明此框架兼容Android和iOS两大主流移动操作系统,"demo"意味着有可供参考的示例,"webapp"则表示该框架也可用于Web应用程序的开发。"responsive-layout"是响应式布局的关键,而...
10. **社区支持**:Vue.js和Weex都有活跃的社区,提供了大量的教程、插件和示例,可以帮助开发者解决问题并提升开发效率。 通过掌握以上知识点,开发者可以充分利用Weex的VueCLI脚手架,高效地开发出高质量的混合...
WebAPI支持Area是一种在ASP.NET Web API框架中组织和管理大型项目中路由和控制器的有效方法。Area的概念源自ASP.NET MVC,它允许将大型应用程序分解为更小、更可管理的模块,每个模块都有自己的独立路由空间。这个...
4. **Framework**:Flutter是一个框架,由Google开发,用于构建高性能的移动和Web应用程序。 5. **Responsive Website Builder**:FlutterWebsite是一个响应式网站构建工具,意味着它能够根据不同的屏幕尺寸和设备...
示例存储库React: Vue: 安卓: iOS: 介绍此模板存储库专用于生成已设置 SimpleJWT 的 Django + DRF 服务器。 这样做的目的是轻松创建展示 SimpleJWT 的清晰用法的存储库。 如果您不使用像 React 这样的前端框架或...
`$ionicLoading` 是 Ionic Framework 提供的一个服务,用于在应用程序中显示短暂的加载指示器,而 `$ionicGesture` 则用于处理触摸事件和手势,使我们能够实现更加动态和交互式的用户体验。 首先,我们需要在 ...
示例存储库Android: iOS: React: 介绍该存储库是在前端使用React来与Django,Django Rest Framework和DRF SimpleJWT应用程序进行通信的示例。用法后端(Django)说明。 cd server将您的终端/ cmd放入服务器目录。...
3. **Web/Mobile应用集成**:对于Web或移动应用程序,可以使用HttpClient或网络请求库(如JavaScript的Axios,Android的OkHttp,iOS的Alamofire等)发送HTTP请求,获取并解析返回的JSON数据。 **WCF RESTDemo项目...