[tw5] Render a custom type as text/plain

Hi,

I registered a new type on my TW, text/x-fountain, as per https://groups.google.com/g/tiddlywiki/c/l_uxgh_tsDU. All good; it shows up on the type selector.

But I’d like it rendered as if it was a text/plain file, not as a TW 5 tiddler.

Is there a simple way to achieve this?

Thanks,
Gabriel

You want to use the TWCore’s “cascade” mechanism (see https://tiddlywiki.com/#Cascades)

  • Create a tiddler (e.g., “$:/config/ViewTemplateBodyFilters/x-fountain”), containing:
    [type[text/x-fountain]then[$:/core/ui/ViewTemplate/body/rendered-plain-text]]
  • Tag it with $:/tags/ViewTemplateBodyFilter
  • Add a field named list-before with no field value

Notes:

  • Your “cascade” tiddler can have any name. The name suggested above is consistent with the existing TWCore “ViewTemplateBodyFilters” shadow definitions.
  • The tiddler content is evaluated by the TWCore cascade mechanism, and basically says: "If the tiddler’s type field is equal to “text/x-fountain”, then display the tiddlers body using the “$:/core/ui/ViewTemplate/body/rendered-plain-text” template
  • The $:/tags/ViewTemplateBodyFilter tag adds your definition to the default ViewTemplateBodyFilters cascade
  • The list-before field ensures that your custom cascade is evaluated first when the cascade is processed by the TWCore

enjoy,
-e

P.S. You might want to visit https://talk.tiddlywiki.org/, which is a “Discourse” server that is now the TiddlyWiki community’s primary discussion/Q+A area.

1 Like

Addendum:

Instead of
[type[text/x-fountain]then[$:/core/ui/ViewTemplate/body/rendered-plain-text]]

your cascade definition should be:
[type[text/x-fountain]then[$:/core/ui/ViewTemplate/body/code]]

The difference is that the first definition will render embedded wikitext syntax,
while the second definition simply displays the content as plain text without any other processing.

-e

1 Like

Amazing Eric! Worked perfectly. And thanks for the link and explanation! I’ll use the Discourse server for these type of questions going forward.
Again, 1000 thanks!
Cheers,
Gabriel

Interestingly, when I transclude the node, it’s still rendered as a wiki tiddler, not as code. I wonder, Eric, if, from the top of your head, you’d know how to work around that?
Thanks again, Gabriel

Instead of using simple transclusion (i.e., {{TiddlerTitle}}) use the $codeblock widget, like this:
<$codeblock code={{TiddlerTitle}}/>

Wonderful :slight_smile: Thanks a lot!

The editor preview is also rendering it as a wiki tiddler, not as code. I can live with that, but I was hoping there’d be a fundamental “and here is how you render this type of tiddler” way.
Still, it’s incredible how flexible TW is!

Cheers,
Gabriel

I bet it’s possible to have the preview pane give you a preview according to the template you need.

I’ve just discovered that I can get such an effect within dynamic tables (which can toggle their expanded rows into edit mode). See this thread: Hacking tbl-expand (with templates) in shiraz dynamic tables - #7 by Springer

I suspect part of the explanation has to do with how the preview pane works for “draft of” tiddlers, if/when you haven’t specified a different Edit template (via the cascade for edit mode)

There are various plugins that offer editing interfaces that edit without setting up a “draft of” tiddler, and these may help you (or anyone working regularly with templates other than the default one) to see a live preview of your edits in progress as they would appear in their “final” form.

(Then again, if you’re looking for a completely non-wikified mode, is the preview pane doing anything useful for you at all? Wouldn’t you just toggle that preview pane off for these tiddlers?)

Just a quick follow-up: My memory was wrong – I’m actually seeing the preview pane applying alternate view templates correctly in nearly all cases. The exceptions all turned out to involve cascade-conditions that referenced things like how the title begins (which are of course disrupted by “Draft of…” prefix).

So, I’m baffled as to why the template specified in your ViewTemplate cascade is not working properly in the preview pane while editing.

Yes, very strange. I tried it out with a fresh wiki to see if I had something interfering, but I got the same result. Thanks for chipping in : )