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.
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.
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?
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.
ENV:
I am using the latest version of Mustache from https://github.com/bobthecow/mustache.php
My files are:
index.php ( below )
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'; 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.
Mustache is a logic-less template syntax. It can be used for HTML, config files, source code - anything. It works by expanding tags in a template using values provided in a hash or object. We call it "logic-less" because there are no if statements, else clauses, or for loops.
Mustache is a simple web template system. It is available for many programming languages including JavaScript and Java. Mustache is described as a logic-less template engine because it does not have any explicit control flow statements, such as if and else conditionals or for loops.
The simplest is to use the "filesystem" template loader:
<?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:
{{> template2 }}
{{> template3 }}
The template2.mustache
and template3.mustache
templates would be automatically loaded from the current directory when needed.
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:
<?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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With