Show content if a field is not empty

I create a macro to display an image if a field image is not empty.

I follow the suggestion from this link: Filter to Show Tiddlers with a Specific Empty Field

<$list filter="[all[current]tag[Colleague]]">

<$list filter="[all[current]has:field[image]">
<$macrocall $name="image-pretty" img={{!!image}} align="right" />
</$list>

</$list>

It seems it doesn’t work for me. A grey box is displayed in all tiddlers with/without image field.

The generated html is

<img src="" class=" tc-image-error">

How should I achieve it?

should be

<$list filter="[all[current]has:field[image]]">

imbalanced brackets!

1 Like

Thanks I figure out by myself just now

<$list filter="[all[current]tag[Colleague]]">
<$set name=imagepath value={{!!image}}>
    <$list filter="[all[current]has:field[image]]">
        <$macrocall $name="image-polaroid" img=<<imagepath>> align="right" width="200px" />
    </$list>
</$set>

</$list>

You can simplify this to:

<$list filter="[<currentTiddler>tag[Colleague]has[image]]">
   <$macrocall $name="image-polaroid" img={{!!image}} align="right" width="200px" />
</$list>

Notes:

  • <currentTiddler> is slightly more efficient that all[current]
  • combine both $list filters into one
  • has:field[image] allows tiddlers where the image field exists but has blank contents. Use has[image] to only include tiddlers where the image field is not blank.
  • Use {{!!image}} field reference directly as img parameter without setting a variable

-e

3 Likes

@EricShulman Very cool! If I want the image to appear floating to the right of the text field in view mode (iff the image field has contents), how would that work?

So far, even with float css applied, the story view is stacking the image element vertically above or below the tiddler’s text display (which makes sense, because the float is only within the element, but I’m not sure how to tweak the tiddler display to put something side-by-side with the body).

-Springer