简体   繁体   English

Jade / Pug:将包含变量的html传递到模板中

[英]Jade / pug: passing html that contains a variable into template

I'm working on an application based on keystone.js. 我正在开发基于keystone.js的应用程序。 I would like the end-users to be able to create content that changes based on variables. 我希望最终用户能够创建随变量而变化的内容。 For example, if they would like to create an e-mail template, they could write: 例如,如果他们想创建一个电子邮件模板,则可以编写:

Hi #{firstName},... #{firstName},您好,...

That would be passed into jade as plain html like so: 它将像普通的html一样传递到翡翠中,如下所示:

Hi #{firstName}... #{firstName},您好...

The template should evaluate #{firstName} to the first name in question. 模板应将#{firstName}评估为所讨论的名字。

I've tried to implement this in a jade template as follows: 我试图在一个玉模板中实现这一点,如下所示:

//Jade template
.div
  p #{firstName}
.div
   !=html

The line p #{firstName} prints the value for firstName, but !=html prints "Hi #{firstName},...". 第p#{firstName}行打印firstName的值,但!= html打印“ Hi#{firstName},...”。

Is there anything I'm doing wrong or is this just not supported with Jade? 我做错了什么吗?Jade不支持吗?

this is expected behavior. 这是预期的行为。 ! is a escape character and expects only text not variables. 是一个转义字符,期望仅文本而不是变量。

you should evaluate pug html in nodejs, pass this string as variable to pug template. 您应该在nodejs中评估pug html,将此字符串作为变量传递给pug模板。

nodejs code nodejs代码

var pug = require('pug');
html = pug.render(html, {firstName:firstName});

pug code 哈巴狗代码

.div
  p #{firstName}
.div
   !=html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM