用车把多次传递变量替换? [英] multiple pass variable replacements with handlebars?
问题描述
我还没有找到合适的答案,所以我想尝试一下.
I haven't found a decent answer to this yet, so I figure I'll try.
用例.我有HTML文件,需要进行2次处理.其中之一具有静态变量,但是我需要在编译时将它们替换为多层(开发,中介,生产)的固定值.
Use-case. I have HTML files that will require 2 passes of processing. One of them has static variables in it, but I need them replaced on compile time with fixed values for multiple tiers (dev, intermediary, production).
我还有一组变量,这些变量将在下载这样的模板时被替换,这将是第二遍.
I also have a set of variables that will get replaced when a template like this is downloaded, which would be the second pass.
<!DOCTYPE html>
<html>
<head>
...stuff
</head>
<body>
<h1>Hello {{ReplaceOnCompile}}</h1>
<a href="{{ReplaceOnRuntime}}">Some Link</a>
</body>
</html>
据我所知,Handlebars并没有提供开箱即用的方法.我目前正在使用以下系统工作:
As far as I know, Handlebars doesn't provide any way out of the box to do this. I currently have the system working using the following:
// Create handlebars object from template
const template = await compile(rawFileContents)
// Replace template with variables
const html = await template({
ReplaceOnRunTime: 'foo'
})
那一切都很好,花花公子,但这让我有了:
Thats all good and dandy, but that leaves me with:
<!DOCTYPE html>
<html>
<head>
...stuff
</head>
<body>
<h1>Hello </h1>
<a href="foo">Some Link</a>
</body>
</html>
我在其文档中看不到任何支持自定义定界符/包装器功能来进行传递一/传递两种事情的事情.
I don't see anything in its docs that supports custom delimiters/wrapper experessions to do a pass one/pass two kind of thing.
我的下一个想法是使用 sed
或仅使用像这样的包来进行第一遍替换: https://www.npmjs.com/package/replace-in-file
My next idea is to use sed
or just a package like this to do the first pass replacements:
https://www.npmjs.com/package/replace-in-file
在模板中,对要替换的每种类型的变量使用不同的语法:
And in the template, use a different syntax for each type of variable to replace:
<!DOCTYPE html>
<html>
<head>
...stuff
</head>
<body>
<h1>Hello {<<ReplaceOnCompile>>}</h1> // Pass this through one replacement func
<a href="{{ReplaceOnRuntime}}">Some Link</a> // Pass this through a second one.
</body>
</html>
不确定最好的方法,因为我试图避免使用两个不同的库来完成基本相同的任务,只是把它们错开了.
Not sure the best approach to this, as I'm trying to avoid using two different libraries for essentially the same task, just staggered.
推荐答案
我找到了一种逃脱它们的好方法!
I found a neat way to escape them!
https://repl.it/@nicholashazel/TriflingMeanLogin
您可以通过使用反斜杠对转义表达式进行转义,并使其输出原始的 {{variableName}}
.
You can escape a handlebars expression and have it output the raw {{variableName}}
by simply using a backslash.
<body>
<h1>{{ReplaceMe}}</h1>
<p>\{{DontReplaceMe}}</p>
</body>
const variables = {
ReplaceMe: 'Replaced in pass 1'
}
const template = await handlebars.compile(data)
const html = await template(variables)
<body>
<h1>Replaced in pass 1</h1>
<p>{{DontReplaceMe}}</p>
</body>
Pass 2
const newVariables = {
DontReplaceMe: 'Replaced in pass 2'
}
const newTemplate = await handlebars.compile(html)
const newHtml = await newTemplate(newVariables)
<body>
<h1>Replaced in pass 1</h1>
<p>Replaced in pass 2</p>
</body>
这篇关于用车把多次传递变量替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!