1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-27 03:57:21 +00:00

Doc: How to concatenate text and variables (#2676)

* How to concatenate text and variables

* Adding warnings around anti-example code
This commit is contained in:
Marxsal 2017-01-09 04:39:50 -08:00 committed by Jeremy Ruston
parent 00669e87da
commit b152d0a727

View File

@ -0,0 +1,62 @@
created: 20160424150551727
modified: 20170101020235437
tags: Learning
title: Concatenating text and variables using macro substitution
type: text/vnd.tiddlywiki
It's a frequent use case in ~TiddlyWiki that you will want to put the results of variables together with various bits of strings of text. This process in some programming languages is often referred to as "concatenating" text.
You might, for instance want to set up a template for your customer database, where links will automatically refer to additional contact information about your customer. Inside your tiddler, you might try something like this:
|warning|k
|@@font-size:1.5em;&#9888;@@ Warning:<br/> Don't do it this way!| `[[Additional Info|<<currentTiddler>>-Contact]]` |
But that won't work. If you try this, the link will be interpreted very literally, and will attempt to take you to:
```
<<currentTiddler>>-Contact
```
The solution is to use a macro to put the rendered value of `<<currentTiddler>>` together with the bit of additional text, `-Contact`.
Create a macro either at the top of the tiddler or in a global tiddler (see [[Macros]]) like this:
```
\define linkup(link) [[Additional Info|$link$-Contact]]
```
You might be tempted to invoke the new macro like this:
|warning|k
|@@font-size:1.5em;&#9888;@@ Warning:<br/> Don't do it this way!| `<<linkup <<currentTiddler>> >>` |
But if you do, you will find that `<<currentTiddler>>` doesn't get rendered, but instead gets passed literally.
Instead, you could use the [[MacroCallWidget]] widget, like this:
```
<$macrocall $name="linkup" link=<<currentTiddler>> />
```
In this case, we passed the value of a variable directly to our macro. This is often a general way to go about this task. If you wanted to create more links based on other variables you could re-use the macro for each situation.
If, as in this case, the only variable you are using is `currentVariable` then you could write a simple macro, like this:
```
\define linkup() [[Additional Info|$(currentTiddler)$-Contact]]
```
Notice that in this case we don't pass an argument. Instead, we reference the variable using the special syntax `$(variable)$`. Since we don't pass an argument, we can invoke it without the `<$macrocall>` widget more simply, like this:
```
<<linkup>>
```
<style>
.warning code {background-color:#ffff80}
table.warning {background-color:#ffff80;}
</style>