简体   繁体   English

sails.js应用程序中的车把布局和局部

[英]handlebars layouts and partials in a sails.js application

I'm developing a web application with sails.js v0.10 and I'm using handlebars templates, I want to use layouts and partials. 我正在使用sails.js v0.10开发一个Web应用程序,并且正在使用把手模板,我想使用布局和局部视图。 I figured out that sails.js uses express3-handlebars to provide handlebars layouts and partials. 我发现sails.js使用express3-handlebars提供把手布局和局部。 But I can't use both at the same time. 但是我不能同时使用两者。

My config/views.js file is: 我的config / views.js文件是:

    ...
    engine: 'handlebars',
    layout: 'layouts/main.handlebars'
    ...

This works perfectly, the layout is loaded. 完美运行,加载了布局。 The problem is when I'm try to use partials, I get an error saying that the partial was not found. 问题是当我尝试使用局部函数时,我收到一条错误消息,指出未找到局部函数。

    //UserController.js
    ...
    index: function(req, res) {
        res.view({partials: {userlist: 'partials/userlist'}});
    ...

This only works when I change the value of the config/views.js layout property to false. 这仅在我将config / views.js布局属性的值更改为false时才有效。 But when I do that I can't use layouts anymore. 但是当我这样做时,我不能再使用布局了。

is it a bug in sails.js? 是sails.js中的错误吗? or, am I doing something wrong? 还是我做错了什么?

This is not a bug. 这不是错误。 It is actually by design. 它实际上是设计使然。

In Express 3, built-in support for layouts/partials was deprecated. 在Express 3中,已弃用了对布局/部分的内置支持。 Instead, developers are expected to rely on the view engines themselves to implement this features. 相反,期望开发人员自己依靠视图引擎来实现此功能。 (See https://github.com/balderdashy/sails/issues/494 for more info on that.) (有关更多信息,请参见https://github.com/balderdashy/sails/issues/494 。)

Since adopting Express 3, Sails has chosen to support the legacy layouts feature for convenience, backwards compatibility with Express 2.x and Sails 0.8.x apps, and in particular, familiarity for new community members coming from other MVC frameworks. 自从采用Express 3以来,Sails选择了支持旧版布局功能的目的是方便,与Express 2.x和Sails 0.8.x应用程序向后兼容,尤其是让其他MVC框架的新社区成员熟悉。 As a result, layouts have only been tested with the default view engine (ejs). 结果,仅使用默认视图引擎(ejs)对布局进行了测试。

If layouts aren't your thing, or (for now) if you're using a server-side view engine other than ejs, (eg Jade, handlebars, haml, dust) you'll want to set layout:false in sails.config.views, then rely on your view engine's custom layout/partial support. 如果布局不是您的事,或者(暂时)是使用ejs以外的服务器端视图引擎(例如Jade,车把,haml,dust),则需要在航行中设置layout:false。 config.views,然后依靠您的视图引擎的自定义布局/部分支持。

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

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