llanato llanato - 19 days ago 9
Twig Question

Twig: Variable in format function

I'm passing in a string to

login_create_account_label
which is:

Not a member? %sCreate a Free account%s.


And also
login_create_account_url
which has a value of:

http://localhost/register/


Due to the first format variable having HTML I've also passed in the
raw
option for this element.

<p class="highlight">
{{ login_create_account_label | format('<a href="{{ login_create_account_url }}">', '</a>') | raw }}
</p>


I've been unable to get it to output as I want it to.

Expected (Source HTML):

<p class="highlight">
Not a member? <a href="http://localhost/register/">Create a Free account</a>.
</p>


What I'm getting:

<p class="highlight">Not a member? 0Create a FREE account</a>.</p>

Answer

Twig string manipulation can be a little tricky as there's a bunch of tools available.

Instead of outputting the variable using the output ( {{...}} ) syntax, consider converting your operand to a string using the '~' operator. You can read more about it here.

http://twig.sensiolabs.org/doc/templates.html#other-operators

<p class="highlight">
{{ login_create_account_label|format('<a href="' ~ login_create_account_url ~ '">', '</a>')|raw }}
</p>
Comments