6

I know this is really trivial and not that important but it could save me some lifetime... You know you can declare variables in PHP in a if block

if( $row = $sql->fetch ){ //do something with the $row if $row = null this part is skipped } 

In twig I can't do for example (set image = object.image, if my object has no image, the variable image becomes null and the if statement does not become true

{% if image = object.image %} <img src="{{ image.getUrl() }}"> //and so on {% endif %} 

Instead I have to make it this way (check if the object has an image, if yes save it to new variable and do some stuff with it)

{% if object.image %} {% set image = object.image %} <img src="{{ image.getUrl() }}"> //and so on {% endif %} 

Of course I know this is no first world problem and you may think my question is useless but I have to write those "longer" statements many times a day so I could be few minutes faster in the end. So is there a syntax that allows me to set variables in an if block instead of comparing them?

Thank you very much

EDIT
I't the same like this Can I define a variable in a PHP if condition? just in twig

13
  • If you have lots of the same code you should consider using includes / macro's / extending twig with a Twig_SimpleFunction or Twig_SimpleFilter Commented Mar 14, 2017 at 9:27
  • Can you post a real life example, without "and so on", to make your problem more apparent? Commented Mar 14, 2017 at 10:02
  • 4
    you can remove the set part and just use <img src="{{ object.image.url }}"> Commented Mar 14, 2017 at 10:04
  • @mickdev I know but we mostly have long constructs so in the end I would have something like entry->lnkSomeFieldName->first()->lnkCategory->first()->image->first()->getUrl() and the same with the title if I would not separate it that way Commented Mar 14, 2017 at 11:05
  • 1
    @YourCommonSense I don't know why it should be important what I do after that... I just want to declare my variables in a if condition like in php Commented Mar 14, 2017 at 11:06

4 Answers 4

10

No, there is no way to declare a variable inside an if tag. Variables can only be set using the set tag.

Sign up to request clarification or add additional context in comments.

3 Comments

What a pity, thank you. It seems you are the only person who understood me
@Anubarak other persons tried to solve the problem. But, like I said, you don't want to solve a problem but only to show off. Not that I am blaming you for that but I'd like to ask you not to blame people who tried to help.
I don't see how you tried to help me... you just said you don't care and need an example but Xabbuh had no code and helped me the most. Honestly I don't really get what you mean with "other persons tried to solve the problem" I just wanted to know if there is a syntax for it or not. I appreciate the help from DarkBee since he tried to create me a workaround.
8

You can set it with a ternary operator.

{% set result = condition ? "a": "b" %}

1 Comment

That will only do it in certain cases but most of the time not since you need an additional check if result is empty or not. That was the main concern.
0

Setting a variable based on a condition is absolutely possible!

{% set variable = (condition == 1) ? 'this' : 'that' %} 

The condition can be evaluated against true, false, or another variable as well.

2 Comments

Thank you, but that was not the question and was already said before.
My version clarifies the condition portion, which might help others.
-1

You can use (check here for more help) :

{% if object.image is defined or object.image is not null %} <img src="{{ image.url }}"> {% endif %} 

Hope this helps !

3 Comments

His question is how to reduce the amount of code, not how to extend it even more
True, I did not take the time to fully understand the question, indeed! I was too focused on how to find the answer. Sorry...
How can one find the answer if he does not see the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.