Display source code of code tiddlers

I’ve just read about $:/core/ui/ViewTemplate/body/code and have tied this in a tiddler:

<$tiddler tiddler={{!!target}}><$transclude tiddler="$:/core/ui/ViewTemplate/body/code"/></$tiddler>

This will display the code of the tiddler identified by the //target// field. Fine. Yet I have two questions:

  • if the target is a tw5 tiddler, its code is shown but without syntax coloration. Is there a way to have this feature enabled?
  • if the target tiddler had a //code// tag, could there be a way that, by itself, this tiddler would be displayed as code and yet interpreted as usual when transcluded? The new 5.2.1 mechanism of cascade would surely be of help.

I have tried creating a $:/config/EditTemplateBodyFilters/code tiddler with the $:/tags/EditTemplateBodyFilter tag and the following text:

[[$:/core/ui/EditTemplate/body/code]]

Then I edited the core tiddler $:/tags/EditTemplateBodyFilter to get this in its list field:

$:/config/EditTemplateBodyFilters/canonical-uri $:/config/EditTemplateBodyFilters/default $:/config/EditTemplateBodyFilters/code

But it’s not enough, even if my source code tiddler is tagged code (just a wild trying, no objective reason to believe it would work).

Looking at the core source code, I saw some stuff in the //templates// folder, but it’s a bit confusing, especially with templates/javascript-tiddler.tid and its <scriptwhich is not closed before the </script>

Thank you for your help!

Rather than edit the list field tag your tiddler(s) with $:/tags/EditTemplateBodyFilter rather than editing the list field. This will add it to the list field if the tagged items are reordered (only)

There’s not much in $:/core/ui/ViewTemplate/body/code, I would generally use the <$codeblock> widget directly:

<$codeblock code={{{ [<currentTiddler>get[text]] }}} language={{{ [<currentTiddler>get[type]else[text/vnd.tiddlywiki]] }}}/>

There should be syntax highlighting if you have the Highlight plugin installed. Note that many content types (notably wikitext) used in TiddlyWiki are not recognised by the plugin, and so won’t have syntax highlighting applied.

We already support setting the code-body field to yes to force a tiddler to be displayed in the story river as code but transcluded as normal. Is there a reason you’d prefer a tag?

In any event, the following filter would achieve what you want:

[tag[code]then[$:/core/ui/ViewTemplate/body/code]]

It would need to be put in a tiddler tagged $:/tags/ViewTemplateBodyFilter with a blank list-before field to force it to the top of the cascade.

As for the color syntax, it’s OK for js so good. this is no more a problem.

What I want is a tiddler tagged code (or with a show-as-code field set to yes) to be displayed as code (with syntax in color if available). Why a tag: because only some tiddlers would be concerned. I just want a way to mark them as such.

But I’m not able to show them as code yet. I’m not sure I understand what i was told to do. I’ve created a “code” tiddler whose text is [tag[code]then[[$:/core/ui/ViewTemplate/body/code] without type and with a code-body field set to yes. it does not appear as code. and it does not help a tiddler tagged code to be displayed as code.

A search for code-body yields nothing on tiddlywiki .com. Is there any documentation? I couldn’t get it to change the appearance of a tiddler, but maybe the field needs particular contents?

You need to create a tiddler, any title, and put [tag[code]then[[$:/core/ui/ViewTemplate/body/code] in the text. Tag the tiddler $:/tags/ViewTemplateBodyFilter. Add a field list-before but leave it blank.

Now any tiddler tagged as code will appear as code.

@Mark_S and @jeremyruston Than you, it’s now working. I did not put the list-before field where it was needed.

what should be the type of the tiddler with this list-before tiddler? Simple text? I dislike tiddler without type.

Apologies for the confusion: I had forgotten that the code-body support was accidentally missed off v5.2.1, but is part of the prerelease for v5.2.2.