There is a variable preparing in PHP for rendering in a Twig template like
{{ product.variations.0.add_to_cart_form }}
And I need to change the final rendered result by adding my additional string to it.
The problem is, the value of the key add_to_cart_form
is not a string. It's maybe a result of executing some PHP function or is an element of an array. I don't understand how exactly it's prepared in PHP, but I need to change the final result simply by applying the filter |replace({'some_string':'some_string and_more'})
. If it's possible.
If I put the line {{ product.variations.0.add_to_cart_form }}
to a new template, include it in the parent template and add to a new variable like
{% set test %}
{% include directory ~ '/templates/addtocartform.html.twig' %}
{% endset %}
{% set test = test.replace({'some_string':'some_string and_more'}) %}
{{ test }}
I get nothing on the page. Again, as I understand because the variable is not a string.
Is there a way to "convert" it to a string and then apply the filter |replace()
regardless of the type of the variable ?
Something like template_to_string
(do not be confused with template_from_string
)