[TW5] Default values for templates

101 views
Skip to first unread message

FrD

unread,
Sep 19, 2015, 8:53:19 AM9/19/15
to TiddlyWiki
Hi,

I'm using a template that needs a variable to be set.
So the tiddler "Gronk" calling the template looks like :

<$set name="somevariable" value="Baz">

{{Gronk||Template}}
</$set>

I'd like to have a default value defined for "somevariable" so that a direct call without setting the variable works :

{{Gronk||Template}}

I've tried to define the default value inside the template like this :

<$set name="somevariable1" filter=<<somevariable>> value=<<somevariable>> emptyValue="Default">
... template code ...
</$set>

When "somevariable" is defined outside the template (in the calling tiddler) it's OK. But when I call {{Gronk||Template}} it appears "somevariable" is not set to the default value.

I've found a workaround. In the template I put the following code :

\define myFilter() foobar$(sometext)$ +[removesuffix[foobar]]

<$set name="somevariable1" filter=<<myFilter>> value="Default" emptyValue=<<somevariable>>>
... template code ...
</$set>

I was wondering if there were a simple way of defining default values for variables inside templates.

Any ideas ?

Thanks

FrD

Tobias Beer

unread,
Sep 19, 2015, 9:07:46 AM9/19/15
to tiddl...@googlegroups.com
The way you work with the SetWidget seems about right.

Should value and emptyValue in your case not be set to the same?
Or rather, do you actually need the value parameter at all?

Anyhow, with the above complexities,
it seems to help using real-world examples
to better understand what you want
to achieve with, say, "somevariable".

Best wishes,

— tb 

Andreas Hahn

unread,
Sep 19, 2015, 9:31:00 AM9/19/15
to tiddl...@googlegroups.com
Am 19.09.2015 um 15:07 schrieb Tobias Beer:
> The way do it seems about right.

It does look right, however, an empty string for the filter parameter is
regarded as no filter parameter, therefore you have to design a filter,
that is empty when the variable is empty (which is hard) , but is not an
empty string itself.

/Andreas

FrD

unread,
Sep 19, 2015, 9:46:54 AM9/19/15
to TiddlyWiki
Hi,

@Tobias :

Well my "real life" template is a it complicated but it's easy to make an experiment.
Suppose the template you want to use is : <<somevariable>><<now>> {{!!title}}
In a test  template you can try 3 ways of writing the template :

\define myFilter() foobar$(somevariable)$ +[removesuffix[foobar]]

First expression :
<<somevariable>><<now>> {{!!title}}

Second expression :
<$set name="somevariable1" filter=<<somevariable>> value=<<somevariable>> emptyValue="Default value">
<<sometext1>><<now>> {{!!title}}
</$set>

Third expression :
<$set name="somevariable1" filter=<<myFilter>> value="Default value" emptyValue=<<somevariable>>>
<<sometext1>><<now>> {{!!title}}
</$set>

@Andreas :

You're right : the filter design is the point. I came to the same conclusion and that's why I've been using this strange filter.

But it's twisted and I was wondering if there was a simpler way to specify a default value.

Thanks to both of you !

FrD

FrD

unread,
Sep 19, 2015, 9:50:22 AM9/19/15
to TiddlyWiki
Hi again

Sorry I can't modify my previous post.

In the example, you have to replace "sometext1" by "somevariable1" !

FrD

Tobias Beer

unread,
Sep 19, 2015, 10:54:28 AM9/19/15
to tiddl...@googlegroups.com
Hi Frd,
 
But it's twisted and I was wondering if there was a simpler way to specify a default value.

A "default" how? When your filter returns empty, then you have emptyValue to cater for a default.
Otherwise at what point do you want your default to kick in if not for when your computed value is blank?

I don't see why your third expression would declare a value parameter.
When would that ever kick in?

Best wishes,

— tb

FrD

unread,
Sep 19, 2015, 11:44:53 AM9/19/15
to TiddlyWiki
Hi Tobias,

I can see it's not easy to explain my use case.

In some occasions I find it easier to pass some parameters to a template. It allows me to write more generic templates and re-use them in different contexts

So inside the template I put some placeholders for variables to be defined, like $(somevariable)$.
When the variables are defined when the template is called, it works fine. But I'd like to be able to call the template without defining all the variables : to have default values.

