Requesting help to create a custom filter

There is an tiddler a, and the value of the af field in a is 1234. Additionally, there is an tiddler b, and the value of the bf field in b is addprefix[5678]. How can I obtain the result of the filter [[1234]addprefix[5678]]?
I tried using:

<$vars a={{{[[a]get[af]]}}} b={{{[[b]get[bf]]}}}>
{{{[<a><b>]}}}
</$vars>

But it can not work. I really need everyone’s help. Thank you all!

First thing… it’s a little bit strange to use filter syntax (e.g., addprefix[5678]) as the value of a field.

But… if you are really set on that strategy, there’s a few things you need to adjust:

  • The bf field value needs the outer enclosing square brackets (i.e., [addprefix[5678]]) so it forms a complete filter syntax, not just a filter operator fragment.
  • Then, to apply the filter syntax retrieved from the bf field, you need to use the subfilter operator, like this:
<$vars a={{{ [[a]get[af]] }}} b={{{ [[b]get[bf]] }}}>
{{{[<a>subfilter<b>]}}}
</$vars>

-e

1 Like

If we consider wikitext, and particularly filter syntax a programming language, having situations when a variable holds a string value that is valid code in that language is possible (yet it usually adds complexity to the codebase and might have security implications like code execution if any input is just accepted as trusted), this is reflection. Sometimes I used to do this in Python, so I’ll link the Wikipedia example for reference: Reflective programming - Wikipedia

I tried to approach OP’s question the same way but of course I can’t compete with your blitz skills :laughing:

Here’s my code so far:

\function .eval(str)
[<str>]
\end

{{{[[1234]addprefix[5678]]}}}

<$wikify name="myfilterexp" text="[{{a!!af}}]{{b!!bf}}">

<<myfilterexp>>

---

{{{[.eval<myfilterexp>]}}}

</$wikify>

I have solved the easier first half - I’ve got the inner part of the filter in a variable.

Now I’m struggling to tell TiddlyWiki to treat it like a filter expression. As you can see, I have tried to “cheat” by using a function, but it does not work. How to do it the right way?

1 Like

This works:

{{{[[1234]addprefix[5678]]}}}
<$wikify name="myfilterexp" text="{{a!!af}} +[{{b!!bf}}]">

<<myfilterexp>>
----
{{{ [subfilter<myfilterexp>] }}}
</$wikify>

-e

1 Like

Great.

So a simpler (not involving other tiddler “dependencies”) example showcasing reflection would be:

{{{[[1234]addprefix[5678]]}}}
---
<$set name="myfilterexp" value="[[1234]addprefix[5678]]">
{{{ [subfilter<myfilterexp>] }}}
</$set>