dev-master
9999999-dev https://github.com/r1pp3rj4ck/TwigstringBundleRenders strings instead of files with Twig
MIT
The Requires
by Luis Cordova
by Lars Krueger
twig string
Renders strings instead of files with Twig
This Symfony2 Bundle adds the possibility to render strings instead of files with the Symfony2 native Twig templating engine.
The Bundle adds an additional service named twigstring
. It is used the same way as the original templating service.
The only difference is that the first argument of the method render(<string>, [<array])
is a string to parse instead of a template file., (*1)
The bundles supports variable output, conditions, loops and rendering of an controller. It does NOT support including templates, yet. It would interpret the template name as twigstring., (*2)
One example usage scenario is to load templates from the database instead of using files., (*3)
Extension ideas:, (*4)
If you are an advanced Symfony developer, you know how things are done. The package's name is r1pp3rj4ck/TwigstringBundle
and you need the dev-master
version of it, and the bundle's FQN is LK\TwigstringBundle\LKTwigstringBundle()
. You also need a top-level entry to your config: lk_twigstring: ~
., (*5)
Of course, if you're not sure about yourself, here is the step-by-step installation procedure:, (*6)
You need to add it to your composer.json requirements: ``` js { "require": { "r1pp3rj4ck/TwigstringBundle": "dev-master" } }, (*7)
### 2. Downloading the bundle from packagist Install it by running the following command: $ ./composer.phar update r1pp3rj4ck/TwigstringBundle ### 3. Registering the bundle in the kernel ``` php <?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new LK\TwigstringBundle\LKTwigstringBundle(), // ... ); }
``` yaml, (*8)
lk_twigstring: ~, (*9)
## Usage ``` php // set example parameter $vars = array('var'=>'x'); // get twigstring service $tpl_engine = $this->get('twigstring'); // render example string $vars['test'] = 'u ' . $tpl_engine->render('v {{ var }} {% if var is defined %} y {% endif %} z{% for i in 1..5 %} {{ i }}{% endfor %}', $vars);
or use the short way:, (*10)
``` php // set example parameter $vars = array('var'=>'x');, (*11)
// render example string $vars['test'] = 'u ' . $this->get('twigstring')->render('v {{ var }} {% if var is defined %} y {% endif %} z{% for i in 1..5 %} {{ i }}{% endfor %}', $vars); ```, (*12)
u v x y z
The bundle is licensed under MIT license. For full license see LICENSE file, (*13)
LarsK (Lars KrĂŒger), cordoval (Luis Cordova), r1pp3rj4ck (Attila Bukor), (*14)
Renders strings instead of files with Twig
MIT
twig string