Framework7-template7-pages
提供:Dev Guides
Framework7-Template7ページ
説明
Template7は、http://handlebarsjs.com/[handlebars.js]のような構文を持つモバイルファーストのJavaScriptテンプレートエンジンです。 これは、Framework7の超軽量で非常に高速なデフォルトのテンプレートエンジンです。
まず、すべてのAjaxおよび動的ページをTemplate7テンプレートとしてレンダリングするアプリの初期化で次のパラメーターを渡す必要があります-
var myApp = new Framework7 ({
template7Pages: true//enable Template7 rendering for Ajax and Dynamic pages
});
S.No | Template7 Pages Usage & Description |
---|---|
1 |
アプリの初期化時に送信される_template7Data_パラメーターですべてのページデータを指定することにより、特定のページに必要なデータ/コンテキストを渡すことができます。 |
2 |
Framework7では、カスタムコンテキストを任意の動的ページまたは読み込まれたAjaxに渡すことができます。 |
3 |
テンプレートを動的ページとしてオンザフライでレンダリングおよびロードできます。 |
4 |
AjaxページのレンダリングにTemplate7を使用している場合、そのコンテキストは常に特別なプロパティ_url_query_で拡張されます。 |
例
次の例では、ユーザーの詳細、いいね!などのユーザー情報を表示するリンクを提供します。 それらのリンクをクリックすると。
インデックス
<!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/likesl" 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>
my-app.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:likesl': {
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
});
いいね
<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コードを indexl ファイルとしてサーバーのルートフォルダーに保存します。
- このHTMLファイルをhttp://localhost/indexlとして開くと、出力は以下のように表示されます。
- この例では、ユーザーの詳細などのユーザー情報を表示するリンクを提供します。ユーザーがそれらのリンクをクリックすると、ユーザーのお気に入りが表示されます。