1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-17 15:24:50 +00:00
TiddlyWiki5/editions/tw5.com/tiddlers/widgets/SetWidget.tid

70 lines
2.4 KiB
Plaintext
Raw Normal View History

2014-09-10 23:06:19 +00:00
caption: set
created: 20131115182700000
modified: 20161017122456014
tags: Widgets
title: SetWidget
type: text/vnd.tiddlywiki
! Introduction
2015-05-02 08:56:24 +00:00
The set variable widget assigns a value to a specified [[variable|Variables]]. The new value of the variable is available to the content within the set variable widget.
! Content and Attributes
The content of the `<$set>` widget is the scope for the value assigned to the variable.
|!Attribute |!Description |
2014-11-26 11:22:20 +00:00
|name |The name of the variable to assign (defaults to "currentTiddler") |
|value |The value to assign to the variable if the filter is missing or not empty |
|filter |An optional filter to be evaluated and assigned to the variable (see below) |
|select |<<.from-version "5.1.14">> An optional zero-based index of the item to return from the filter output (see below) |
2014-11-26 11:22:20 +00:00
|emptyValue |The value to assign to the variable if the filter is present and evaluates to an empty list (see below) |
!! Simple Variable Assignment
The simplest way of using set variable widget assigns a string to a variable. The following example assigns a literal string
```
<$set name="myVariable" value="Some text">
<$text text=<<myVariable>>/>
</$set>
```
Both the name and value attributes can be transcluded. For example:
```
<$set name=<<anotherVariable>> value={{template!!text}}>
<$text text=<<myVariable>>/>
</$set>
```
!! Conditional Variable Assignment
This form of the set variable widget chooses one of two specified values according to whether a filter evaluates to an empty list. Here's an example that sets a variable according to whether the current tiddler is called "myMagicTitle":
```
<$set name="myVariable" filter="[all[current]field:title[myMagicTitle]]" value="It's magic" emptyValue="It's not magic">
<$text text=<<myVariable>>/>
</$set>
```
!! Filtered List Variable Assignment
This form of the set variable widget evaluates the filter and assigns the result to the variable as a space-separated list (using double square brackets for titles containing spaces).
```
<$set name="myVariable" filter="[tag[HelloThere]]">
2014-11-26 11:22:20 +00:00
<$text text=<<myVariable>>/>
</$set>
```
!! Filtered Item Variable Assignment
<<.from-version "5.1.14">> This form of the set variable widget evaluates the filter and assigns the specified result to the variable as a single item (ie, not using double square brackets for titles containing spaces).
```
<$set name="myVariable" filter="[tag[HelloThere]]" select="0">
<$text text=<<myVariable>>/>
</$set>
```