How to check if an image is undefined from inside a macro?
SOLVE
What sorts of values can you pass into macros? I can pass strings fine, but for images I have been passing in the src and alt attributes in lieu of an image object. Can I pass the image object itself somehow?
The normal {% if image.src%} doesn't work from inside the macro. I also tried using the "default" filter '{{widget.image.src|default("empty")}}' to detect an empty image, but it never gets used. Another puzzling thing is when I try running the "count" filter on the value. {{image|count}} gives me a value of 38 or so, when I don't have an image defined.
Is there a good way to pass images into a macro or detect if the image passed in is defined or not?
I think the challenge is trying to access the object from within your macro. Set it up so your if statement/default is in the before passing into the macro.
Can you post your macro code or explain your end goal/use case? That would help me understand.
This doesn't work. Foo is the image field, and the always shows blank, regardless of if there's an image defined or not.
I did end up working around the problem, by detecting if the image is empty first, and then calling the macro without the final parameter if it doesn't. Kind of messy, but it works. Thanks for the help, though!
EDIT: Ah, didn't see that latest reply. That's great that there's a solution, in this case I would just create an image object with the {% image %} code, but not display it? Then use that?
How to check if an image is undefined from inside a macro?
SOLVE
I don't think you really need a macro or if statement for what you are doing. With the default image module, if an image is not selected, it won't include an image tag. Use HUBL and don't specify a default image.
{% image "image1" label='My Image' %}
Without an image selected, the markup is just an empty wrapper:
It still displays an image with no source, and just the alt tag. The other thing is I'm using a jQuery plugin for lazy loading that needs the image source to be in a data-layzr attribute instead of src.