passing multiple strings into a template?

88 views
Skip to first unread message

Elijah

unread,
Mar 16, 2019, 8:24:14 PM3/16/19
to TiddlyWiki
Is there any (other) way to pass multiple strings into a template?

The way I'm currently doing it is as follows:

template:

<div style="background-color:black; color:white; padding:5px"><div align=left style="font-size: 200%"></div><div style="margin:5px"><$view field="quote"/></div><div align=right style="font-size: 200%"></div><div style="font-size: 150%">-<$view field="attribution"/></div></div>

tiddler:

{{tiddler||template}}

There are several reasons this isn't ideal, and so I am hoping someone has done something like this before, and knows another, better, way.

Mark S.

unread,
Mar 16, 2019, 8:47:24 PM3/16/19
to TiddlyWiki
In cases like this, it's better if you explain what your objective is, rather than tilting at the technology. For instance, if your sentences are stored in individual tiddlers, then perhaps some sort of listing macro would work. It all depends.

Good luck
-- Mark

Elijah

unread,
Mar 16, 2019, 9:54:11 PM3/16/19
to TiddlyWiki
My personal Tiddlywiki has hundreds of tiddlers each containing a single quotation (some just a few words, others entire excerpts with several paragraphs), from various authors. I'm trying to create a template into which I pass in both the quote, and the author's name. I want to display the quote, with attribution, in some specific, preformatted way, that is consistent throughout my Tiddlywiki. I also want the original tiddlers which contain the quotes to display this preformatted text. Basically, I want exactly what I get when I use the above template (in the OP), except I don't want to have to keep the quote, and attribution each in a field of their own. I'd like to keep them somewhere in the main text-box.

Why? One reason is that as I said, some of these quotes consist of multiple paragraphs, and you cannot easily insert a multi-paragraph string into a field. Another reason is that the boxes into which you input a field's contents (value) only display ~60 characters at a time.

If this is the only way, or the best way to do it, then so be it, but if there is another, better way, I'd like to know what it is.

Mark S.

unread,
Mar 16, 2019, 10:23:16 PM3/16/19
to TiddlyWiki
In your example, you're using a field for the quote. Why not use the text field for the actual body of the quote?

Elijah

unread,
Mar 16, 2019, 10:49:54 PM3/16/19
to tiddl...@googlegroups.com
Because then I can't get that tiddler too to display the formatted quote. At least, that is how it is currently. Right now, the tiddler with the quote, and attribution fields has the {{tiddler||template}} in the main text field.

I'll give you a "real" example to help explain.

This is the template:

<div style="background-color:black; color:white; padding:5px"><div align=left style="font-size: 200%"></div><div style="margin:5px"><$view field="quote"/></div><div align=right style="font-size: 200%"></div><div style="font-size: 150%">-<$view field="attribution"/></div></div>

It's title is "$:/eab/block-quote".

I have a tiddler literally called "untitled note" (untitled note is the default title for new tiddlers in my Tiddlywiki, and this one just happens to be the first note I added to the Tiddlywiki), it has the following contents:

text field:

{{untitled note||$:/eab/block-quote}}

"quote" field:

Empty your mind, be formless. Shapeless, like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.

And "attribution" field:

Bruce Lee

And so the tiddler "untitled note" itself displays (without the CSS, approximately):

Empty your mind, be formless. Shapeless, like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot it becomes the teapot. Now, water can flow or it can crash. Be water my friend.
                                                                                                                           

-Bruce Lee

Which is exactly what I want to display. What I want though is to not have to use the "quote", and "attribution" fields at all, because of the above mentioned "issues", especially that which is that I can't have multi-paragraph quotes with this current setup. Maybe this is the best way to do it though, in which case I'll just rework the the template to accept an arbitrary number of fields, one for each paragraph of a quote, and then display them accordingly, which now that I'm thinking about it, may not be that difficult

Mark S.

unread,
Mar 17, 2019, 12:42:03 AM3/17/19
to TiddlyWiki
What you want to do is change how the tiddler presents its content in view mode. But you don't want every tiddler to change how it appears.

