Skip to content

Commit 6213a38

Browse files
committed
Add default exmple to txp:variable tag
1 parent 4ee7dd8 commit 6213a38

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

tags/variable.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ There are two parts to making this work. First a variable is created that stores
164164
Note: A Textpattern tag, used as an attribute (a parsed attribute), must be surrounded with single quotes.
165165
{: .alert-block .information}
166166

167-
Alternatively, to avoid complicated quote escaping inside the `value` attribute, you can use the tag as a container:
167+
Alternatively, to avoid complicated [quote escaping](/tags/learning/#attribute-value-escaping) inside the `value` attribute, you can use the tag as a container:
168168

169169
~~~ html
170170
<txp:variable name="foo"><txp:permlink /></txp:variable>
@@ -240,6 +240,35 @@ Using the above code in a page template would permit you to use example.org/arti
240240

241241
Other tags used: [article](/tags/article), [else](/tags/else), [if_variable](/tags/if_variable), [page_url](/tags/page_url).
242242

243+
### Example 8: Using default values
244+
245+
You may want to make up a variable that contains the value of some dynamic content and display a default value if that content is empty. Conceptually, this seems easy but you need to bear in mind that the primary purpose of the `<txp:variable>` when used with a `value` is to _store_ the content and when used without a `value` it will _display_ any stored content.
246+
247+
To use default values you must therefore only use them when you intend to display what is stored and **not** when you assign the value to the variable. Viz:
248+
249+
~~~ html
250+
<!-- Variable assignment: displays nothing -->
251+
<txp:variable name="primary_section" value="about" />
252+
253+
<!-- Variable display: default overrides the value attribute (it's ignored)
254+
and the tag will display _articles_ -->
255+
<txp:variable name="primary_section" value="about" default="articles" />
256+
~~~
257+
258+
This demonstrates it even more clearly:
259+
260+
~~~ html
261+
<!-- Variable has a value so is output by the 2nd tag
262+
Result: about -->
263+
<txp:variable name="primary_section" value="about" />
264+
<txp:variable name="primary_section" default="articles" />
265+
266+
<!-- Variable has no value so default value is output by the 2nd tag
267+
Result: articles -->
268+
<txp:variable name="primary_section" value="" />
269+
<txp:variable name="primary_section" default="articles" />
270+
~~~
271+
243272
## Genealogy
244273

245274
### Version 4.7.2

0 commit comments

Comments
 (0)