This is my first posting here, but I have a query that’s bugging me!
I’m reading Grok TiddlyWiki and in the section on variables the usage given is like the following example: <$set name=“varname” value=“some text”> and it closes with </$set>.
My question is fairly simple even though I didn’t find an answer: If, say, I want the variable value to show double quotes how do I do it? For example, if I want the value to be displayed literally as “The sun rises in the East” with the quotation marks, how do I write the variable name?
welcome @wikster! you can enclose your doublequotes using single quotes as below. hth!
<$set name="myVariable" value='"Some text"'>
<$text text=<<myVariable>>/>
</$set>
Take note that the $set
widget is an older syntax. It’s still valid, but for simple variable assignments it can be replaced by the $let
widget, which has a more compact and flexible syntax. Instead of writing:
<$set name="varname" value='"Some text"'>
...
</$set>
You can write:
<$let varname='"Some text"'>
...
</$let>
In addition you can assign multiple variables within the same $let
widget, like this:
<$let varname='"Some text"' another="Something else" argle="bargle">
...
</$let>
which is clearly more compact then using $set
, which requires separate widgets for each assignment:
<$set name="varname" value='"Some text"'>
<$set name="another" value="Something else">
<$set name="argle" value="bargle">
...
</$set>
</$set>
</$set>
Yes, there is a limit to which values """..."""
can enclose: the value can not end with a "
.
So there is no simple way to give this literal value to a variable nor an attribute:
"It's a pity!", he said, "This should be easier..."
Fred
But, there’s always a solution:
<$set
name=myVariable
value={{{[["It's not so simple!", he said, "But it does work..."]]}}}
>
myVariable: <<myVariable>>
</$set>
…or another…
\define myValue() "It's still not simple!", he said, "But again it does work..."
<$set
name=myVariable
value=<<myValue>>
>
myVariable: <<myVariable>>
</$set>
Fred
<$let test=""" "It's a pity!", he said, "This should be easier..." """>
<<test>>
</$let>
Now this is strange! This syntax trims the leading space, but not the trailing one!
<$let test=""" "It's a pity!", he said, "This should be easier..." """>
XXX<<test>>XXX
</$let>
gives this result:
XXX"It's a pity!", he said, "This should be easier..." XXX
Fred
I did create a PR with a fix in Sept 2020, which was rejected. It was replaced by 2 workarounds instead of a fix.
If you download the test.zip from the PR and copy the following code into a new tiddler it works as expected.
<$set name=test value=""""It's a pity!", he said, "This should be easier..."""">
xxx<<test>>xxx
</$set>
You’d need to open a new issue at GH so we may be able to fix it. May be …
-mario
Something to remember is this is about delimiters and ultimately you must use a delimiter that does not appear in the content you are delimiting. First we have the use of "
'
and """
see here however in tiddlywiki we have a range of delimiters when coding including [[ ]] {{ }}
which can delimit a string.
In @pmario’s example when we define the myValue it is delimited by the end of line, and in wiki text the <<varname>>
uses << >>
. But other encoding methods including JavaScript and others are available. Basically, the simplest one is the triple quotes """ something """
because you are very unlikely to want this in your string. Use a space to separate the delimiter from the first quote, but you can trim, split and split regexp, add/remove prefix/suffix to name a few.
Herew is one approach for the above;
\function test-trim() [<test>trim[]]
<$let test=""" "It's a pity!", he said, "This should be easier..." """>
XXX<<test-trim>>XXX
</$let>
It also allows you to avoid the $text widget.
Or simpler
\function test() [["It's a pity!", he said, "This should be 'easier'..."]]
XXX<<test>>XXX
I challenge any one to present an example we can’t delimit.
darn, i was really hoping to get my most favorite tiddler whose beloved name is """"']]}}}/>
into a variable, but i guess it just can’t be done…
Not at all
\define sillyname() """"']]}}}/>
{{{ [<sillyname>] }}}
What do you want to do now?
Although tiddlywiki tells you not to use titles like this they can still be used in most ways. But it would be silly