Typically in this situation, you use a (real) template. Or actually, you modify the view template. To indicate that you want this tiddler treated differently, you typically do something like tag it with a special manner. Let's say you tag all quotes with "Quote". Although, if you never use "attribution" anywhere else, you could use the existence of that field. But for the moment, let's go with tag Quote.

Back up your TW, just in case. You might want to try this on TiddlyWiki.com just to see how the flow goes.

Create your tiddler with populated field "attribution". Put the text of the quote in the text box, because it's big and easy to edit. ;-) Put a tag "Quote" on the tiddler. Save it.

Edit the following tiddler (I'm assuming that you have a recent edition of TW -- I'm doing this based on v 5.1.19) : 

$:/core/ui/ViewTemplate/body

Paste in the following contents and save the tiddler:


<$list filter="[all[current]!tag[Quote]]">
<$reveal tag="div" class="tc-tiddler-body" type="nomatch" stateTitle=<
<folded-state>> text="hide" retain="yes" animate="yes">

<$list filter="[all[current]!has[plugin-type]!field:hide-body[yes]]">

<$transclude>

<$transclude tiddler="$:/language/MissingTiddler/Hint"/>

</$transclude>

</$list>

</$reveal>
</$list>
<$list filter="[tag[Quote]]">
<div style="background-color:black; color:white; padding:5px"><div align=left style="font-size: 200%"></div><div style="margin:5px"><$view field="text"/></div><div align=right style="font-size: 200%"></div><div style="font-size: 150%">-<$view field="attribution"/></div></div>
</$list>

Your tiddler should now appear formatted with text and attribution.

Note that what this does is wrap a <$list> structure around the original contents of the tiddler and displays them when the tag is not "Quote". Then it adds a 2nd <$list> to display your template text when the tag is "Quote" . So the lower part is the part you want to edit if you want to tweak your design. If you're using a recent version of TW, then the text should work out of the box.

Now any tiddler you tag with "Quote" will have it's text and attribution formatted per your template text.

HTH
-- Mark

Mohammad

unread,
Mar 17, 2019, 2:37:44 AM3/17/19
to TiddlyWiki
It is also worth to have a look at the below solution

TW-Scripts



--Mohammad

Eric Shulman

unread,
Mar 17, 2019, 3:44:36 AM3/17/19
to TiddlyWiki
On Saturday, March 16, 2019 at 6:54:11 PM UTC-7, Elijah wrote:
Rather than using the template mechanism, you might try using a macro definition instead...

1) Create a tiddler, e.g., "ShowQuoteMacro", tagged with "$:/tags/Macro", containing:
\define showQuote(quote,attrib)
<div style="background-color:black; color:white; padding:5px">
   <div align=left style="font-size: 200%">“</div>
   <div style="margin:5px; white-space:pre-wrap;">$quote$</div>
   <div align=right style="font-size: 200%">”</div>
   <div style="font-size: 150%">-$attrib$</div>
</div>
\end

2) Invoke the macro from your tiddler content, like this:
<<showQuote
"""this is a test.  Sometimes text will be long enough to word wrap onto another line or even more than one line if it is really really long.

Other times there will be line breaks
1
2
3
like those above

or sometimes there are "quotes" inside the text."""

"Joe Somebody"
>>

The macro content is essentially the same as your previous template content, with two notable differences:
* The <$view> widgets have been replaced by macro parameter references ($quote$ and $attrib$).  These references are automatically substituted by the parameter values passed into the macro when it is invoked.
* The <div> surrounding the actual quote text has an added style of "white-space:pre-wrap".  This ensures that any newlines (and other whitespace) in the content is preserved in the output.

Also, take note that the macro invocation encloses the text to be displayed in tripled-quotes.  This allows that text to contain regular quote characters itself, as shown in the example above.

That should do it.  Let me know how it goes.

enjoy,
-e
Eric Shulman
TiddlyTools.com: "Small Tools for Big Ideas!" (tm)
InsideTiddlyWiki: The Missing Manuals

Elijah

unread,
Mar 17, 2019, 11:22:22 AM3/17/19
to TiddlyWiki
Eric, that is exactly what I was looking for. Very nice.

Thanks guys.
Reply all
Reply to author
Forward
0 new messages