Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render content from string/database with twig? [duplicate]

Tags:

php

twig

symfony

Because of several reasons including translation of content I had to build a simple CMS to render the pages of my Symfony2 application.

My problem is that is seems impossible to render content from a string. Twig only accepts files. My content may contain dynamic parts like locale or similar, so the render power of twig would be very useful.

I tried to render it using the TwibstringBundle, but its functionality is quite limited and it does not work with the path-function.

like image 385
madc Avatar asked Dec 02 '22 00:12

madc


1 Answers

see http://twig.sensiolabs.org/doc/functions/template_from_string.html and http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service

{% include template_from_string("Hello {{ name }}") %}
{% include template_from_string(page.template) %}

Since the string loader is not loaded by default, you need to add it to your config.

# src/Acme/DemoBundle/Resources/config/services.yml
acme.twig.extension.loader:
    class:        Twig_Extension_StringLoader
    tags:
         - { name: 'twig.extension' }

Where Acme/acme is your application name and DemoBundle is the bundle you want to enable it for.

like image 146
Heyflynn Avatar answered Dec 04 '22 09:12

Heyflynn