【发布时间】:2018-07-25 09:59:22
【问题描述】:
我正在处理一个静态网页,我想在不同的 html 文件中添加部分内容。
考虑一个带有 javascript 代码的 src 文件夹,该代码稍后由 browserify 编译,以创建一个 bundle.js 文件。
在 src 文件夹中,我创建了一个 views/partials/test 路径。 test 文件夹包含车把部分尝试。
test/template.js
const yo = require('yo-yo')
let template = yo`
<script id='test' type='text/x-handlebars-template'>
<div>
<h1>This is a test sentence</h1>
</div>
</script>
`
module.exports = template
test/index.js
const handlebars = require('handlebars')
const template = require('./template')
$('body').append(template);
handlebars.registerPartial('Test', $('#test').html());
在主 html 文件中,我只添加了 bundle.js 并将部分添加到正文中,在本例中为 {{> Test}},根据 handlebars.registerPartial 名称。
但是,在主页视图中,我得到的语法 {{> Test}} 是纯文本,而不是测试文本:这是一个测试句。我尝试将把手 cdn 脚本直接添加到 html 文件,但它显示相同的结果。
我是否在 javascript 代码中遗漏了某些内容,或者我需要在主 html 文件中添加一些内容?
我认为handlebars.registerPartial 没有注册任何东西..
任何解决此问题的建议将不胜感激。
【问题讨论】:
标签: javascript html handlebars.js partials