在NodeJS中模板化模板

在NodeJS中模板化模板

本文介绍了在Mustache模板中转义双括号{{...}}。 (在NodeJS中模板化模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模板化模板,如下所示:

I'm trying to template a template, like below:

{{{
{
  "name" : "{{name}}",
  "description" : "{{description}}"
}
}}}

{{{debug this}}}
<h1>{{name}}</h1>

我想要三重括号留下来,但是用传入的JSON代替双括号。任何人都知道在没有编写后期处理JS代码的情况下执行此操作的最佳方法,如果没有,是否有适合此类场景的nodeJS模板引擎?

Where I want to triple brackets to stay, but double brackets to be replaced with the JSON passed in. Anyone know the best way to do this without writing post-process JS code, and if not, is there a good nodeJS template engine for this type of scenario?

推荐答案

您可以将分隔符切换为不会与三重胡须相冲突的内容,例如erb样式标记:

You can switch delimiters to something that won't conflict with the triple mustaches, like erb-style tags:

{{=<% %>=}}
{{{
{
  "name": "<% name %>",
  "description": "<% description %>"
}
}}}
{{{debug this}}}
<%={{ }}=%>

请注意,您可以在整个模板中多次执行此操作。每当遇到冲突的事情时,选择一组新的分隔符:)

Note that you can do this as many times as you like throughout your template. Any time you run into something that conflicts, pick a new set of delimiters :)

这篇关于在Mustache模板中转义双括号{{...}}。 (在NodeJS中模板化模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:12