Issues using |replace with array as replacement - twig

I'm running into issues when trying to use the |replace function in Twig. I'm trying to display another value depending on a previous value.
{{ Results.Offer._symbol_at_attributes. Type|replace({'CP': '{{ Results.Offer._symbol_at_attributes.Packagesell}}','DP': '{{ Results.Offer._symbol_at_attributes.Sellprice }}'})}}

As Results is a variable you don't need to add quotes around it. Adding quotes around it will turn into a string. Also the {{ ... }} should only be used to output things. The correct snippet would be,
{{ Results.Offer._symbol_at_attributes. Type|replace({'CP': Results.Offer._symbol_at_attributes.Packagesell,'DP': Results.Offer._symbol_at_attributes.Sellprice}) }}

Related

How to Prevent ansible from evaluating variable values

I am calling a playbook from another playbook, as per my requirement I need to pass a variable something like {{ abc }}, now I want to stop evaluation of this variable from my outer playbook.
Like in bash, we can stop variable evaluation by using single quotes.
----
A='${abc}';
echo $A
O/P=>
${abc}
----
Can someone please help on this.
Agree with #zeitounator.
Pls use: "{% raw %}{{ whatever }}{% endraw %}"

Two Ternary Operators in One Twig Statement

I am using two different ternary operators to conditionaly add classes using twig. Here is what my code looks like:
class="topbar-links {{ dropdown ? 'topbar-links__dropdown' }} {{ cta ? 'topbar-links__cta' }}"
I am wondering if it is really necessary for me to close the first ternary statement with ending curly brackets and then immediately start a new ternary statement with opening curly brackets. Is there some way to combine this in one statement? Perhaps something like this:
class="topbar-links {{ dropdown ? 'topbar-links__dropdown', cta ? 'topbar-links__cta' }}"
Now this does not work - but it's the type of thing I am looking for. In short, some way to simplify the code. Is something like this possible? If so, how?
Thanks.
You can achieve this with
class="topbar-links{{ (dropdown ? ' topbar-links__dropdown') ~ (cta ? ' topbar-links__cta') }}"
One way of doing it would be to populate an array I guess
class="topbar-links {{ [ topbar ? 'topbar-links__dropdown', cta ? 'cta-links__dropdown', ]|filter(v => v)|join(' ') }}"
demo

Replace substring into html code

In this block I'm need to replace substring http to https
{{ system.getBlock('info-product-carousel')|raw }}
I try to use replace like this:
|replace({'%http%': "https"})
But this construction don't work in my situation:
{{ system.getBlock('info-product-carousel')|raw|replace({'%http%': "https"})} }}
I somewhere was mistaken, but I don't no twig and couldn't find the solution by myself.

How to escape % when referring to a resource field in twig?

I'm using twig and need to have access to a resource field named "%".
When I try this in the twig template I get an error:
{{ something.something2.%}}
Is there any way to escape the % ?
So i realize this is an old question but i just found the answer to it.
You have to use the attribute function:
{{ attribute(something.something2, '%') }}
This is used to get the value of a property with any special characters that twig my otherwise interpret to mean other things.
You can check out the documentation for the function here: http://twig.sensiolabs.org/doc/functions/attribute.html

How to round a value in Twig

I want to round a value in Twig.
Example: I want to display 80.5555 as 80.55.
Can any one suggest me how to do that?
{{ 80.5555 | number_format(2) }}
Here is the documentation number_format
You could use the format filter for that:
{{ '%.2f'|format(80.5555) }}
But note that it will round it to 80.56.
Twig documentation says that there's a filter for this task, since 1.15.0:
just write {{80.5555|round}}
https://twig.symfony.com/doc/1.x/filters/round.html

Resources