Smarter way to test a condition for a template?

I’m making a viewtemplate. It should only be applied if the current tiddler fulfills a certain condition.

…but this nesting construct seems like it wastes the outer filter when basically the same filter is used again within:

<$list filter="""[is[current]tags[]tags[]tag[grandparent]first[]]""" variable=IGNORE >
<$list filter="""[is[current]tags[]tags[]tag[grandparent]]""" variable=tag>
...

As you can imagine, there’s risk that the ...tags[]tags[]... branches out far.

Can this be improved to only use a single listwidget (or perhaps some other construct)?

The succeeding code needs access to the current tiddler title.

Thank you.

<%if%> sets its own variable (and doesn’t change <<currentTiddler>>), so if you’re running 5.3.2+, you could try something like this:

<% if [is[current]tags[]tags[]tag[grandparent]] +[format:titlelist[]join[ ]] %>
<$list filter="[enlist<condition>]" variable=tag>

</$list>
<% endif %>

I’m not sure how the concatenation+enlisting compares performance-wise to just running the the filter twice, but it looks less redundant, at least. I imagine that the more complex the outer filter, the more you save by doing it this way.

2 Likes

@etardiff - thank you!

Looking up Conditional Shortcut Syntax it mentions that it, indeed, is a “shortcut syntax” …but not what is it a shortcut for. I’m guessing it is a listwidget.

Regardless, I’d definitely think some string ops should be faster than rerunning the filter again (which would search everything!), and this idea can be used also without the <% ...%> syntax, i.e it is as usable in regular nested listwidgets. Cool!

Wow. It’s right in the documentation:

Within an “if” or “elseif” clause, the variable condition contains the value of the first result of evaluating the filter condition.

But I never took that in. Thanks!