2017 © Pedro PelĂĄez
 

symfony-bundle TwigstringBundle

Renders strings instead of files with Twig

image

r1pp3rj4ck/TwigstringBundle

Renders strings instead of files with Twig

  • Monday, October 13, 2014
  • by cordoval
  • Repository
  • 1 Watchers
  • 3 Stars
  • 14,472 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 17 Forks
  • 2 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

TwigstringBundle information & howto

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)

  • option to include templates
  • add a black-/whitelist for allowed allowed twig tags

Installation

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)

1. Register the bundle in composer

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(), // ... ); }

4. Registering the bundle in the configuration

``` yaml, (*8)

app/config/config.yml

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)

Example output:

u v x y z

License

The bundle is licensed under MIT license. For full license see LICENSE file, (*13)

Authors

LarsK (Lars KrĂŒger), cordoval (Luis Cordova), r1pp3rj4ck (Attila Bukor), (*14)

The Versions

13/10 2014