背景:
我已经阅读了尽可能多的Mustache文档,但是我无法理解如何使用partials,甚至我是否正在以正确的方式使用Mustache.
以下代码工作正常.我的问题是我有三个Mustache文件,我想要包含并一次渲染所有文件.
我猜这是部分的意思,但我似乎无法使它工作.
问题:
我将如何在这个上下文中工作,以便我的三个Mustache文件被加载并且都被传递$ data变量?
我应该以这种方式使用file_get_contents作为模板吗?我已经看到使用Mustache函数,但我找不到足够的文档来使它工作.
ENV:
我从https://github.com/bobthecow/mustache.php使用最新版本的Mustache
我的文件是:
index.php(下面)
template.mustache
template1.mustache
template2.mustache
class.php
码:
// This is index.php
// Require mustache for our templates
require 'mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();
// Init template engine
$m = new Mustache_Engine;
// Set up our templates
$template = file_get_contents("template.mustache");
// Include the class which contains all the data and initialise it
include('class.php');
$data = new class();
// Render the template
print $m->render( $template, $data );
Run Code Online (Sandbox Code Playgroud)
谢谢:
任何部分PHP实现的例子(包括必要的文件结构都需要)都会非常感激,所以我能够深入了解:)
bob*_*cow 24
最简单的是使用"filesystem"模板加载器:
<?php
// This is index.php
// Require mustache for our templates
require 'mustache/src/Mustache/Autoloader.php';
Mustache_Autoloader::register();
// Init template engine
$m = new Mustache_Engine(array(
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__))
));
// Include the class which contains all the data and initialise it
include('class.php');
$data = new class();
// Render the template
print $m->render('template', $data);
Run Code Online (Sandbox Code Playgroud)
然后,假设你template.mustache看起来像这样:
{{> template2 }}
{{> template3 }}
Run Code Online (Sandbox Code Playgroud)
在template2.mustache和template3.mustache模板将在需要时自动从当前目录加载.
请注意,此加载程序用于原始模板和部分.例如,如果将partials存储在子目录中,则可以添加专门用于partials的第二个加载器:
<?php
$m = new Mustache_Engine(array(
'loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views'),
'partials_loader' => new Mustache_Loader_FilesystemLoader(dirname(__FILE__).'/views/partials')
));
Run Code Online (Sandbox Code Playgroud)
Mustache_Engine有关Mustache.php wiki上的这些和其他选项的更多信息.
| 归档时间: |
|
| 查看次数: |
11433 次 |
| 最近记录: |