kainjow / Mustache

Mustache text templates for modern C++
Boost Software License 1.0
357 stars 49 forks source link

Request method to initialize with alternate delimiters #56

Closed derekbsnider closed 2 years ago

derekbsnider commented 3 years ago

I think it would be particularly useful if there was a way to set what the tag delimiters were on the mustache object directly.

Example:

mustache tmpl{"Hello <%%what%%>!"};

tmpl.set_delimiters("<%% %%>");
std::cout << tmpl.render({"what", "World"}) << std::endl;
KitsuneRal commented 3 years ago

Prepending <=%% %%=> to the template would achieve the same result, wouldn't it?

derekbsnider commented 3 years ago

Prepending <=%% %%=> to the template would achieve the same result, wouldn't it?

Yes, it would, but requires manipulating the template, which seems less clean than having access to a method to set it.

KitsuneRal commented 3 years ago

Is it "clean" to describe a template without specifying a delimiter used in it, in the first place? I understand there might be a common convention you might want to apply across a class of templates (I do have exactly that case) but I prefer to prepend the template with the intended delimiters as soon as possible to make it complete.

derekbsnider commented 3 years ago

I personally wouldn't consider it "clean" for the only control over the template class to be via the template data itself. Many template delimiter conventions predate the Mustache convention of curly braces, and thus it would be nice to be able to specify a different default set without manipulating the input data.

KitsuneRal commented 3 years ago

Well, Mustache is not just double curly braces, the list of conventions is much longer... Even after redefining the delimiter the template language remains Mustache. But I remembered that there's already set_custom_escape() anyway that also changes language metadata, and delimiter assignment falls in that category as well.

derekbsnider commented 3 years ago

How would one change the delimiter using set_custom_escape()?

KitsuneRal commented 3 years ago

One cannot; set_custom_escape() is for a different thing.

derekbsnider commented 3 years ago

Also have a case where being able to set the default delimiter outside of the template would be more important -- if you need to apply the template line by line, and do not have access to the whole document (i.e. inside of a stream filter).

kainjow commented 2 years ago

Seems low use demand currently. Feel free to submit a PR with a test, and I'll consider it.