-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
3 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,11 @@ | ||
# Madapaja.TwigModule | ||
|
||
Madapaja.TwigModule is [Twig](http://twig.sensiolabs.org/) **v2** adaptor extension for [BEAR.Sunday](https://github.com/koriym/BEAR.Sunday) framework. | ||
Madapaja.TwigModule is [Twig](http://twig.sensiolabs.org/) v2 module for [BEAR.Sunday](https://github.com/koriym/BEAR.Sunday) framework. | ||
|
||
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/madapaja/Madapaja.TwigModule/badges/quality-score.png?b=2.x)](https://scrutinizer-ci.com/g/madapaja/Madapaja.TwigModule/?branch=2.x) | ||
[![Code Coverage](https://scrutinizer-ci.com/g/madapaja/Madapaja.TwigModule/badges/coverage.png?b=2.x)](https://scrutinizer-ci.com/g/madapaja/Madapaja.TwigModule/?branch=2.x) | ||
[![Build Status](https://travis-ci.org/madapaja/Madapaja.TwigModule.svg)](https://travis-ci.org/madapaja/Madapaja.TwigModule) | ||
|
||
# Installation | ||
# Documentation | ||
|
||
## Composer install | ||
|
||
``` | ||
composer require madapaja/twig-module 2.x-dev | ||
``` | ||
|
||
## Module Install | ||
|
||
```php | ||
namespace MyVendor\MyPackage\Module; | ||
|
||
use Ray\Di\AbstractModule; | ||
|
||
class HtmlModule extends AbstractModule | ||
{ | ||
protected function configure() | ||
{ | ||
$this->install(new TwigModule); | ||
} | ||
} | ||
``` | ||
|
||
Place twig template file in the same directory of resource class or `var/templates/` directory. | ||
|
||
```twig | ||
<h1>{{ greeting }}</h1> | ||
``` | ||
|
||
`_ro` is automatically assigned as target resoure. | ||
|
||
```twig | ||
{{ _ro.code }} // resouce code | ||
{{ _ro.header.content-type }} // header | ||
{% for todo in _ro.body %} // body | ||
{{ todo.title }} | ||
{% endfor %} | ||
``` | ||
|
||
For example, The template file for `Resource/Page/Index` class should be place either | ||
|
||
* `src/Resource/Page/Index.html.twig` | ||
|
||
or | ||
|
||
* `var/templates/Page/Index.html.twig` | ||
|
||
## Extending Twig | ||
|
||
You may want to extend twig with `addExtension()` method. | ||
In that case, provide your own Twig Provider class. | ||
|
||
```php | ||
use Ray\Di\Di\Named; | ||
use Ray\Di\ProviderInterface; | ||
|
||
class MyTwigEnvironmentProvider implements ProviderInterface | ||
{ | ||
private $twig; | ||
|
||
/** | ||
* @Named("original") | ||
*/ | ||
public function __construct(\Twig_Environment $twig) | ||
{ | ||
// $twig is an original \Twig_Environment instance | ||
$this->twig = $twig; | ||
} | ||
|
||
public function get() | ||
{ | ||
// Extending Twig | ||
$this->twig->addExtension(new MyTwigExtension()); | ||
|
||
return $this->twig; | ||
} | ||
} | ||
``` | ||
|
||
And bind it to `Twig_Environment` class. | ||
|
||
```php | ||
class HtmlModule extends AbstractModule | ||
{ | ||
protected function configure() | ||
{ | ||
$this->install(new TwigModule()); | ||
|
||
// override \Twig_Environment provider | ||
$this | ||
->bind(\Twig_Environment::class) | ||
->toProvider(MyTwigEnvironmentProvider::class) | ||
->in(Scope::SINGLETON); | ||
} | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
You may want to specify custom path or options for Twig template engine. | ||
|
||
```php | ||
class HtmlModule extends AbstractModule | ||
{ | ||
protected function configure() | ||
{ | ||
$appDir = dirname(dirname(__DIR__)); | ||
$paths = [ | ||
$appDir . '/src/Resource', | ||
$appDir . '/var/templates' | ||
]; | ||
$options = [ | ||
'debug' => true, | ||
'cache' => $appDir . '/tmp/twig' | ||
]; | ||
$this->install(new TwigModule($paths, $options)); | ||
} | ||
} | ||
``` | ||
The documentation is available in [English](http://bearsunday.github.io/manuals/1.0/en/html-v2.html) or [Japanese](http://bearsunday.github.io/manuals/1.0/ja/html-v2.html). |