Accessing array using constant - twig

I am generating an associative array in my controller class and want to access the different sub arrays via a constant.
I tried the following, but the render engine throws up an error:
{% for job in scheduledJobs.(constant('Namespace\\Class::CONSTANT')) %}
Twig_Error_Syntax: Expected name or number in
"dashboard.html.twig" at line 67
What is wrong?
I am using 1.15.0.

Okay, I found the solution. You can use the attribute function.
The source code would look like the following:
{% for job in attribute(scheduledJobs, constant('Namespace\\Class::CONSTANT')) %}
http://twig.sensiolabs.org/doc/functions/attribute.html

Related

Twig - path and dynamic methodname [duplicate]

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

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

Twig use statement fails with "with ... as" declarations

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

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

Resources