handlebars.js 中缺少(或“可选”)表达式?
我正在节点中编写构建脚本。简而言之,该脚本执行以下操作:
- 提示用户输入信息(项目名称、描述等)
- 克隆模板 git repo
- 从模板名称重命名文件(例如 com_foo_template.js --> com_foo_myproject.js)
- 替换中的标记基于步骤 1 中输入的一些模板文件
我尝试使用 Handlebars.js 进行令牌替换步骤。
还有第二阶段的部署,其中也涉及代币替换。这是我的问题的根源。
在某些文件中,其中包含的所有令牌都将在初始化期间被替换(克隆/重命名/替换部分)。在其他文件中,只有其中一些令牌会在初始化时被替换,而其他令牌在部署步骤运行之前不会被替换(例如部署日期、git 提交哈希等)。考虑以下文件:
<zimletConfig name="{{name}}" version="{{deploy_version}}">
<global>
<property name="allowedDomains">*.foo.com</property>
<property name="gitCommit">{{gitcommit}}</property>
<property name="deployDate">{{deploydate}}</property>
</global>
</zimletConfig>
在此文件中,只有 {{name}} 应该在初始化时替换;其他令牌应仅在部署时替换。但是,当我运行 init 步骤时,部署时令牌将被替换为空字符串:
<zimletConfig name="com_foo_myproject" version="">
<global>
<property name="allowedDomains">*.foo.com</property>
<property name="gitCommit"></property>
<property name="deployDate"></property>
</global>
</zimletConfig>
如果传入的对象中不存在令牌,是否有办法让句柄不替换令牌?
I'm writing a build script in node. In a nutshell, the script does the following:
- prompts the user for information (project name, description, etc)
- clones a template git repo
- renames files from their template name (e.g. com_foo_template.js --> com_foo_myproject.js)
- replaces tokens in the some template files based on the input in step 1
I'm attempting to use Handlebars.js for the token replacement step.
There's a second phase for deploying, which also involves token substitution. This is the root of my question.
In some of the files, all of the tokens contained within will be replaced during init-time (the clone/rename/replace part). In other files, only some of these tokens will be replaced at init-time, and others won't be replaced until the deploy step runs (things like the deploy date, the git commit hash, etc.). Consider the following file:
<zimletConfig name="{{name}}" version="{{deploy_version}}">
<global>
<property name="allowedDomains">*.foo.com</property>
<property name="gitCommit">{{gitcommit}}</property>
<property name="deployDate">{{deploydate}}</property>
</global>
</zimletConfig>
In this file, only {{name}} should be replaced at init-time; the other tokens should be replaced at deploy-time only. When I run my init step, though, the deploy-time tokens are replaced with empty strings:
<zimletConfig name="com_foo_myproject" version="">
<global>
<property name="allowedDomains">*.foo.com</property>
<property name="gitCommit"></property>
<property name="deployDate"></property>
</global>
</zimletConfig>
Is there a way to have handlebars not replace tokens if they don't exist in the object passed in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我有一个解决方案;我实现了一个
helperMissing
帮助器,它似乎像我想象的那样工作。});
我很好奇这是否是解决问题的最佳方法。
I think I've got a solution; I implemented a
helperMissing
helper and it seems to work like I'd think it should.});
I'm curious if this is the best way to solve the problem.
这不是最好的解决方案,因为如果占位符有 . (点)在两者之间,即如果它的对象变量,那么这将不起作用,
例如:
version="{{my_object.deploy_version}}
对于具有 object.value 格式的占位符,需要一个解决方案。
我可以看到只能使用自定义帮助器来实现此目的。它会变得有点难看。例如:
This is not the best solution because if the placeholder has a . (dot) in between, i.e. if its object variable, then this will not work
eg:
version="{{my_object.deploy_version}}
A solution is needed for placeholders which has object.value format.
I can see achieving this only with a custom helper. it would get a little ugly. Eg: