Lurifax
Lurifax

Reputation: 35

Render twig HTML string with twig-variable

I have some html buttons I want to render with twig. This is the HTML:

<a href="/Worklog/editWorklog?worklogid={{worklog.id}}&customerid={{worklog.customerid}}"><button class="btn btn-primary">Edit worklog</button></a>

I created a method in php to return the HTML string above which I pass in with twig like this:

{{ html.editWorklogButton|raw }}

But when the button is rendered with raw it also renders {{ worklog.id }} and {{ worklog.customerid }} raw of course, losing the id's, giving me href to:

localhost/Worklog/editWorklog?worklogid={{worklog.id}}&customerid={{worklog.customerid}}

which instead should be something like:

localhost/Worklog/editWorklog?worklogid=1&customerid=2

I've checked twig documentation, but can't find anything on this. Is this simply not possible to do?

Upvotes: 1

Views: 2131

Answers (1)

Jeto
Jeto

Reputation: 14927

You may use the template_from_string extension.

The template_from_string function loads a template from a string.

In your case, it should be something like this:

{{ include(template_from_string(html.editWorklogButton)) }}

Upvotes: 2

Related Questions