5

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

Community
  • 1
  • 1
Robin Schambach
  • 617
  • 3
  • 11
  • 26
  • If you have lots of the same code you should consider using includes / macro's / extending twig with a `Twig_SimpleFunction` or `Twig_SimpleFilter` – DarkBee Mar 14 '17 at 09:27
  • Can you post a *real life example*, without "and so on", to make your problem more apparent? – Your Common Sense Mar 14 '17 at 10:02
  • 4
    you can remove the set part and just use `` – mickdev Mar 14 '17 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 – Robin Schambach Mar 14 '17 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 – Robin Schambach Mar 14 '17 at 11:06
  • @DarkBee a macro won't work since you can't return something in macros as far as I know – Robin Schambach Mar 14 '17 at 11:08
  • @Anubarak it's up to you. If you want a good answer that solves your problem at whole, not just a partial solution that is too specific to the hasty sketch you posted - then it's better to post the actual code. If you don't want a solution but just to show off - well, your code is enough. – Your Common Sense Mar 14 '17 at 11:13
  • @Anubarak What do mean? [snippet](https://twigfiddle.com/lfuh9p) – DarkBee Mar 14 '17 at 11:19
  • @YourCommonSense I don't know what this has to do with a certain "problem" it's just a simple syntax question like "how can I shorten this code in php"? `$var = $var + 1` answer: `$var++`. I have the feeling you missunderstood me. In every other language `if (variable = value) is not a comparison but a declaration and sets variable to value. But twig gives me an error when I do this way. My only question is if there is a syntax that allows it – Robin Schambach Mar 14 '17 at 11:21
  • @DarkBee http://craftcms.stackexchange.com/questions/10065/use-a-twig-macro-to-set-a-variable macros can only render code and are not able to set/return variables. Its not only about images, it`s about every single parent-child relation that I have to check. And there are a lot of them in CraftCMS – Robin Schambach Mar 14 '17 at 11:24
  • @Anubarak [updated snippet](https://twigfiddle.com/lfuh9p) – DarkBee Mar 14 '17 at 11:30
  • @Anubarak like I said, it is *entirely* up to you. You have to understand that you need a real code example not for me. I don't care actually. I just suggested it for you, in case you want to get an answer. But of course it's up to you. Cheers :) – Your Common Sense Mar 14 '17 at 11:31
  • @DarkBee thank you but this does not solve my problem at all... Its not about images, its about declaring a variable in an if block. Its just about syntax. it would take much longer to create a macro for every single parent child relation than just make 2 lines instead of 1. – Robin Schambach Mar 14 '17 at 11:36

3 Answers3

8

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

xabbuh
  • 5,801
  • 16
  • 18
  • What a pity, thank you. It seems you are the only person who understood me – Robin Schambach Mar 14 '17 at 13:31
  • @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. – Your Common Sense Mar 14 '17 at 13:44
  • 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. – Robin Schambach Mar 14 '17 at 13:54
5

You can set it with a ternary operator.

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

Tushar Wasson
  • 494
  • 5
  • 10
  • 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. – Robin Schambach Jan 20 '23 at 15:57
-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 !

Community
  • 1
  • 1