Skip to content

Commit

Permalink
improve setwidget examples and add a link to and from enlist operator (
Browse files Browse the repository at this point in the history
  • Loading branch information
pmario authored May 25, 2021
1 parent ca96f7f commit f342fdc
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
5 changes: 2 additions & 3 deletions editions/tw5.com/tiddlers/filters/enlist Operator.tid
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
caption: enlist
created: 20170221144241063
modified: 20190610165246154
modified: 20210505100241872
op-input: ignored
op-neg-input: a [[selection of titles|Title Selection]]
op-neg-output: those input titles that are <<.em not>> listed in <<.place L>>
Expand All @@ -14,13 +14,12 @@ tags: [[Filter Operators]] [[Field Operators]] [[Selection Constructors]] [[Nega
title: enlist Operator
type: text/vnd.tiddlywiki

<<.tip " Literal filter operands cannot contain square brackets but you can work around the issue by using a variable:">>
<<.tip """Literal filter operands cannot contain square brackets but you can work around the issue by using a variable. ''Learn more at:'' [[SetWidget]] documentation under the heading "Filtered List Variable Assignment" """>>

```
<$set name="myList" value="first [[second with a space]] third">
<$list filter="[enlist<myList>]">
...
```


<<.operator-examples "enlist">>
8 changes: 8 additions & 0 deletions editions/tw5.com/tiddlers/widgets/$__docs_anyField.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
created: 20210505094244703
field: list
modified: 20210505094434740
tags:
title: $:/docs/anyField
type: text/vnd.tiddlywiki

This tiddler is needed for the last [[SetWidget]] example
97 changes: 62 additions & 35 deletions editions/tw5.com/tiddlers/widgets/SetWidget.tid
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
caption: set
created: 20131115182700000
modified: 20180315165405442
modified: 20210505095640228
tags: Widgets
title: SetWidget
type: text/vnd.tiddlywiki
Expand All @@ -15,79 +15,106 @@ The content of the `<$set>` widget is the scope for the value assigned to the va

|!Attribute |!Description |
|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 |
|value |The value to assign to the variable if the <<.attr filter>> attribute is missing or not empty |
|tiddler |<<.from-version "5.1.15">> Optional title of the tiddler from which to read the value |
|subtiddler |<<.from-version "5.1.16">> Optionally specifies the title of a subtiddler within a plugin tiddler identified by the ''tiddler'' attribute |
|field |<<.from-version "5.1.15">> Optional field of the tiddler from which to read the value (only used if ''tiddler'' is used) |
|index |<<.from-version "5.1.15">> Optional index of the tiddler from which to read the value (only used if ''tiddler'' is used) |
|filter |An optional filter to be evaluated and assigned to the variable (see below) |
|filter |An optional filter to be evaluated and assigned to the variable (see "Filtered List Variable Assignment" below). The variable can be used with the <<.olink enlist>> operator |
|select |<<.from-version "5.1.14">> An optional zero-based index of the item to return from the filter output (see below) |
|emptyValue |The value to assign to the variable if the specified value is missing or empty (see below) |

<<.tip """If the value of your variable is enclosed in double square brackets this might indicate that you are returning a list of values from the filter. To use a single title from the filter output without the double square brackets see ''Filtered Item Variable Assignment'' below.""">>

! Examples

!! 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">
<<<

<$macrocall $name='wikitext-example-without-html'
src='<$set name="myVariable" value="Some text">
<$text text=<<myVariable>>/>
</$set>
```
</$set>' />

<<<

Both the name and value attributes can be transcluded. For example:

```
<$set name=<<anotherVariable>> value={{template!!text}}>
<$text text=<<myVariable>>/>
</$set>
```
<<<

<$macrocall $name='wikitext-example-without-html'
src='<$set name=anotherVariable value="myVariable">
<$set name=<<anotherVariable>> value={{$:/language/DefaultNewTiddlerTitle}}>
<$text text=<<myVariable>>/>
</$set>
</$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">
<<<

<$macrocall $name='wikitext-example-without-html'
src="""<$set name="myVariable" filter="[all[current]field:title[myMagicTitle]]" value="It's magic" emptyValue="It's not magic">
<$text text=<<myVariable>>/>
</$set>
```
</$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]]">
<<<

<$macrocall $name='wikitext-example-without-html'
src='<$set name="myVariable" filter="[tag[HelloThere]]">
<$text text=<<myVariable>>/>
</$set>
```
</$set>' />

!! Filtered Item Variable Assignment
<<<

!! Filtered Item Variable Assignment Single Element

<<.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">
<<<

<$macrocall $name='wikitext-example-without-html'
src='<$set name="myVariable" filter="[tag[HelloThere]]" select="0">
<$text text=<<myVariable>>/>
</$set>
```
</$set>'/>

<<<

!! Transcluded Variable Assignment

<<.from-version "5.1.15">> This form of the set variable widget obtains the value to assign to the variable from a value in a tiddler field or index. For example:

```
<$set name="myVariable" tiddler="HelloThere" field="text">
<<<

<$macrocall $name='wikitext-example-without-html'
src='<$set name="myVariable" tiddler="HelloThere" field="list">
<$text text=<<myVariable>>/>
</$set>
```
</$set>'/>

The example above could also be written as `<$set name="myVariable" value={{HelloThere!!text}}>`. The advantage of using the ''tiddler'' attribute is that the tiddler title and field or index can themselves be computed. For example:
<<<

```
<$set name="myVariable" tiddler=<<myTiddler>> field={{$:/currentField}}>
<$text text=<<myVariable>>/>
</$set>
```
The example above could also be written as `<$set name="myVariable" value={{HelloThere!!list}}>`. The advantage of using the ''tiddler'' attribute is that the tiddler title and field or index can themselves be computed. For example:

<<<

<$macrocall $name='wikitext-example-without-html'
src='<$set name="myTiddler" value="HelloThere">
<$set name="myVariable" tiddler=<<myTiddler>> field={{$:/docs/anyField!!field}}>
<$text text=<<myVariable>>/>
</$set>
</$set>'/>

<<<

0 comments on commit f342fdc

Please sign in to comment.