-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
When substituting a $(varname)$
placeholder using the substitute[]
operator, where varname
is a function, the function does not have access to variables set in the filter run (currentTiddler
, index,
etc). Substitutions for filtered expressions work as expected.
This is discrepancy is likely because the wiki.getSubstitutedText method predates functions and just calls widget.getVariable
to resolve variable references for substitution:
TiddlyWiki5/core/modules/wiki.js
Lines 1175 to 1178 in 8ab4306
// Substitute any variable references with their values | |
return output.replace(/\$\((.+?)\)\$/g, function(match,varname) { | |
return widget.getVariable(varname,{defaultValue: ""}) | |
}); |
Instead we probably want to do something along these lines:
// Substitute any variable references with their values
return output.replace(/\$\((.+?)\)\$/g, function(match,varname) {
return widget.evaluateVariable(name, defaultValue: ""})[0];
});
See widget.evaluateVariable
:
TiddlyWiki5/core/modules/widgets/widget.js
Lines 856 to 865 in 8ab4306
Widget.evaluateVariable = function(widget,name,options) { | |
var result; | |
if(widget.getVariableInfo) { | |
var variableInfo = widget.getVariableInfo(name,options); | |
result = variableInfo.resultList || [variableInfo.text]; | |
} else { | |
result = [widget.getVariable(name)]; | |
} | |
return result; | |
}; |
Questions and concerns:
I am pressed for time at present and have not had the time to consider:
- Whether we need to take into account variable/function parameters.
- Do we support parameters for variables being substituted. e.g.
$(varname myparam)$
? - We do support string parameters for variables in filters:
TiddlyWiki5/core/modules/filters.js
Lines 270 to 271 in 8ab4306
var varTree = $tw.utils.parseFilterVariable(operand.text); operand.value = widgetClass.evaluateVariable(widget,varTree.name,{params: varTree.params, source: source})[0] || "";
- Do we support parameters for variables being substituted. e.g.
- What would be the possible ramifications of adapting
widget.getVariable
to properly evaluate functions? - Are there any repercussions for substituted attributes because of this change?
To reproduce:
\function getIndex() [<index>add[1]]
\procedure template-with-var() $(getIndex)$
\procedure template-with-filteredexpression() ${ [<getIndex>] }$
\function test-with-substitute-variable()
[[abc]split[]] :map[<template-with-var>substitute[]] :and[join[ / ]]
\end
\function test-with-substitute-filteredexpression()
[[abc]split[]] :map[<template-with-filteredexpression>substitute[]] :and[join[ / ]]
\end
\function test-with-function()
[[abc]split[]] :map[function[getIndex]substitute[]] :and[join[ / ]]
\end
!! Test with substitute (variable)
<<test-with-substitute-variable>>
!! Test with substitute (filtered expression):
<<test-with-substitute-filteredexpression>>
!! Test with function:
<<test-with-function>>
All three test functions should return the same output: 1 / 2 / 3
. but test-with-substitute-variable
returns 1 / 1 / 1
because the value of index
in getIndex
is an empty string, i.e. the variable is undefined. (TiddlyWiki v5.3.7)
Metadata
Metadata
Assignees
Labels
Type
Projects
Status