Skip to content

Commit

Permalink
Expand the twig extension a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominick Johnson committed Apr 24, 2024
1 parent 5d1c31b commit a5567d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This is an extended fork of [Plates](https://github.com/thephpleague/plates) tha
* Loading controllers (or, any arbitrary function or object) using the same loader used to load templates.
* Loading static resources (but *not* public web assets...for now) using the same loader used to load templates.
* Name-based associations between templates, controllers, and resources.
* An optional extension adding integration with [Twig](https://twig.symfony.com)

Plates is a very handy little project, but doesn't appear to be receiving new features or responding to pull requests. Contemplate is a drop-in replacement for Plates; you should be able to simply change the import, and everything should "just work" so long as you don't have any custom template functions whose names interfere with new methods added by Contemplate. You can then add additional features over time using Contemplate's extended functionality.

Expand Down Expand Up @@ -70,7 +71,7 @@ $engine->path('some_article', 'markdown');
$form_handler_object = $engine->import('some_form', type:'form_handler_object');
```

This library also introduces an optional extension you can use to bridge Contemplate with [Twig](https://twig.symfony.com), meaning you can use both template systems simultaneously. The bridge is very small and light-weight, opting for simplicity and low overhead over full interop (e.g., a Twig template can't extend a Plates template and vice-versa; though they can include one another and share data).
This library also introduces an optional extension you can use to bridge Contemplate with Twig, meaning you can use both template systems simultaneously. The bridge is very small and light-weight, opting for simplicity and low overhead over full interop (e.g., a Twig template can't extend a Plates template and vice-versa; though they can include one another and share data).

The syntax of Twig is nicer than the regular regular PHP code used in Contemplate/Plates, and has a lot of niceties like auto-escaping. But the native Plates-style templates do have the advantages of being more flexible for advanced user cases, and easier to convert to from legacy plain-PHP code. It can be nice to have both available.

Expand All @@ -81,8 +82,17 @@ $toothpick = new DMJohnson\Contemplate\Extension\ContemplateTwig\ContemplateTwig
'autoescape' => 'html',
]);
$engine->loadExtension($toothpick);
// After loading the extension, you can use the ContemplateTwig instance as a proxy for the Twig Environment
$toothpick->addGlobal('CONFIG', $yourConfig);
// You can also expose Plates extension functions as functions/filters in Twig
// (These functions may not work if they rely on access to the Template object, however)
$toothpick->passthruFunction('uri')
$toothpick->passthruFilter('asset')
```

Then, in a controller or template, do this:

// Then, in a controller or template, do this:
```php
$this->renderTwig('profile', ['name'=>'Gath', 'location'=>'Foo']);
// Or, you can render the Twig template directly from outside a template or controller like this:
$toothpick->render('profile', ['name'=>'Gath', 'location'=>'Foo']);
Expand Down
14 changes: 14 additions & 0 deletions src/Extension/ContemplateTwig/ContemplateTwig.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ public function __call($name, $arguments)
{
return \call_user_func_array([$this->twig, $name], $arguments);
}

/** Expose one of the Contemplate template functions (registered via `registerFunction` or via an extension) to Twig */
public function passthruFunction(string $name, array $twigFuncOptions = []){
$callable = $this->contemplate->getFunction($name)->getCallback();
$twigFunc = new \Twig\TwigFunction($name, $callable, $twigFuncOptions);
$this->twig->addFunction($twigFunc);
}

/** Expose one of the Contemplate template functions (registered via `registerFunction` or via an extension) to Twig as a filter*/
public function passthruFilter(string $name, array $twigFuncOptions = []){
$callable = $this->contemplate->getFunction($name)->getCallback();
$twigFunc = new \Twig\TwigFilter($name, $callable, $twigFuncOptions);
$this->twig->addFilter($twigFunc);
}
}

0 comments on commit a5567d9

Please sign in to comment.