Is it possible to remove duplicate wikitext code when showing both codeblocks and the result of rendering them?

With a few tweaks to how your functions are defined, this definitely looks possible, using just one parameterized template tiddler.

First, create that template tiddler. Give it a title like ShowExample and add its content:

\whitespace trim
\parameters (definitions:"", demoCode:"")

<$codeblock code=```$(definitions)$

$(demoCode)$```
/>

renders as:

<pre>
<$wikify text=```$(definitions)$

$(demoCode)$``` name="result"><$text text=<<result>>/></$wikify>
</pre>

Next, in your New Tiddler, wrap the function definition up like so:

\procedure definitions()
\function multiply-by-two(number)
[<number>multiply[2]]
\end multiply-by-two
\end definitions

Explanation: Macro and and procedure definitions without parameters behave like ordinary variable definitions, so wrapping your function definition in \procedure definitions() ... \end definitions makes the whole function definition available as a string variable. Wrapping it up like this also has the advantage that each code example can contain multiple definitions.

To render everything, transclude ShowExample in New Tiddler:

\procedure definitions()
\function multiply-by-two(number)
[<number>multiply[2]]
\end multiply-by-two
\end definitions

<$transclude
    $tiddler="ShowExample"
    definitions=<<definitions>>
    demoCode="<<multiply-by-two 4>>"
  />

Using a WikifyWidget is perhaps not ideal in terms of performance, but I think this is necessary to evaluate the function definition pragma in the template tiddler.

Hope that solved your problem or gave you some ideas.

1 Like