1

I created a variable, like this :

{% set checkboxHTML = '<button class="btn btn-default btn-md" data-id="{{b.id}}">Edit</button>' %}

Now I trying print in two ways:

  1. {{ checkboxHTML }}

Above it printed like text (no html)

  1. {{ checkboxHTML | raw }}

Print the html, but the variable {{ b.id }} doesn't take it like twig sintax, take it like text

How to print that variable inside a text ?

2 Answers 2

5

Alternative method to solve this is using the expanded {% set %}
Do note when using this method, the content is considered being safe

{% set checkboxHTML %}
    <button class="btn btn-default btn-md" data-id="{{ b.id }}">Edit</button>
{% endset %}

twigfiddle

Sign up to request clarification or add additional context in comments.

Comments

4

You have to use set, like this:

{% set checkboxHTML = '<button class="btn btn-default btn-md" data-id="' ~ b.id ~ '">Edit</button>' %}

Documentation: https://twig.symfony.com/doc/2.x/tags/set.html

2 Comments

sorry, I use set, I forgot put it, but event though doesn't work
@JoseAlejandroRamirezRivera ok, I edited and added how you can add variable, you use ~

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.