gpt4 book ai didi

javascript - 相思木- Handlebars : Unable to render view: The partial could not be found

转载 作者:太空宇宙 更新时间:2023-11-03 22:35:51 36 4
gpt4 key购买 nike

// project/layouts/main.hbs

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
{{{@body}}}
</body>
</html>


// project/views/home-public.hbs

{{> nav-public}}
<div class="container">
<div class="starter-template">
<h1>Home Public</h1>
<p class="lead">This is my home.</p>
</div>
</div>


// project/partials/nav-public.hbs

<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Example</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">Journey</a>
</li>
<li><a href="/">Departures</a>
</li>
<li><a href="about" style="margin-left:1em">About</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="/signout">Sign in</a>
</li>
</ul>
</div>
</div>
</nav>

运行的 Node 代码:

app.use(handlebars({
defaultLayout: 'main'
}));

app.use(function* () {
yield this.render('home-public', {
user: {
email: "name@example.com"
}
});
});

我看不出出了什么问题。有什么想法吗?

最佳答案

这个一开始也让我感到困惑。原因是您在 {{>partial-name}} 调用中使用连字符和/或路径分隔符,当您来自express-handlebars时,这似乎是合理的。答案在文档中:

partialId(file)

This function is a little backwards compared to layouts and views, but it takes a path for a partial template file. (relative to partialsDir) and converts it into a handlebars-friendly identifier.

For example: "navigation.hbs" => "navigation"

By default, it will strip the extension and camel-case the remaining string.

For example: "nav/main.hbs" => "navMain"

所以基本上,默认情况下它会将您的部分路径完全转换为camelCaseForHyphensAndDirectorySlashes,例如example/my-partial将需要部分调用{{> exampleMyPartial }}.

幸运的是,如果您更喜欢使用实际代表部分文件路径的部分名称,那么自定义它相当容易。这是我使用的配置(我使用 strip-extension 模块来删除文件扩展名):

var stripExtension = require('strip-extension');
app.use(koaHandlebars({
partialId: function(file) {
// Note: the .replace below is just to normalise windows paths, you
// may not need it.
return stripExtension(file).replace('\\', '/');
}
});

关于javascript - 相思木- Handlebars : Unable to render view: The partial could not be found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29309198/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com