Use Cascades for Custom Transclusion

TiddlyWiki now has cascades to control rendering different parts of a tiddler and this is quite powerful.

See $:/ControlPanel → Info → Advanced → Cascades

Question
How to use this mechanism for custom transclusion?

For example, assume I want to replace the below code with cascades

<$tiddler tiddler=someTiddler>
<$transclude tiddler="myTemplate" mode="block"/>
</$tiddler>

In the proposed solution I like to have below features

  1. if someTiddler has the field hide-body set to yes, use $:/core/ui/ViewTemplate/body/blank instead of myTemplate
  2. if someTiddler has the field code-body set to yes, use $:/core/ui/ViewTemplate/body/code instead of myTemplate
  3. if someTiddler has the field template-body set to someTemplate uses someTemplate instead of myTemplate (e.g. user sets a custom template through template-body field)

If the cascade is provided via tagged tiddlers:

<$tiddler tiddler=someTiddler>
<$transclude tiddler={{{ [<currentTiddler>] :cascade[tag[mytag]!is[draft]get[text]]  }}} mode="block"/>
</$tiddler>

If the cascade is held in a single tiddler with one filter on each line:

<$tiddler tiddler=someTiddler>
<$transclude tiddler={{{ [<currentTiddler>] :cascade[<cascadeTiddler>get[text]split[\n]]  }}} mode="block"/>
</$tiddler>

Example cascade filter for " 1. if someTiddler has the field hide-body set to yes, use $:/core/ui/ViewTemplate/body/blank instead of myTemplate":

[hide-body[yes]then[$:/core/ui/ViewTemplate/body/blank]]

PS: untested code

3 Likes

Thanks for reposting/translating my question, @Mohammad, and thanks @saqimtiaz for the outline of how to proceed!

2 Likes

I would have thought this list would be better in the reverse order, more specific to less specific. Custom, code view, hide

Also the last (currently) item is the only thing not already in the View Template Body cascade. So lets perhaps add it, view-body-template-field.json (237 Bytes)

  • its a single tiddler solution that uses the template named in any tiddler that has its body-template
  • Create a template and name it in the body-template field of any tiddler. You will need to display the {{!!text}} if you want to see the content.

Now we have the above, if you want the content from any tiddler to be “transcluded” in another tiddler but want it to apply the “view Body template cascade” first transclude it via one of these templates;

  • $:/core/ui/ViewTemplate/body - Just body after cascaded
  • $:/core/ui/StoryTiddlerTemplate - whole of tiddler and toolbar with cascade

Eg put this in your tiddler.

;Test tiddlers
<$list filter="[prefix[Test ]] [prefix[$:/Test]] +[sort[]]">
   {{||$:/core/ui/ViewTemplate/body}}
</$list>

test-tiddlers-view-body-template.json (1.3 KB)

This addresses the OT but generalises it for the whole wiki.

However this unleashes another substantial benefit which I will share in a subsequent Topic. I will republish this solution in that new topic.

  • Can you guess what we can do with the above using existing core tiddlers?
2 Likes

see [IDEA] allow individual tiddlers to override ViewTemplateBodyFilter cascade with a specified body-template value · Issue #6952 · Jermolene/TiddlyWiki5 · GitHub, submitted on Sept 18, 2022

2 Likes

It even uses the same fieldname :nerd_face:

However now its a “single tiddler additional cascade”, and an “authorised hack method that does not harm any core tiddlers”, perhaps it need not go in the core?

  • But to make it available in the core plugins, Making it a de facto standard?
    • but this would make it a dependency for some solutions/plugins.
  • If not installed it “gracefully” reverts to defaults.
  • With this in place there are some other opportunities that could be included in such a plugin I am currently Exploring, and can share soon.
    • A key variation on the method already discussed, is to allow a “list of templates”, although normally one, We may want to include the Title/toolbar/tags but not the body. Perhaps the filter in the view body cascade could use the first[] template in a list?.
    • I have some related work for selective reorder and display of view/edit templates on any tiddler (an override) and they are very close to being the same code.

Notes:
There are good reasons for it to contain list-before=""

  • because even if people are likely to add other conditional template triggers, a local “body-template” should override any others.