Render choice of field using conditional logic

76 views
Skip to first unread message

Scott Sauyet

unread,
May 13, 2020, 3:04:43 PM5/13/20
to TiddlyWiki
Can I replace part of the $:/core/ui/ViewTemplate/title tiddler with some conditional logic?

I've been working on auto-generating a static read-only TW5 to document a system.  It's been working quite well, thanks to some help from people on this list.  One thing I'm trying to do is to display the caption rather than the title in the tiddler card header.  But I need to do this conditionally, because not every tiddler will have the caption property.  I've found that I can modify this:

<h2 class="tc-title">
<$view field="title"/>
</h2>

to this:

<h2 class="tc-title">
<$view field="caption"/>
</h2>

to achieve the change, but I'm hoping to do that conditionally with something that works like this:

<h2 class="tc-title">
<if field caption exists><then><$view field="caption"/></then><else><$view field="title"/></else></if>
</h2>

Obviously the syntax is nowhere near real, but I think it should demonstrate what I'm trying to do.  Is this readily achievable?

Thanks,

  -- Scott

Eric Shulman

unread,
May 13, 2020, 3:33:18 PM5/13/20
to TiddlyWiki
On Wednesday, May 13, 2020 at 12:04:43 PM UTC-7, Scott Sauyet wrote:
I'm hoping to do that conditionally with something that works like this:
<h2 class="tc-title">
<if field caption exists><then><$view field="caption"/></then><else><$view field="title"/></else></if>
</h2>
Obviously the syntax is nowhere near real, but I think it should demonstrate what I'm trying to do.  Is this readily achievable?

The above functionality is already built into the <$view> widget:

in the documentation (https://tiddlywiki.com/#ViewWidget) it says:

The content of the <$view> widget is displayed if the field or property is missing or empty.

Thus, you would write something like this:
<$view field="caption"><$view field="title" /></$view>

Note how the outer $view widget has a matching /$view, and the inner $view widget uses the "short form" with the "/>" ending.

enjoy,
-e


 

Mat

unread,
May 13, 2020, 3:35:45 PM5/13/20
to TiddlyWiki
Untested but should show the idea even if something is wrong:
 
tags: $:/tags/ViewTemplate
list
-before: $:/core/ui/ViewTemplate/title
text
:
<$list filter="[all[current]has[caption]]">
<style>[data-tiddler-title="{{currentTiddler}}"] {display:none}</style>
<h2 class="tc-title"><$view field=caption/></h2>  <------------------- or <$link/>
</$list>

<:-)

Mat

unread,
May 13, 2020, 3:40:18 PM5/13/20
to TiddlyWiki
Ah, to name one thing that is wrong: It should be

<style>[data-tiddler-title="{{currentTiddler}}"].tc-title {display:none}</style>

and consequently the tc-title in the following has to be changed and you should made a custom css defintion for it:

<h2 class="tc-title-apfelstrudel"><$view field=caption/></h2>  <------------------- or <$link/>

<:-)

Scott Sauyet

unread,
May 13, 2020, 3:44:48 PM5/13/20
to TiddlyWiki
Thank you Eric,

That is exactly what I needed, and it works great.  I read about the ViewWidget but saw nothing that suggested this.  Is there a better place I should be looking for such information, so that I don't keep bothering people here with simple questions?

  -- Scott

Scott Sauyet

unread,
May 13, 2020, 3:45:49 PM5/13/20
to TiddlyWiki
Thank you Mat.  I didn't test, since Eric's solution already did what I needed.  It's amazing to see how many different ways there seem to be to do one thing here!

  -- Scott

Mat

unread,
May 13, 2020, 4:00:46 PM5/13/20
to TiddlyWiki
Right - Erics solution is way, way, better than mine. Wish I'd thought of it.

<:-)

TonyM

unread,
May 13, 2020, 4:43:15 PM5/13/20
to TiddlyWiki
Just for a Diversity of solutions, Coming late from Down under (In Sydney Australia)

<$text text={{{ [all[current]get[caption]] ~[{!!title}] }}}/>

Regards
Tony

Eric Shulman

unread,
May 13, 2020, 4:56:24 PM5/13/20
to TiddlyWiki
On Wednesday, May 13, 2020 at 1:43:15 PM UTC-7, TonyM wrote:
Just for a Diversity of solutions, Coming late from Down under (In Sydney Australia)
<$text text={{{ [all[current]get[caption]] ~[{!!title}] }}}/>

While the <$view> widget has built-in "fallback" handling, it can only be used for *displaying* a field value.  In contrast, Tony's solution, using "inline filter" syntax, is actually more generalized in that it can be used to provide a fallback value for use as a parameter in any widget, not just the $view widget.

-e


Scott Sauyet

unread,
May 14, 2020, 12:19:10 AM5/14/20
to TiddlyWiki
Thanks, everyone.

Tony, your solution is the sort of thing I was expecting.  Although I'll probably use Eric's as it's simple and accomplishes what I want, I think I will study that version.  I'm guessing that by really understanding that, I'll learn a bit more about the underlying model.  Thanks,

  -- Scott
Reply all
Reply to author
Forward
0 new messages