How to use twig object variable with at-sign # inside - symfony

I send from Symfony object which contains at-sign # in variable.
Object name is invoice and with {{ dump(invoice) }} in twig template I see object and parameter with path:
invoice[0].banSpojDod#showAs
But I dont know how to get value of this banSpojDod#showAs because there is at-sign #.
Could you help me someone please?

You could try with The attribute function can be used to access a "dynamic" attribute of a variable:
{{ attribute(invoice[0], 'banSpojDod#showAs') }}
Hope this help

Ok thanks. Problem was that I used it in loop, and some parameters not exists. I needed to add exist conditions. So my final code works:
{% for f in invoice %}
{% if attribute(f,'banSpojDod#showAs') is defined %}
{{ attribute(f,'banSpojDod#showAs') }}
{% endif %}
{% endfor %}

Related

Symfony2.7 Twig How to obtain a token?

Inside Twig file I have this code:
{% set player = app.security.getToken().getUser().getPlayer() %}
{% if player.getSelectedCharacter() is not null %}
{% set character = player.getSelectedCharacter() %}
{% .... %}
{% endif %}
But at now, app.security is deprecated. So I want to change this. I can obtain user token inside my controller and send it to the Twig. But I prefer to get it directly via Twig.
How I can do this?
As you said and mentioned in the documentation.
The app.security global is deprecated as of 2.6. The user is already
available as app.user and is_granted() is registered as function.
I think you can just try something like this in your view.
app.user.getPlayer()

Show image name in Twig, relation one to one

I have a one to one relation between Player and Image. I have also this line in my controller:
var_dump($players[0]->getLinkedImage1()->getName());
It shows the name of an image correctly.
And I have also this line in the template:
{% for players in player %}
{{ player.age }}
{{ player.linkedImage1.name }}
{% endfor %}
but I get this error:
Impossible to access an attribute ("name") on a NULL variable ("")
I expected the last line shown the same name as in the controller.
EDIT: finally I found out that the property was public, that was the reason. Anway I still understand it..
My bad !
Look your for loop..
Try
{% for player in players %}
How did you write the property ? linkedImage1?
If you wrote something like linked_image_1 or linkedImage_1 you should call
{{ player.linked_image_1 }}
or
{{ player.linkedImage_1 }}
then twig will call the related getter according to : http://api.symfony.com/2.4/Symfony/Component/DependencyInjection/Container.html#method_camelize
If its a virtual getter you can directly access with :
{{ player.getLinkedImage1().name }}
or {{ player.getLinkedImage1().getName() }} `
You should try
{{ player.getLinkedImage1().getName() }}

Twig: filter in an if condition

I want to use an filter in an if condition in Twig. The reason for this is a Symfony2 attribute, which I can't compare directly, I have to change it beforehand. I have started with this code:
{% if app.request.attributes.get('_controller')|split('::')|first == 'some\controller\name' %}
do something
{% endif %}
Unfortunately this does not function. So I thought I would use set before the comparison:
{% set controller = app.request.attributes.get('_controller')|split('::')|first %}
{% if controller == 'some\controller\name' %}
do something
{% endif %}
{{ controller }} {# would print 'some\controller\name' #}
Guess what? "do something" is not printed, even if the variable controller now exists and has the value I compare it with. What am I doing wrong?
Ok I tested it, Twig has a strange behavior. "\" is escaped or something like this.
I extended my twig environement with the var_dump function, check this:
{{ var_dump("Sybio\Bundle\WebsiteBundle\Controller\MainController") }}
//string(48) "SybioBundleWebsiteBundleControllerMainController"
{{ var_dump(app.request.attributes.get('_controller')|split('::')|first) }}
// string(52) "Sybio\Bundle\WebsiteBundle\Controller\MainController"
{{ var_dump("Sybio\\Bundle\\WebsiteBundle\\Controller\\MainController") }}
// string(52) "Sybio\Bundle\WebsiteBundle\Controller\MainController"
That's why your test is always false.
You need to double the backslashes of your compared string...
{% if app.request.attributes.get('_controller')|split('::')|first == 'some\\controller\\name' %}
do something
{% endif %}

twig convert a string to the object that it represent

imaging that i have a object and which can be called in a twig template like this:
{{ object1.object2.object3.property3A }}
well, it will show me the content if we use php to write is :
$object1->getObject2()->getObject3()->getProperty3A();
My question is if i have a string ,
$refString="object1.object2.object3.property3A";
and then it is passed to twig, how could i get the property3A? For my experience, we can do this in php like this:
$refString="object1->getObject2()->getObject3()->getProperty3A()";
echo $$refString;
But i do not know how to make it work in twig.
I didn't tested this, but i think it schould do the trick.
{#
recursively reading attributes from an object
! object1 must be available !
theValue is the value of property3A
#}
{% for key in "object1.object2.object3.property3A"|split('.') %}
{% if not loop.first %}{# skip the 'object1' part #}
{% set theValue = attribute(theValue|default(object1), key) %}
{% endif %}
{% endfor %}
I don't think there is a "shortcut" to do this in twig. If you can't find a simple way to do this, you can write you own extension, that would convert a STRING_TYPE to a VAR_TYPE.
Twig internals might put you on the right track. This is an example of what is feasable with twig extension and might inspire you.
I ran into a similar situation. This answer will only work if the object you need is available to the template and you know the name of it with a string.
In this case, you can access the object using Twig's Global Variable _context:
{% set object1 = _context['object1'] %}
And then access the methods and variables of the object as normal:
{{ object1.object2.object3.property3A }}

How do you check if an object exists in the Twig templating engine in Symfony2?

I have a multidimensional array where some objects exist and others don't. I keep getting a
Method "code" for object "stdClass" does not exist in...?
The code I am using in my template is:
{% for item in items %}
<p>{% if item.product.code %}{{ item.product.code }}{% endif %}</p>
{% endfor %}
Some products do not have this code and unfortunately this data structure is provided via a feed, so I cannot change it.
When I looked at the Twig documentation I interpreted that if an object or method was not there it would just return null?
Quickly did a lookup, hope this is works for you :p
defined
defined checks if a variable is defined in the current context. This is very useful if you use the strict_variables option:
{# defined works with variable names #}
{% if foo is defined %}
...
{% endif %}
{# and attributes on variables names #}
{% if foo.bar is defined %}
...
{% endif %}

Resources