【问题标题】:Using hapi and handlebars, with the default layout support from hapi, can alternative layout be selected from with a page?使用 hapi 和车把,以及 hapi 的默认布局支持,是否可以从页面中选择替代布局?
【发布时间】:2015-05-20 13:38:05
【问题描述】:

Another answer 和 hapi api 表明 hapi 在使用句柄栏时内置了对布局的支持。然而,似乎只允许配置中定义的一种布局作为默认“layout.html”布局的替代方案。

that answer 中展示了如何使用把手布局来使用把手布局支持在这样的页面中执行此操作:

{{#extend "layout2"}}{{/extend}}

虽然我可以使用把手布局,但我想尽可能多地使用 hapi 提供的内置内容。

是否有可能拥有超过默认布局并在页面模板中选择该布局?可能是这样的:

{{!< layout/layout2}}

【问题讨论】:

    标签: templates layout handlebars.js hapijs


    【解决方案1】:

    您可以覆盖reply.view() 中的视图管理器配置:

    options - 可选对象,用于覆盖此响应的服务器视图管理器配置。无法覆盖仅在初始化时加载的 isCachedpartialsPathhelpersPath

    这是一个例子:

    index.js:

    var Handlebars = require('handlebars');
    var Hapi = require('hapi');
    var Path = require('path');
    
    var server = new Hapi.Server()
    server.connection({
        host: '127.0.0.1',
        port: 8000
    });
    
    server.views({
        engines: {
            html: Handlebars.create()
        },
        path: Path.join(__dirname, 'views'),
        layoutPath: Path.join(__dirname, 'views/layouts'),
        layout: 'default'
    });
    
    server.route({
        method: 'GET',
        path: '/default',
        handler: function (request, reply) {
    
            reply.view('item', { title: 'Item Title', body: 'Item Body' });
        }
    });
    
    server.route({
        method: 'GET',
        path: '/custom',
        handler: function (request, reply) {
    
            reply.view('item', { title: 'Item Title', body: 'Item Body' }, { layout: 'custom' });
        }
    });
    
    server.start();
    

    视图/布局/custom.html:

    <html>
      <body>
        <h1>Custom Layout</h1>
        {{{content}}}
     </body>
    </html>
    

    视图/布局/default.html:

    <html>
      <body>
        <h1>Default Layout</h1>
        {{{content}}}
     </body>
    </html>
    

    视图/item.html:

    {{body}}
    

    当您访问http://localhost:8000/default 时,它将使用default.html。但是http://localhost:8000/custom 将使用custom.html

    【讨论】:

    • 不错!虽然我希望在页面中定义布局.. 我可以使用它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2021-01-18
    • 2020-02-02
    相关资源
    最近更新 更多