The default value of a variable is supposed to be used by the template when the variable is not defined when the template is called.

So inside the template I have to check whether the variable is defined or not :
If the variable is defined, that's fine, let's use its value.
If not, the default value is affected to the variable.

The set widget allows to affect a value to a variable according to the result of a filter (empty list of tiddlers or not). So I tried to go this way.
But my problem was to design the filter. I ended with a fancy filter.
I was wondering if there is a better way to do that :
- using something else than the set widget with the filter and emptyValue,
- or using a more straightforward filter.

I hope it's a bit clearer now ?

FrD


Le samedi 19 septembre 2015 16:54:28 UTC+2, Tobias Beer a écrit :
Hi Frd,
 
But it's twisted and I was wondering if there was a simpler way to specify a default value.

A "default" how? When your filter returns empty, then you have emptyValue to cater for a default.
Otherwise at what point do you want your default to kick in if not for when your computed value is blank?

So, I don't see why your third expression would declare a value parameter,
because I don't see when that would ever kick in.

Best wishes,

— tb

Evolena

unread,
Sep 19, 2015, 11:56:31 AM9/19/15
to TiddlyWiki
I've not tested what I propose, but you cloud try to define macros in your template to set the default values.

For example, if your variable in the template is somevariable:

\define somevariablevalue(value:"default") $value$

<$macrocall $name="somevariablevalue" value=<<somevariable>>/>


OK, it is less readable than only <<somevariable>>, but it may works (not tested, again).

Tobias Beer

unread,
Sep 19, 2015, 2:20:27 PM9/19/15
to TiddlyWiki
Hi Evolena,
 
\define somevariablevalue(value:"default") $value$

<$macrocall $name="somevariablevalue" value=<<somevariable>>/>

Nice idea. The biggest constraint would be, that the "default" value has to be a literal and cannot be an evaluated textreference or macro / variable.

- tb

FrD

unread,
Sep 19, 2015, 3:35:20 PM9/19/15
to TiddlyWiki
Hi Evolena,

Thanks for the tip. I've tested it : it works.
But as Tobias says, it's only useful for literals.

Anyway it's pretty useful for many cases.

Thanks

FrD

Evolena

unread,
Sep 19, 2015, 3:44:08 PM9/19/15
to tiddl...@googlegroups.com
As all the examples you provided used a literal default value, it could have been a suitable solution.

But... after a little test... good news: a macro or variable seems to be OK as default parameter value of a macro:

\define default() test
\define somevariablevalue(value:<<default>>) $value$

<$macrocall $name="somevariablevalue" value=<<somevariable>>/>

And textreference are ok too:

\define somevariablevalue(value:{{!!field}}) $value$

<$macrocall $name="somevariablevalue" value=<<somevariable>>/>

What else? ^^
(I've learnt some things today too!)

Tobias Beer

unread,
Sep 19, 2015, 6:29:51 PM9/19/15
to TiddlyWiki
Hi Evolena,
 
But... after a little test... good news: a macro or variable seems to be OK as default parameter value of a macro:

Now I'm confused, because... what was I testing earlier? ^_^

Must have been Joe Pesci, or... one of the last times this kicked in:


Quite happy to see it work, after all.

- tb

Evolena

unread,
Sep 20, 2015, 2:28:57 AM9/20/15
to TiddlyWiki
Le dimanche 20 septembre 2015 00:29:51 UTC+2, Tobias Beer a écrit :
Now I'm confused, because... what was I testing earlier? ^_^

[...] one of the last times this kicked in:


Haha, when testing I thought it didn't work because of that!

FrD

unread,
Sep 20, 2015, 3:39:56 AM9/20/15
to TiddlyWiki
Hi Evolena,

Great news !
I didn't know of default values for macros. I've learnt a lot today.
Most of my use-cases are text references like {{!!title}} or some other field as it is common when using templates.

Thanks !

FrD

Tobias Beer

unread,
Sep 20, 2015, 3:41:43 AM9/20/15
to TiddlyWiki
Haha, when testing I thought it didn't work because of that!

:D

do the double-check is the mother of correctitude

- tb 
Reply all
Reply to author
Forward
0 new messages