Infobox templates

865 views
Skip to first unread message

Pluszak

unread,
Mar 3, 2009, 12:45:10 PM3/3/09
to TiddlyWiki
I'd like to create a template to use like an infobox (like on
Wikipedia).
Is there a way to define something once and then just include it on
the pages I need with the parameters I give?
Like once defying a tiddler named Infobox and then in others just
doing {{Infobox|foo|bar}}

Eric Shulman

unread,
Mar 3, 2009, 1:02:15 PM3/3/09
to TiddlyWiki
> Is there a way to define something once and then just include it on
> the pages I need with the parameters I give?
> Like once defying a tiddler named Infobox and then in others just
> doing {{Infobox|foo|bar}}

First, create the tiddler called [[InfoBox]] with the desired
content. You can then 'transclude' (include and render) that
tiddler's content into another tiddler, by using the TW core macro:

<<tiddler InfoBox>>

This macro also allows you to pass optional space-separated
parameters, like this:

<<tiddler InfoBox with: "foo" "bar" "baz" "mumble frotz" "gronk snork
snerfle")

You can indicate where these parameter values are to be inserted into
the transcluded InfoBox content by embedded special 'substitution
markers' into that content, using $1 through $9, where the number
corresponds to the order of the parameters as passed in. Thus, if
InfoBox contains something like this:
Hello $1... $2 said: $3, $4 and $5
the above macro would render output like this:
Hello foo... bar said: baz, mumble frotz, and gronk snork snerfle

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios


FND

unread,
Mar 3, 2009, 1:07:04 PM3/3/09
to Tiddl...@googlegroups.com
> You can then 'transclude' (include and render) that
> tiddler's content into another tiddler, by using the TW core macro

FWIW, the community wiki also has some details on this:
http://tiddlywiki.org/wiki/Transclusion


-- F.

Pluszak

unread,
Mar 3, 2009, 2:01:19 PM3/3/09
to TiddlyWiki
Thank you guys.

Pluszak

unread,
Mar 3, 2009, 6:53:44 PM3/3/09
to TiddlyWiki
Hmm, though now that I tried it...
I'd need a parser function like #if from mediawiki to add some field
only on condition a parameter was given. Probably that should be done
using javascript (inline javascript plugin).
Is there a way to not only automaticaly assume $1 $2 $3
but some:

<<tiddler [[infobox]] with:$2="baz" $1="bar" $5="foo">>

If not what would you advise? Sending as first parameter something
like 0100101 to analyze it with javascript to know which parameters
will be used?

FND

unread,
Mar 4, 2009, 5:30:22 AM3/4/09
to Tiddl...@googlegroups.com
> I'd need a parser function like #if from mediawiki to add some field
> only on condition a parameter was given.

There's a ticket for that:
http://trac.tiddlywiki.org/ticket/890

For now, Eric has implemented this as a CoreTweak.


-- F.

Pluszak

unread,
Mar 4, 2009, 8:28:55 AM3/4/09
to TiddlyWiki
And as for giving the names to the parameters I send?

Eric Shulman

unread,
Mar 4, 2009, 9:35:06 AM3/4/09
to TiddlyWiki
> I'd need a parser function like #if from mediawiki to add some field
> only on condition a parameter was given. Probably that should be done
> using javascript (inline javascript plugin).
> Is there a way to not only automaticaly assume $1 $2 $3
> but some:
>
> <<tiddler [[infobox]] with:$2="baz" $1="bar"  $5="foo">>

The substitution mechanism provided by the <<tiddler ... with: ...>>
macro, while very handy for many purposes, does not really provide
support for conditional handling, named parameters, or other
'advanced' use-cases.

Fortunately, as you've noted, InlineJavascriptPlugin can be used very
effectively to dynamically-generate wiki-formatted content using fully
programmable logic and data... and it can include a bit of code that
'unpacks' the $1 through $9 params and convert them into an
associative array of name/value pairs, like this:

<script>
var args={};
if ('$1'!='$'+'1') { var t='$1'.split('='); args[t.shift()]=t.join
('='); }
if ('$2'!='$'+'2') { var t='$2'.split('='); args[t.shift()]=t.join
('='); }
if ('$3'!='$'+'3') { var t='$3'.split('='); args[t.shift()]=t.join
('='); }
if ('$4'!='$'+'4') { var t='$4'.split('='); args[t.shift()]=t.join
('='); }
if ('$5'!='$'+'5') { var t='$5'.split('='); args[t.shift()]=t.join
('='); }
if ('$6'!='$'+'6') { var t='$6'.split('='); args[t.shift()]=t.join
('='); }
if ('$7'!='$'+'7') { var t='$7'.split('='); args[t.shift()]=t.join
('='); }
if ('$8'!='$'+'8') { var t='$8'.split('='); args[t.shift()]=t.join
('='); }
if ('$9'!='$'+'9') { var t='$9'.split('='); args[t.shift()]=t.join
('='); }

// now you can do something with the args[]...
var out='';
if (args['debug']) for (var arg in args) out+=arg+' is: "'+args[arg]
+'"\n';
return out;
</script>

If you put this script in [[SomeTiddler]] and then invoke it from
another tiddler, like this:

<<tiddler SomeTiddler with: foo=bar "baz=mumble frotz" gronk
debug=true>>

You will get output like this:

foo is: "bar"
baz is: "mumble frotz"
gronk is: ""
debug is: "true"

Of course, this is just an example. For your purposes, once you have
easy reference to the name/value pairs, you can use whatever
conditional logic you need to generate the desired output string based
on the values passed in.
Reply all
Reply to author
Forward
0 new messages