我已經閱讀儘可能多的鬍鬚文檔儘可能多,但我無法讓我的頭周圍如何使用partials甚至我是否正確地使用鬍子。
以下代碼正常工作。我的問題是我有三個Mustache文件,我想一次包含並渲染所有文件。
我在猜測這就是partials的意思,但我似乎無法讓它工作。
問題:
如何讓我的三個鬍子文件被加載,並且都被傳遞$ data變量我會得到泛音在這方面的工作?
我應該以這種方式使用file_get_contents作爲模板嗎?我已經看到鬍鬚功能正在使用,但我找不到足夠的文檔來使其工作。
ENV:
我使用的是最新版本的鬍子從https://github.com/bobthecow/mustache.php
我的文件有:
的index.php(下同)
template.mustache
template1.mustache
template2.mustache
class.php
CODE:
// 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);
謝謝:
一個PHP執行分音(包括必要的文件結構將如何需要來定)的任何示例,將不勝感激,只是這樣我才能得到一個堅實的理解:)
讓我建議這個帖子充滿了包括partials在內的例子,但不包括文件結構,因爲這取決於你/你的框架:http://coenraets.org/blog/2011/12/tutorial-html-templates- with-mustache-js/ –
謝謝。真的,我正在尋找使用存儲在單獨文件中的模板的示例的PHP實現。 – Lishamatish