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

posted @   Zhentiw  阅读(279)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示