tests-always-included / mo

Mustache templates in pure bash
Other
563 stars 67 forks source link

Feature (or Docs): Provide a way to render a literal `{{text}}` #72

Closed eherot closed 5 months ago

eherot commented 5 months ago

This seems to be possible with Ruby mustache templates by overriding the default delimiter (e.g. hello {{=<% %>=}} {{world}}<%={{}}=%>) but not with this tool. Or if it is possible I can't find it explained in the docs.

fidian commented 5 months ago

The Mustache spec does include changing the delimiters, but they want to have that phased out according to what I read when I was researching that feature. Even if you did change the default to <% and %>, there is no convenient way to include <% and %> in your template.

What I do is define two constants.

BRACE_OPEN="{{"
BRACE_CLOSE="}}"

Then you can use {{BRACE_OPEN}}text{{BRACE_CLOSE}} to get what you want. See `demo/writing-out-braces'.

Another method is to replace another string, such as <% and %> using various tools. For instance, you can use sed.

echo "<%test%>" | sed -e 's/<%/{{/g' -e 's/%>/}}/g'

A third would be to use a function. This is a bit more complex because you need to source mo. Here are two different ways to do the same thing.

#!/usr/bin/env bash
. `which mo`
wrapper() {
    echo -n "{{$(cat)}}"
}
echo "{{#wrapper}}test{{/wrapper}}" | mo
quote() {
    echo -n "{{$1}}"
}
echo "{{quote 'test'}}" | mo --allow-function-arguments
eherot commented 5 months ago

Thanks for the suggestions. These all do kind of work as workarounds, though for reasons of cleanliness in my use case it would be nice if there were some method that could be embedded entirely within the template

fidian commented 5 months ago

I get what you are saying and how it would be nice to have a way to include literals inside of a Mustache template. Not all Mustache parsers support the optional delimiter changing and all of those will have a similar problem. It would be extremely useful to be able to mark some text as "not parseable", so it could be used as user input (or other uses).

Closing this issue because it appears that it's as resolved as it's going to get.