Creating Dynamic Field Values with Double Square Brackets

I’m trying to create a macro that creates new tiddlers with a field value wrapped in double square brackets. Specifically, I want to set a “reference” field with a value like [[FieldValue]].

Here’s what I’m attempting to do:

<$button>
<$action-createtiddler 
    $basetitle={{$:/state/selected-reference!!reference}} 
    tags="test"
    reference={{{ [[$:/state/selected-reference!!reference]addprefix[[[]addsuffix[]]]] }}}>
Create
</$button>

The goal is to have the “reference” field of the new tiddler contain the value from $:/state/selected-reference!!reference, but wrapped in double square brackets - so that I can output the field later as the link to it’s reference tiddler.

However, this doesn’t work as intended. TiddlyWiki seems to interpret the square brackets in the filter expression as part of the syntax, rather than as literal characters to be included in the field value.

https://showroom.tiddlyhost.com/#Principle:Principle
this should explain it hands on.

Is there a way to create a field value that includes double square brackets around it directly when creating a new tiddler? I’d like to avoid having to modify the field value after the tiddler is created.

Any suggestions or solutions would be greatly appreciated. Thank you!

I’m not following your overall objective, but this should get you a lot further:

<$let 
open-dbl="[[" close-dbl="]]"
>
...
reference={{{ [...addprefix<open-dbl>addsuffix<close-dbl>] }}}
...
</$let>
1 Like

Unless the field is intended to contain a list of multiple tiddler titles, you should not include double square brackets in the field value. Instead, if you want to display a link using the field value, you should create the link when rendered, using a $link widget, like this:

<$link to={{!!reference}}/>
1 Like

thank you so much for your help! For lists, this shoud be the best solution. In my case, I actually use dynamic tables from Shiraz plugin (I didn’t wanted to overcomplicate my setup in explanation) - there you don’t really have the possibility to influence how fields are displayed. And since the tiddlers I plan to create like this are only temporary ones, double brackets are fine I think.

It’s a custom Dungeons & Dragons Encounter Manager using Shiraz dynamic tables. I create encounter tiddlers based on the templates of archetype monsters and I’m now able to 1-click reference back to the archetype.

Thank you very much for your help, I got it working!

1 Like

Hi @lateralus123, I’m glad to see you found a solution that meets your needs! I just wanted to say that is is possible (and not too difficult!) to customize the way a column displays in a Shiraz dynamic table. Mohammed wrote up a basic tutorial that you can refer to, but very briefly:

If the field that needs a special display is is reference

  1. Make a tiddler that will serve as the template for each cell in the reference column. Dynamic tables use tags to find the appropriate templates, so the title of this template can be whatever you want. I’ll go with “DynamicTablesBodyLink”.
  2. Give your new tiddler the tag $:/tags/Table/BodyTemplate because it will be used for cells in the body of the table. (Similarly, you would use $:/tags/Table/HeaderTemplate to add a custom header for a specific column, or $:/tags/Table/FooterTemplate for a custom footer.)
  3. Add a field called tbl-column-list which contains the space-separated names of any fields that should be displayed with this special template. At minimum, it should include reference.

Mohammed advises building your cell templates to account for both edit and view mode as well as sorting, so his sample templates look quite complex. However, if you don’t need to be able to edit these cells, the contents of this template tiddler can be very simple:

<td> <!-- generates a table cell-->
	<$link to={{{ [<currentRecord>get<currentColumn>] }}} />
</td>

Within the dynamic tables macro, <<currentRecord>> refers to each tiddler displayed as a row in the table. <<currentColumn>> obviously refers to the current column, which will generally be the name of the field being rendered (though it doesn’t have to be, as you can see in special columns like tbl-expand). So if the reference field contains only the name of the reference tiddler without square brackets, each reference cell will display a link to that tiddler.

If you do want to preserve the in-table editing functionality, it’s just a bit more complicated. This is adapted from the default body template and Mohammed’s upcoming Shiraz 3.0 rewrite, so it uses TW 5.3.2+ conditional syntax (rather than $reveal widgets) and the <<editCell>> macro from Shiraz 2.9.7.

You can see that I’m also using the same link syntax here as the contents of the cell when it’s not in edit mode.

\procedure link()
<$link to={{{ [<currentRecord>get<currentColumn>] }}} />
\end

<td>
<%if [<tempTableEdit>is[missing]] :or[<tempTableEdit>getindex[mode]!match[edit]] %>
	<<link>>
<%elseif [<tempTableSort>getindex[sortIndex]match<currentColumn>] %><!--check if the current column is not selected for sorting-->
	<<link>>
<%else%>
	<<editCell>>
<%endif%>
</td>

I know this seems like a lot of work when you already having a functioning solution (just put the wikitext link in the field!) but I hope it may be helpful if you want to do anything more complicated with dynamic tables in the future. I’d also like to second Eric’s very sound advice to keep your field contents free of wikitext whenever possible—and I say this as someone who really should have heard it in my early days and didn’t. :wink:

Lastly, if you use Discord, you should join us on the TiddlyWiki 5 server, which seems to have the most active TW-for-TTRPGs community at present!

1 Like