[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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值