olga
olga

Reputation: 969

How can I display a string that contains HTML with quotes in twig template?

How can I display a string (variable imgStr) that contains HTML with quotes in twig template?

String is :

 <img src="{{ asset( 'bundles/meeting/images/uploads/c986889a9912f69f6324d5c79c2d072119411384.png') }}" height="500" />

1) If i use raw filer <br> {{ imgStr|raw }} <br> i am getting empty square instead of image. I believe the reason is ''/"" quotes inside the string.

2) How to use function template_from_string? According official documentation, i must add the Twig_Extension_StringLoader extension explicitly when creating your Twig environment. But i do not understand how to make this in Symfony2. I also did not find this function in Vendor/twig/extensions .

Upvotes: 0

Views: 1128

Answers (1)

xocasdashdash
xocasdashdash

Reputation: 538

You can try loading the template_from_string extension declaring it as a service. Here you have it in yml format:

myproject.load_template_from_string:
  class: Twig_Extension_StringLoader
  tags:
    - { name: twig.extension }

This way the extension is loaded on the compiler pass and then you can use it like this:

{% set router = "<script src=\"{{ asset('bundles/fosjsrouting/js/router.js') }}\"></script>" %}
{{ include(template_from_string(router))}}

Note: I had to escape the variable because it's declared inline on the same template, but it should work without a hitch with variables passed to the template.

Upvotes: 2

Related Questions