- Framework7教程
- Framework7 - 主页
- Framework7 - 概述
- Framework7 - 环境
- Framework7 组件
- Framework7 - 布局
- Framework7 - 导航栏
- Framework7 - 工具栏
- Framework7 - 搜索栏
- Framework7 - 状态栏
- Framework7 - 侧面板
- Framework7 - 内容块
- Framework7 - 布局网格
- Framework7 - 覆盖层
- Framework7 - 预加载器
- Framework7 - 进度条
- Framework7 - 列表视图
- Framework7 - 手风琴
- Framework7 - 卡片
- Framework7 - 芯片
- Framework7 - 按钮
- Framework7 - 操作按钮
- Framework7 - 表格
- Framework7 - 选项卡
- Framework7 - Swiper 滑块
- Framework7 - 照片浏览器
- Framework7 - 自动完成
- Framework7 - 选择器
- Framework7 - 日历
- Framework7 - 刷新
- Framework7 - 无限滚动
- Framework7 - 消息
- Framework7 - 消息栏
- Framework7 - 通知
- Framework7 - 延迟加载
- Framework7 样式
- Framework7 - 颜色主题
- Framework7 - 细线
- Framework7 模板
- Framework7 - 模板概述
- Framework7 - 自动编译
- Framework7 - Template7 页面
- Framework7 快速点击
- Framework7 - 活动状态
- Framework7 - 点击保持事件
- Framework7 - 触摸波纹
- Framework7 有用的资源
- Framework7 - 快速指南
- Framework7 - 有用的资源
- Framework7 - 讨论
Framework7 - Template7 页面
描述
Template7 是一个移动优先的 JavaScript 模板引擎,具有类似handlebars.js 的语法。它是 Framework7 中超轻量且速度极快的默认模板引擎。
首先,我们需要在应用程序初始化时传递以下参数,将所有 Ajax 和动态页面呈现为 Template7 模板 -
var myApp = new Framework7 ({ template7Pages: true // enable Template7 rendering for Ajax and Dynamic pages });
序列号 | Template7 页用法和说明 |
---|---|
1 | 模板/页面数据
您可以通过在初始化应用程序时发送的template7Data参数中指定所有页面数据来传递特定页面所需的数据/上下文。 |
2 | 传递自定义上下文
Framework7 允许您将自定义上下文传递到任何动态页面或任何加载的 Ajax。 |
3 | 直接加载模板
您可以将模板动态渲染和加载为动态页面。 |
4 | 网址查询
如果您使用 Template7 来呈现 Ajax 页面,则其上下文始终将使用特殊属性url_query进行扩展。 |
例子
以下示例提供了链接,当您单击这些链接时,会显示用户信息,例如用户详细信息、用户喜欢等。
索引.html
<!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <meta name = "viewport" content = "width = device-width, initial-scale = 1, maximum-scale = 1, minimum-scale = 1, user-scalable = no, minimal-ui" /> <meta name = "apple-mobile-web-app-capable" content = "yes" /> <meta name = "apple-mobile-web-app-status-bar-style" content = "black" /> <title>Framework7</title> <link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.min.css" /> <link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.colors.min.css" /> </head> <body> <div class = "views"> <div class = "view view-main"> <div class = "navbar"> <div class = "navbar-inner"> <div class = "center sliding">Template7 Pages</div> </div> </div> <div class = "pages navbar-through toolbar-through"> <div data-page = "index" class = "page"> <div class = "page-content"> <div class = "list-block"> <ul> <li> //plain data objects allow to pass custom context to loaded page using 'data-context-name' attribute <a href = "#" data-template = "about" data-context-name = "about" class = "item-link item-content"> <div class = "item-inner"> //provides link as 'About' <div class = "item-title">About</div> </div> </a> </li> <li> //a context name for this link we pass context path from template7Data root <a href = "/framework7/src/likes.html" class = "item-link item-content"> <div class = "item-inner"> //provides link as 'Likes' <div class = "item-title">Likes</div> </div> </a> </li> </ul> </div> </div> </div> </div> </div> </div> <script type = "text/template7" id = "about"> <div class = "navbar"> <div class = "navbar-inner"> <div class = "left sliding"> <a href = "#" class = "back link"> <i class = "icon icon-back"></i><span>Back</span></a> </div> <div class = "center sliding">About Me</div> <div class = "right"> <a href = "#" class = "link icon-only open-panel"> <i class = "icon icon-bars"></i></a> </div> </div> </div> <div class = "pages"> <div data-page = "about" class = "page"> <div class = "page-content"> <div class = "content-block"> <div class = "content-block-inner"> //displays the details of the user by using the 'my-app.js' file <p>Hello, i am <b>{{firstname}} {{lastname}}</b>, <b>{{age}}</b> years old and working as <b>{{position}}</b> at <b>{{company}}</b>.</p> </div> </div> </div> </div> </div> </script> <script type = "text/javascript" src = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/js/framework7.min.js"></script> <script type = "text/javascript" src = "/framework7/src/js/my-app.js"> </script> </body> </html>
我的应用程序.js
// Initialize your app var myApp = new Framework7 ({ animateNavBackIcon: true, // Enable templates auto precompilation precompileTemplates: true, // Enabled rendering pages using Template7 template7Pages: true, // Specify Template7 data for pages template7Data: { //provides the url for different page with data-page = "likes" 'url:likes.html': { likes: [ { title: 'Nelson Mandela', description: 'Champion of Freedom' }, { title: 'Srinivasa Ramanujan', description: 'The Man Who Knew Infinity' }, { title: 'James Cameron', description: 'Famous Filmmaker' } ] }, about: { firstname: 'William ', lastname: 'Root', age: 27, position: 'Developer', company: 'TechShell', } } }); // Add main View var mainView = myApp.addView('.view-main', { // Enable dynamic Navbar dynamicNavbar: true });
喜欢.html
<div class = "navbar"> <div class = "navbar-inner"> <div class = "left sliding"> <a href = "#" class = "back link"> <i class = "icon icon-back"></i><span>Back</span></a> </div> <div class = "center sliding">Likes</div> <div class = "right"> <a href = "#" class = "link icon-only open-panel"> <i class = "icon icon-bars"></i></a> </div> </div> </div> <div class = "pages"> <div data-page = "likes" class = "page"> <div class = "page-content"> <div class = "content-block-title">My Likes</div> <div class = "list-block media-list"> //iterate through likes <ul> {{#each likes}} <li class = "item-content"> <div class = "item-inner"> <div class = "item-title-row"> //displays the title and description by using the 'my-app.js' file <div class = "item-title">{{title}}</div> </div> <div class = "item-subtitle">{{description}}</div> </div> </li> {{/each}} </ul> </div> </div> </div> </div>
输出
让我们执行以下步骤来看看上面给出的代码是如何工作的 -
将上面给定的 HTML 代码保存为服务器根文件夹中的index.html文件。
以 http://localhost/index.html 打开此 HTML 文件,输出如下所示。
该示例提供了链接,当您单击这些链接时,会显示用户信息,例如用户详细信息、用户喜欢的信息。