1

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?

8
  • Try {{ dump(worklog) }} to check what values you have. Commented Apr 4, 2021 at 7:15
  • Yep thx, have doublechecked this, the values in {{ worklog }} is correct, because if I use only the html showed above, the href outputs then with the correct worklogID and customerID using {{ worklog.id }} and {{ worklog.customerid }}. So either this is not possible or I'm missing something. Commented Apr 4, 2021 at 7:22
  • I think it may be because you are outputting this value dynamically. So what you output it as html.editWorklogButton|raw - it will not interpret any of the values (you are saying you want it as is with the raw bit). Commented Apr 4, 2021 at 7:25
  • I believe you have a non-standard use-case. there's no built-in directive to render a twig string within a twig template, since it can't be considered safe, especially not with the surrounding context. this would suggest, that the template comes from the user (which would be unsafe). Commented Apr 4, 2021 at 7:26
  • 1
    This might help (but I'm a bit unfamiliar with Twig so not 100% sure). Commented Apr 4, 2021 at 7:33

1 Answer 1

2

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)) }}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.