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
Related
This question already has answers here:
How to access dynamic variable names in twig?
(5 answers)
Closed 1 year ago.
I want get a dynamic getter-function in twig. like this:
{% set brick = project.software.[brickName]() %}
But this didn't work. "brickName" is something like "getBootstrap":
{# set brick = project.software.getBootstrap() #}
Thanks for any idea - Andreas
You need to use the attribute function, given your example and assuming the brickName is a variable set earlier, I'd use:
{% set brick = attribute(project.software, brickName) %}
In order to avoid a reference to invalid method/property, it is recommended to check before if it is defined, but maybe this would be overkill for your use case, anyway for completeness of the example, let's copy from there:
{{ attribute(project.software, brickName) is defined ? 'Exists!' : 'Brick method not found' }}
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)
%}
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}) }}
I have twig.js 0.8.2. When I try to use the "use" statement like here:
{% use "../../base/hello.twig " %}
The file is imported without any problem, but if hello.twig has at least one block having the same name of the current one, I want to use this instead:
{% use "../../base/hello.twig " with content as parent-content %}
My node.js server cast an error:
TwigException: Twig.expression.type.variable cannot follow a
Twig.expression.type.string at template:36 near 'with...'
Error parsing twig template views/parkers/hpi-check/nmr-help.twig:
TypeError: Cannot read property 'forEach' of undefined
Any ideas? Thank you.
im pretty sure that its because you have a minus in your variable name, try change it to a underscore
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