[tw5] If-Else Statements and Splitting Fields

I have a two-part question: I am trying to determine if a field exits and if it does split the field value into 2 variables. I am very, very new to TiddlyWiki5, and have been sticking to Classic.

Part 1:
First, I am trying something like this:

<$if> \field "entry 0" exists then do part 2 <$/if>
<$else> \this does not exist message </$else>

Part 2:
Second, I am trying to split the field into two variables at a “|”.

@@.variable_0 <$transclude field=<<variable[0]>>/>@@

\How do I split at "|" and create this second variable?
@@.variable_1 <$transclude field=<<variable[1]>>/>@@
</$list>

Thanks.

Hi,

Wikitext is highly efficient if you want to create text output, it’s not a general purpose programming language.

Filters are there to create selections of tiddlers. eg: “[tag[test]has[my-field]]”, which will list all tiddlers that are tagged: “test” and have a field: my-field. … In this case it doesn’t matter if the field value is empty or not. … “has” just tells us that the field exists.

I don’t understand, why you want to split field values. It only makes everything more complicated, as you already found out.

It’s 22:22 here in my time zone, so I don’t want to look at your “code” and guess, what the end result should look like.

For me it would be easier, if you would tell me in plain text, what you want to achieve and not how you want to achieve it.

  • How does your tiddler content look like

  • How does the field value looks like?

  • Why do you want to split it?

  • How should the end-result look like … In plain text.

-mario

What I have in the fields that I want to split are an author and quote. For example, in I have a field that looks like this:

entry (0): author name|Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Viverra ipsum nunc aliquet bibendum enim facilisis gravida.

entry (1): author name|Massa sed elementum tempus egestas. Vitae turpis massa sed elementum tempus egestas.

It’s the author and the quote that I am trying to split. The HTML should look like this:

\quote 1

<div class='quote'>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Viverra ipsum nunc aliquet bibendum enim facilisis gravida.</div>
<div class='author'>author name</div>
\quote 2

<div class='quote'>Massa sed elementum tempus egestas. Vitae turpis massa sed elementum tempus egestas.</div>
<div class='author'>author name</div>

The text I am trying to render:


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Viverra ipsum nunc aliquet bibendum enim facilisis gravida.
~ author name

Massa sed elementum tempus egestas. Vitae turpis massa sed elementum tempus egestas.

~ author name

\or if no such field exists, I would like to render this:
No quotes available.

Some tiddlers will have multiple quotes, so keeping the author and quote in the same field to keep things neat would be prefered.
Thanks.

Hi,

OK. … I see. I think you had the right idea at your other post about transclusions: https://groups.google.com/g/tiddlywiki/c/uyxEH7lLngE … Working with templates is the way the whole TW UI is built. … all of it.

TW scales best if tiddlers are used for fragments of content that make sense on its own. Author – Quote already makes sense on its own. So the most future-proof way to tackle the problem would be to use a tiddler for that. …

Some tiddlers will have multiple quotes, so keeping the author and quote in the same field to keep things neat would be prefered.

From your last sentence in your post I guess, that that doesn’t feel right for you. … So I’ll show you how to “split” your entry … and what happens

My field looks like this. … I wouldn’t use a construction like this (0) in field names. It is allowed since TW v5.2.0 … (I know that I’m paranoid :wink:


entry-0: author name | L orem ipsum dolor

<$let entry="entry-0">
<$list filter="[all[current]get<entry>split[|]trim[]]" variable=element>

<<element>>

</$list>
</$let>

The result will be come HTML code like this:

<p>
<p>author name</p>
<p>L orem ipsum dolor sit amet, consectetur</p>
</p>

OK … So the problem now is, that that’s the wrong order. … Now we need to fix this. …

The next problem will be formatting it to get … <div class="author">author name</div><div class="quote"> ... </div>
and so on … and so on.

It also doesn’t scale very well. What if you want to add more info. eg: the author birth date, or the book title where the quote is from …

I think I could “solve it”, but I would know that I would wasting my time, because it will end up in frustration. …


I know, that I don’t see the big picture of your vision. But let me tell you how I would tackle that specific problem you described.

I would go with a specific tiddler to collect quotes. eg: Albert Einstein

title: Albert Einstein
entry-0: “Genius is 1% talent and 99% percent hard work...”
entry-1: e = m * c^^2^^

{{||quotes-template}}

Then I’d create a template tiddler that is able to list all quotes, so I can show a list of all quotes if I open the Albert Einstein tiddler, as shown in the code above.

The following template will list all entry-x fields.

title: quotes-template

<$list filter="[all[current]fields[]prefix[entry]]" variable="entry">
<div class="quote">
<$transclude field=<<entry>> />
</div>
<div class="author">~ {{!!title}}</div>
</$list>

But it can’t access a single entry if needed. To access a single entry I’d create a macro, that looks very similar

title: quote-macros
tags: $:/tags/Macro

\define quote(author, entry)
<div class="quote">
<$transclude tiddler=<<__author__>> field=<<__entry__>> />
</div>
<div class="author">~ <<__author__>></div>
\end

The macro can be used with

<<quote "Albert Einstein" "entry-0">>

The templates can be used to list all quotes.

{{quote-collection-tiddler-name||quotes-template}}

Creating all that stuff took less time, than thinking about some code for your structure, that only lead to new problems. …

I’ll attach a zip-file that contains some more stuff I did play with. … Using templates is extremely powerful. So the zip contains a second template quotes-template-ol … which shows, that “entry-0” is a bad name. It should be “entry-1” …

It hope that helps.

have fun!

mario

(Attachment quote-experiment.zip is missing)