PHP中的胡子部分 - 我如何使用它们?

时间:2022-09-18 14:31:37

CONTEXT:

I have read as much Mustache documentation as possible but I cannot get my head around how to use partials or even whether I am using Mustache in the correct way.

我已经阅读了尽可能多的Mustache文档,但我无法理解如何使用partials,甚至我是否正在以正确的方式使用Mustache。

The code below is working correctly. My problem is that I have three Mustache files that I want to include and render all at once.

以下代码工作正常。我的问题是我有三个Mustache文件,我想要包含并立即渲染所有文件。

I am guessing that this is what partials are meant for but I cannot seem to make it work.

我猜这是部分的意思,但我似乎无法使它工作。


QUESTIONS:

How I would get partials working in this context so that my three Mustache files are being loaded and are all being passed the $data variable?

我将如何在这个上下文中工作,以便我的三个Mustache文件被加载并且都被传递$ data变量?

Should I be using file_get_contents in this manner for the template? I have seen Mustache functions being used in its place but I cannot find extensive enough documentation to get it working.

我应该以这种方式使用file_get_contents作为模板吗?我已经看到Mustache函数正在使用它,但我找不到足够的文档来使它工作。


ENV:

I am using the latest version of Mustache from https://github.com/bobthecow/mustache.php

我从https://github.com/bobthecow/mustache.php使用最新版本的Mustache

My files are:
index.php ( below )
template.mustache
template1.mustache
template2.mustache
class.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 );

THANK YOU:

Any examples of a PHP implementation of partials ( including how the necessary file structure would need to be ) would be greatly appreciated, just so I am able to get a solid understanding :)

任何部分PHP实现的例子(包括必要的文件结构都需要)都会非常感激,所以我能够深入了解:)

1 个解决方案

#1


23  

The simplest is to use the "filesystem" template loader:

最简单的是使用“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);

Then, assuming your template.mustache looks something like this:

然后,假设您的template.mustache看起来像这样:

{{> template2 }}
{{> template3 }}

The template2.mustache and template3.mustache templates would be automatically loaded from the current directory when needed.

当需要时,将自动从当前目录加载template2.mustache和template3.mustache模板。

Note that this loader is used for both the original template and the partials. If you have your partials stored in a subdirectory, for example, you can add a second loader specifically for partials:

请注意,此加载程序用于原始模板和部分。例如,如果将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')
));

There's more info on these and other Mustache_Engine options on the Mustache.php wiki.

有关Mustache.php wiki上的这些和其他Mustache_Engine选项的更多信息。

#1


23  

The simplest is to use the "filesystem" template loader:

最简单的是使用“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);

Then, assuming your template.mustache looks something like this:

然后,假设您的template.mustache看起来像这样:

{{> template2 }}
{{> template3 }}

The template2.mustache and template3.mustache templates would be automatically loaded from the current directory when needed.

当需要时,将自动从当前目录加载template2.mustache和template3.mustache模板。

Note that this loader is used for both the original template and the partials. If you have your partials stored in a subdirectory, for example, you can add a second loader specifically for partials:

请注意,此加载程序用于原始模板和部分。例如,如果将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')
));

There's more info on these and other Mustache_Engine options on the Mustache.php wiki.

有关Mustache.php wiki上的这些和其他Mustache_Engine选项的更多信息。