diff --git a/core/modules/widgets/setvariable.js b/core/modules/widgets/setvariable.js index 6dbdead5b..e319a61e3 100755 --- a/core/modules/widgets/setvariable.js +++ b/core/modules/widgets/setvariable.js @@ -39,9 +39,21 @@ Compute the internal state of the widget SetWidget.prototype.execute = function() { // Get our parameters this.setName = this.getAttribute("name","currentTiddler"); + this.setFilter = this.getAttribute("filter"); this.setValue = this.getAttribute("value"); + this.setEmptyValue = this.getAttribute("emptyValue"); // Set context variable - this.setVariable(this.setName,this.setValue,this.parseTreeNode.params); + var value = this.setValue; + if(this.setFilter) { + var results = this.wiki.filterTiddlers(this.setFilter,this); + if(!this.setValue) { + value = $tw.utils.stringifyList(results); + } + if(results.length === 0 && this.setEmptyValue !== undefined) { + value = this.setEmptyValue; + } + } + this.setVariable(this.setName,value,this.parseTreeNode.params); // Construct the child widgets this.makeChildWidgets(); }; @@ -51,7 +63,7 @@ Selectively refreshes the widget if needed. Returns true if the widget or any of */ SetWidget.prototype.refresh = function(changedTiddlers) { var changedAttributes = this.computeAttributes(); - if(changedAttributes.name || changedAttributes.value) { + if(changedAttributes.name || changedAttributes.filter || changedAttributes.value || changedAttributes.emptyValue) { this.refreshSelf(); return true; } else { diff --git a/editions/prerelease/tiddlers/SetWidget.tid b/editions/prerelease/tiddlers/SetWidget.tid new file mode 100644 index 000000000..fc4c39dac --- /dev/null +++ b/editions/prerelease/tiddlers/SetWidget.tid @@ -0,0 +1,57 @@ +title: SetWidget +created: 201311151827 +modified: 20141113212520422 +tags: Widgets +caption: set + +! Introduction + +The set variable widget assigns a value to a specified [[variable|WidgetVariables]]. The new value of the variable is availale 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 | +|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) | +|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"> +<> + +``` + +Both the name and value attributes can be transcluded. For example: + +``` +<$set name=<> value={{template!!text}}> +<> + +``` + +!! 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"> +<> + +``` + +!! 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[Introduction]]"> +<> + +```