Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use embed within template_from_string #2761

Closed
rubenrubiob opened this issue Oct 6, 2018 · 9 comments
Closed

Use embed within template_from_string #2761

rubenrubiob opened this issue Oct 6, 2018 · 9 comments
Labels

Comments

@rubenrubiob
Copy link

Hi,

I am trying to use an embed tag in a call to the template_from_string function. Summarized, the code is something like the following:

{% set element = '<p>text text text</p>
                    {% embed "_snippets/_download-item.html.twig" %}
                        {% set download_item_url = "" %}
                        {% set download_item_title = "22 gener 2018" %}
                        {% set download_item_summary = "Suspendisse potenti" %}
                    {% endembed %}'

{{ include(template_from_string(element)) }}

When trying to render this, Twig throws an error:

Unable to find template "__string_template__54b3a6e2128ff1c07931188225a8717a29c821eab5f09ad82231dfff78713dca"

Of course, I have checked that the code works if used directly, without calling template_from_string.

Is it possible to use an embed within template_from_string? If it is not possible, I might write an extension that does what I intend, but I do not see clearly how to do so. Maybe an extension that generates a temporal template in cache —or in memory— that can be loaded within the environment?

Thank you!

@ericmorand
Copy link
Contributor

Well, this may be a bug.

The loader tries to load __string_template__54b3a6e2128ff1c07931188225a8717a29c821eab5f09ad82231dfff78713dca instead of _snippets/_download-item.html.twig and fails since this template is not registered in the loader - and i can confirm it fails with both array and filesystem loaders.

@stof, but isn't the createTemplate environment method supposed to add a loader that can resolve that template name?

@ericmorand
Copy link
Contributor

ericmorand commented Oct 9, 2018

This may be coming from that line:

$this->setLoader($current);

Commenting it out solves the issue but I'm not sure of the side effects.

Edit: test suite pass perfectly with that line commented.

@ericmorand
Copy link
Contributor

@rubenrubiob, as a workaround, you could come with your own environment subclass that only reimplement createTemplate without that line above.

@rubenrubiob
Copy link
Author

I have checked commenting that line and it indeed works fine.

Meanwhile, I will try the workaround you propose me, waiting for the solution of the issue.

Thank you!

Regards!

@stof
Copy link
Member

stof commented Oct 31, 2018

this indeed looks like a bug

@stof
Copy link
Member

stof commented Oct 31, 2018

And removing the line you mentioned would create other bugs. This does not look like the right fix.

@fabpot
Copy link
Contributor

fabpot commented Mar 12, 2019

fix in #2883

@fabpot fabpot closed this as completed Mar 12, 2019
fabpot added a commit that referenced this issue Mar 12, 2019
…fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

Fix "embed" support when used from "template_from_string"

closes #2761

Commits
-------

92a63e0 fixed "embed" support when used from "template_from_string"
@ericmorand
Copy link
Contributor

ericmorand commented Jul 18, 2019

And removing the line you mentioned would create other bugs. This does not look like the right fix.

@stof, Could you please elaborate? The test suite pass entirely when removing this line and it fixes the bug. One may wonder what is the purpose of this line and why there is no test to ensure that this purpose is fulfilled.

The official fix is really disturbing. Parsing object classname to support runtime needs, I don't even know where to start...

@stof
Copy link
Member

stof commented Jul 18, 2019

@ericmorand the testsuite passes because it does not keep using the loader after the embed. Not resetting the loader would leak the embed template for any following usage of Twig (which can create weird issues).

The official fix relies on the fact that the embedded template has already been compiled at the same time than the main template (and so the class is defined). And we have a convention for the naming of class names for embed templates based on the name of the compiled class for the main template. So we can avoid going through Environment::getTemplateClass to get the class name based on the template name (which would expect the loader to keep knowing about that name).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants