Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate StringLoader Twig Extension in Symfony

I'm trying to active the Twig StringLoader Extension in a Symfony 2.3 project but just can't get the yaml-syntax right.

This post refers to the answer by Heyflynn on a post dealing with the exact same problem but providing a solution that just does not work (for me).

Writing the following lines in my app/config/config.yml just gives me the exception below:

# app/config/config.yml
acme.twig.extension.loader:
class:        Twig_Extension_StringLoader
tags:
     - { name: 'twig.extension' }

Gives me this:

FileLoaderLoadException: Cannot import resource ".../app/config/config.yml" from ".../app/config/config_dev.yml". (There is no extension able to load the configuration for "acme.twig.extension.loader" (in .../app/config/config.yml). Looked for namespace "acme.twig.extension.loader", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "apy_data_grid", "project", "acme_demo", "web_profiler", "sensio_distribution")

(I was already wondering about the acme appearence in acme.twig.extension.loader and replaced it with the name of the project bundle but that gives me the same exception again. Just cutting it off doesnt work either.)

Please help!

like image 547
user2900170 Avatar asked Feb 14 '23 22:02

user2900170


1 Answers

The Twig is not part of acme bundle, Twig is vendor bundle itself and therefore the Error is correct. There is not such namespace as acme.twig.extension.loader

The fixed code would be:

# services.yml
services:
    twig.extension.stringloader:
        class: Twig_Extension_StringLoader
        tags:
            - { name: twig.extension }

This can be added to /app/config/config.yml to use to in each bundle or add it into your bundle folder into /Resources/config/services.yml to use in only in certain bundle.

Then in twig templates use them as:

{{ include(template_from_string(page.template)) }}

The above works for me in Symfony v2.5

like image 97
99problems Avatar answered Feb 20 '23 17:02

99problems