madc madc -4 years ago 133
Twig Question

Render content from string/database and generate links with twig

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 now is, that is seams 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.

Any suggestions to work around the issue?

Answer Source

see and

{% 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
    class:        Twig_Extension_StringLoader
         - { name: 'twig.extension' }

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download