Best practice for template tiddlers that transclude current tiddler?

I’m creating a “wrapper” template that outputs some field values and the main text of a tiddler. A very simplified example would look like this:

!! {{!!title}}
!!! {{!!contextDescription}}
<section class="my-section"><$transclude /></section>

Transcluding other tiddlers with the template works fine, but when opening the template tiddler itself, I get the red “Recursive Transclusion” error message. I know why it appears and I’m not bothered by it. But I read in [tw5] Query -- How Fatal is the Red message "Recursive transclusion error in transclude widget" that the error can affect performance. What’s the best (i.e. most performant) way to deal with this error?

  • Just ignore it?
  • Wrap the transclusion in a list expression that checks if currentTiddler!=templateName?
  • Add a special tag that tells TiddlyWiki to avoid transclusion?
  • Make it a system tiddler to hide it as much as possible?

Just to check how are you applying the template to each tiddler? Eg tagged viewTemplate?

You could wrap the template codein a list widget to test if [all[current]!match[templatetitle]]
and variable=nul

I’m applying the template only to some specific pages, it’s not a viewTemplate.

Wrapping the template in a $list widget got rid of the error, so I guess that’s what I’ll go with, if there are no objections or TiddlyWiki-internal checks that could handle this.

1 Like

For the future, note that when the tiddler holding your code is displayed as a Story River tiddler (i.e. through the normal View or Edit Templates), this transclude widget is causing ALL of the same code to become translcuded into the <section> element, which starts a recursive loop of dooooooom, as it transcludes itself down into infinity. The $list widget filtering it out when not needed is a good option.

1 Like

FYI: There was a problem ion tiddlywiki displaying the system tiddler $:/core/ui/PageTemplate and with 5.2.1 the cascade templates remedied this by using the $:/core/ui/ViewTemplate/body/code template for all [prefix[$:/core]] - with some exceptions.

You could create a tiddler tagged $:/tags/ViewTemplateBodyFilter
containing

[get[title]match[Your tiddler title]] +[limit[1]then[$:/core/ui/ViewTemplate/body/code]]

and the list-before field = $:/config/ViewTemplateBodyFilters/system

This above will hide “[[Your tiddler title]]” and you can additional tiddlers, or perhaps use another method like;

[system-object[code]] +[limit[1]then[$:/core/ui/ViewTemplate/body/code]]

then in any tiddler you want the code displayed and not the result set the system-object field on that tiddler to code.

1 Like

I just built the proposed body Template for system-object=code

I also added click to edit code and a right hand settings to edit the body template tiddler.

code-system-objects-body.json (1.3 KB)

The TW core already contains some logic, that tests for a field key/value pair: code-body=yes to display the content as code. See https://tiddlywiki.com/prerelease/

1 Like

Check it out, I will develop it further. But thanks for pointing that out. You would modify that tiddler where mine is an add in.