• [Hapi.js] View engines


    View engines, or template engines, allow you to maintain a clean separation between your presentation layer and the rest of your application. This post will demonstrate how to use the vision plugin with hapi to enable template support.

    index.js
        server.register(require('vision'), function(){
            server.views({
                engines: {
                    hbs: require('handlebars')
                },
                relativeTo: __dirname,
                path: 'views'
            });
    
    
            server.route( {
                method: 'GET',
                path: '/user/{username?}', 
                handler: function ( request, reply ) {
                    var username = request.params.username ? request.params.username : "World";
                    reply.view('home', {username: username})
                }
            } );
        });

    home.hbs:

    <h1>Hello, {{username}}!</h1>

    view can also support layout, to do this, we only need to add :

            server.views({
                engines: {
                    hbs: require('handlebars')
                },
                relativeTo: __dirname,
                path: 'views',
                layout: true
            });

    layout.hbs:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>I'm hapi!</title>
        <style>
            * {
                font-family: 'DejaVu Sans';
                font-weight: 100; color: #333;
            }
            h1 {
                margin: 40px; padding: 50px;
                text-align: center; background-color: #FA4;
                box-shadow: 10px 10px 25px 0px #888;
            }
        </style>
    </head>
    <body>
    {{{content}}}
    </body>
    </html>

    It will automaticlly wrap the content into the layout.hbs.

  • 相关阅读:
    数据库隔离级别
    前端传递的参数名称和后端接收的参数名称不一致
    事务管理
    AOP
    spring 集成 shiro安全框架
    SpringMVC----@RequestMapping__请求方式
    SpringMVC----@RequestMapping__修饰类
    SpringMVC入门概述+案例
    SpringMVC-web.xml头代码
    Spring注解驱动开发----->容器day02
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5226222.html
Copyright © 2020-2023  润新知