https://github.com/phly/phly-mustache
Extensible Mustache templating for PHP
https://github.com/phly/phly-mustache
Last synced: about 1 year ago
JSON representation
Extensible Mustache templating for PHP
- Host: GitHub
- URL: https://github.com/phly/phly-mustache
- Owner: phly
- License: bsd-2-clause
- Created: 2015-09-02T15:50:35.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2022-01-05T22:07:20.000Z (over 4 years ago)
- Last Synced: 2025-04-20T04:18:45.188Z (about 1 year ago)
- Language: PHP
- Size: 657 KB
- Stars: 15
- Watchers: 3
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# phly-mustache
[](http://travis-ci.org/phly/phly-mustache)
phly-mustache is a [Mustache](http://mustache.github.com) implementation written
for PHP. It conforms to the principles of mustache, and allows for
extension of the format via pragmas.
In particular, it offers support for [template
inheritance](https://github.com/mustache/spec/pull/75) ala
[hogan.js](https://github.com/twitter/hogan.js), using the `{{render('some-template', $view);
```
By default, phly-mustache will look under the current directory for templates
ending with '.mustache'; you can create a stack of directories using the
default resolver:
```php
use Phly\Mustache\Resolver\DefaultResolver;
$resolver = new DefaultResolver();
$resolver->addTemplatePath($path1);
$resolver->addTemplatePath($path2);
$resolver = $mustache->getResolver()->attach($defaultResolver);
```
In the above, it will search first `$path2`, then `$path1` to resolve the
template.
The default resolver is composed in an aggregate resolver by default; as such,
you can also fetch it by type from the aggregate instead of adding it manually:
```php
use Phly\Mustache\Resolver\DefaultResolver;
$resolver = $mustache->getResolver()->fetchByType(DefaultResolver::class);
```
Template names may be namespaced, using the syntax `namespace::template`:
```php
$resolver->addTemplatePath($path1, 'blog');
$resolver->addTemplatePath($path2, 'contact');
```
Per the above configuratin, rendering the template `contact::index` will resolve
to `$path2`. If it cannot, it will drop back to the default namespace (any paths
registered without a namespace).
You may also change the suffix it will use to resolve templates:
```php
$resolver->setSuffix('html'); // use '.html' as the suffix
```
If your templates use pragmas, you must first add pragma handlers to the
`Mustache` pragma collection. This can be done as follows:
```php
use Phly\Mustache\Pragma\ImplicitIterator as ImplicitIteratorPragma;
$mustache->getPragmas()->add(new ImplicitIteratorPragma());
$mustache->render('template-with-pragma', $view);
```
Views can be either associative arrays or objects. For objects, any public
member, either a property or a method, may be referenced in your template. As an
example:
```php
class View
{
public $first_name = 'Matthew';
public $last_name = "Weier O'Phinney";
public function full_name()
{
return $this->first_name . ' ' . $this->last_name;
}
}
```
Any property (or array key) may also refer to a valid callback; in such cases,
the return value of the callback will be used.
```php
$view = new stdClass;
$view->first_name = 'Matthew';
$view->last_name = "Weier O'Phinney";
$view->full_name = function() use ($view) {
return $view->first_name . ' ' . $view->last_name;
};
```
Refer to the documentation ([online](http://phly-mustache.readthedocs.org) /
[local](doc/book/)) for full usage details.
## Architecture
Phly\Mustache consists of five primary classes:
- **Lexer**: tokenizes mustache syntax.
- **Renderer**: renders a list of tokens, using substitions provided via a view.
- **Pragma\PragmaInterface**: interface for pragmas, which may modify how tokens are handled.
- **Resolver\ResolverInterface**: resolves a template name to mustache syntax or tokens.
- **Mustache**: facade/gateway class. Tokenizes and renders templates, caches
tokens, provides partial aliasing, aggregates pragmas, and acts as primary
interface for end-users.