Adding templater opening/closing tags to inline code

I am adding my notes related to the Templater plugin into Obsidian. I want to literally type the opening and closing Templater tags (<% and %>), but when I type `<%`, it gets interpreted as an opening tag and ruins the formatting of everything that follows (as it now thinks I want to be in the Templater engine).

I’ve tried using HTML entities for the percent sign, but that seems to get typed literally (e.g., whether I tried `<\0x25`, `<&#x25;`, etc.) rather than replaced with %. I’ve tried escaping the percent sign, which prevents the Templater engine from kicking in but also renders the backslash (e.g., I see `<\%` in the note). I’ve searched through the Templater docs as well as this forum, but couldn’t find anything similar (most threads I found were about escaping the backtick to render it within backticks, rather than the percent sign, which outside of Templater, renders just fine).

I can confirm it’s not an issue with remaining on the line while in live preview (i.e., outside of inline code, the backslash will show when you’re on the line in live preview but not when your cursor is elsewhere).

Preparing to run before presenting a very hacky solution, have you tried the following?

<code><<span style="display: none"></span>%+ tR = "Completed" %></code>

It actually works, and it’s even possible to copy the entire templater string and it’ll get pasted without the nasty stuff…

As a variant over that theme, you could possibly do a special class for the code element, this is untested but something like:


code.T::before {
  content: "<%";
}

And then use:

<code class="T">+ tR = "nicer?" %></code>

(You could potentially use ::after for the ending %> also, if you want to)

1 Like

That works great. Thanks @holroy!

The ending %> wasn’t a problem since as long as I never enter the templater engine, %> means nothing special to Obsidian. Thanks for the suggestion above!

I tried to edit my post but it’s been too long since I posted it.

It seems I spoke a bit too soon, as I had tried this solution without putting it in backticks and it worked fine. However, when I tried backticks, it would render the span HTML. When I tried without, the ending </code> tag was getting rendered and taking it out made no difference (but the <% showed as plain text rather than monospace). Your hint of using display: none got me thinking of the zero-width space. I copy-and-pasted that from the character viewer between the < and %, and that worked perfectly.

Doss that still allow for the code to copied though? I thought about suggesting that, but didn’t as I figured it would then be copied if you copied your example code into a proper template.

I’m not quite sure I understand what your issue with code vs backticks was in the code I suggested, or what didn’t display as expected.

That’s a good point: copying-and-pasting is definitely something that anyone reading this in the future would want to consider. In my case, I was making notes on handling white space with Templater so I only needed to type <%- and <%_.

To clarify what I was seeing, I copied your code block (<code><<span style="display: none"></span>%+ tR = "Completed" %></code>) into a note and this gets rendered out:

<%+ tR = "Completed" %></code>

I reduced that down to <code><<span style="display: none"></span>%, saw that it rendered out <%, and posted my answer that this worked. However, I then went to copy it into my real notes and noticed it was not rendering as code. I surrounded it in backticks and got the whole thing output literally (i.e., <code><<span style="display: none"></span>% rendered to the page). I thought I might be missing a setting about interpreting HTML, but the opening code tag was replaced (even if it didn’t seem to do anything).

I literally copy-pasted this, and get this output:
image

No extra </code>, and nicely formatted as code in a monospaced font. Strange stuff is happening somewhere along the way in your case. Bummer.

Ah… You’re using live preview, where Code mirror is doing some of its magic… I almost never use live preview, just reading view. I only use live preview when someone needs something specific for that mode. So, back to the drawing board it is, if live preview is a pre-requisite of yours (which it seems to be).

Is live preview mode == Source mode being unchecked?

I’ve used it because it’s the default, but I’ve only been using Obsidian steadily for about the past two weeks (I used it on-and-off before that, but never as invested as now) so I haven’t really established habits yet. It’s no prerequisite, though if the above about source mode is true, then I still have the same experience as before (i.e., typing just the opening Templater tag, <%, starts the Templater engine).

Anyway, I appreciate your discussion on this and still consider your first reply a solution, even if I had to deviate from it. Thanks again, both here and for your many other replies on this forum.

Yes, and you’ll notice that your view will render stuff when your cursor is not within that block. So stuff like queries will be executed, tables will render, and it’ll try to mimic the reading view as good as it can, but you’re still in an editing mode.

Personally, I don’t like it, as I don’t like the shift in viewing experience when I traverse my document, but some do like it since it renders stuff without leaving edit mode, and it kind of makes Obsidian feel more like a WYSIWYG editor.

I tested this now, and it doesn’t allow for the template code to be used as a template since the no-width-space is indeed copied as part of the text.

I tested this, as well, and while it shows the text “correctly” the formatting is a little of, and it doesn’t allow for copying of the ::before and ::after content. Bummer.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.