Template transclusions from variables or fields or macros

266 views
Skip to first unread message

Paul Lee

unread,
Nov 28, 2019, 6:24:55 AM11/28/19
to TiddlyWiki
I have a template for objects representing documents. I want to transclude the text content of the specified document from the tiddlers for that document as an object. Every document has a tiddler with various information about the document. Most of this information is stored in fields and output into the tiddler from the template, but I could also use variables if necessary. One piece of information that will be stored in either a field or a variable is the title of another tiddler, and that tiddler contains the full text of the document. From the document template, I need a way to transclude a tiddler in each of the child tiddlers based on either a field value or a variable set in those child tiddlers.

I know how to output the value of a field in a child tidder using a template. The obvious first attempt doesn't work:
{{<$view field="content-tiddler" />}}

In the child tiddler, the field content-tiddler contains the string "DemoContent", which is the exact name of the tiddler I am trying to output inside of the tiddler for this object, which is the same tiddler that is directly transcluding the template tiddler.

Since that does not work, I tried using variables. In both the template tiddler and the child tiddler, I tried setting a variable named contentTiddler with its value as the name of the tiddler that is being transcluded into the child tiddler:

<$set name="contentTiddler" value="DemoContent" />

Then, in the template tiddler:
{{<contentTiddler>}}

This does not work, and results in no visible effect. DemoContent is not empty; I copied the Gettysburg Adress into it for testing purposes.

My next attempt involves a macro, and seems to be the closest to success, although it still does not work.

In the template tiddler, I define a macro named transcludeContent with its internal variable named contentTiddler having the default value of "DemoContent":
\define transcludeContent(contentTiddler:"DemoContent")
Hello, world! {{$(contentTiddler)$}}
\end

I am assuming that I could override the value of contentTiddler by overwriting it from within the child tiddlers using <$set name="contentTiddler" value="AnotherDocumentsText" />, but I haven't bothered to test that yet, as I haven't gotten the macro to work at all, although I did try redefining contentTiddler from the child tiddler:

<$set name="contentTiddler" value="DemoContent" />


Then, I simply call the macro from inside the template tiddler:

<<transcludeContent>>

This might be close to success, although it still fails. Currently, it outputs the "Hello world!" that I put in there, and then displays a highlighted red error Recursive transclusion error in transclude widget. This happens in both the template tiddler and the child tiddler.

What am I missing?

Mark S.

unread,
Nov 28, 2019, 1:42:05 PM11/28/19
to TiddlyWiki
I'm not sure how you're activating your template.

If you have a template tiddler, tagged $:/tags/ViewTemplate, with the following content:

<$list filter="[all[current]has[content-tiddler]]">
<$transclude tiddler={{!!content-tiddler}}/>
</$list>

Then any tiddler with content-tiddler field will include content from the tiddler referenced in the content-tiddler field.

The outer list filter is to prevent the template from being activated for other tiddlers that don't have the content-tiddler field.

Good luck!

TonyM

unread,
Nov 28, 2019, 5:25:36 PM11/28/19
to TiddlyWiki
Paul,

Just a few notes to help.

Marks suggestion is great. It is important to understand 
{{<contentTiddler>}}
Is simply not valid syntax. You are trying to use the simplified version before using the general version.

However a slight variation looks similar
{{{ [<contentTiddler>] }}}
The triple braces allow you to use a inline filter
 and since you want to transclude tiddler with the name
<$transclude tiddler={{{ [<contentTiddler>] }}} />
untested!

However you can be more direct 
<$transclude tiddler=<<contentTiddler>> />

However as Mark points out you can reference the field content of the current tiddler directly for transclusion tiddler name
<$transclude tiddler={{!!content-tiddler}}/>

Regards
Tony

Paul Lee

unread,
Nov 29, 2019, 6:48:06 AM11/29/19
to TiddlyWiki


On Thursday, November 28, 2019 at 1:42:05 PM UTC-5, Mark S. wrote:
I'm not sure how you're activating your template.
 
I was simply transcluding the template with the {{||templateName}} notation, following the example. I didn't know there was a specific tag I needed to use in the template tiddler.

That <$transclude> widget solution does indeed work, and for my purposes it is an ideal solution. I had read through the filter syntax, but I will need to review it again. I notice that it still works even when I don't have the template tagged with $:/tags/ViewTemplate.

Thanks for the help!

Paul Lee

unread,
Nov 29, 2019, 6:54:00 AM11/29/19
to TiddlyWiki

However a slight variation looks similar
{{{ [<contentTiddler>] }}}
The triple braces allow you to use a inline filter

That appears to be the sort of functionality I was looking for; a way to substitute into the transclusion syntax. However, I can't quite get it to work; I'm having better luck with the <$transclude> widget methods.

However you can be more direct 
<$transclude tiddler=<<contentTiddler>> />

Where contentTiddler is a macro in some other tiddler, or a locally defined variable?

Tony, thanks for your help! :)
Reply all
Reply to author
Forward
0 new messages