Replace substring into html code - twig

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.

Related

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

Twig: How to correctly chain two if-conditions where one condition contains the "not" keyword?

I'm trying to write an if statement in twig that doesn't seem to be working.
{% if content.thumbnail_uri != '' and '/podcasts/' not in content.thumbnail_uri %}
The first statement before the and is working properly.
For the second statement I want to ensure content.thumbnail_url (which is a string/URI that is extracted from a JSON string) doesn't contain the sub-string /podcasts/.
How do i write this if-statement correctly in a twig template?
You can use braces to group your statements correctly.
The following syntax will work:
{%
if (content.thumbnail_uri is not empty)
and ("/podcasts/" not in content.thumbnail_uri)
%}

Issues using |replace with array as replacement

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}) }}

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