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.

[Hapi.js] View engines的更多相关文章

  1. [Hapi.js] Friendly error pages with extension events

    hapi automatically responds with JSON for any error passed to a route's reply()method. But what if y ...

  2. js 实现angylar.js view层和model层双绑定(改变view刷新 model,改变model自动刷新view)

    近段时间研究了下angular.js 觉得它内部实现的view和model层之间存在很微妙的关系,如下图 如上图说的,view的改变会update 数据层model, 数据层会update视图层vie ...

  3. [Hapi.js] Up and running

    hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...

  4. [Hapi.js] Managing State with Cookies

    hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...

  5. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...

  6. [Hapi.js] Extending the request with lifecycle events

    Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...

  7. [Hapi.js] POST and PUT request payloads

    hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...

  8. [Hapi.js] Serving static files

    hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...

  9. [Hapi.js] Replying to Requests

    hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serial ...

随机推荐

  1. Linux查看网络即时网速

    sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 使用ntop等工具,就更方便简单了,不过这个更灵活 P.S. sar在sysstat包 来源:http://www.c ...

  2. DataTable复制自身行

    在我们工作的过程中有可能要使用DataTable产生一些重复数据(在不重复读取数据库的情况下) 无废话,直接上代码 DataTable复制自身一行(目的产生重复数据),已测试通过可直接复制 /// & ...

  3. 分享一个在线制作GIF格式loading图片的好网址

    等待您寻找惊喜:http://preloaders.net/

  4. 重写OnPaint事件对窗体重绘 实例1

    public class WindowOne:Form { public WindowOne() { InitializeCompnent(); } public void InitializeCom ...

  5. 如何安装windows7系统

      在XP时代,光驱对于我们而言仅仅是装系统用,不过在Win7发布之后,可以用U盘直接安装,省时省力. 首先在互联网下载UltraISO光盘映像文件制作/编辑/格式转换工具,(当然还有其它如WinIS ...

  6. 工欲善其事必先利其器---SQL在线可视化模型设计,(还可学习拖拽知识)

    作为技术人员,在开发项目中,不可避免的要跟数据库打交道,一个完整的项目正常情况下是讨论完整体需求,有了大致的框框在脑海中后,是需要设计合理的数据库的,这时会有其他的专业的UML建模工具可以使用, 但是 ...

  7. 《转载》深入理解 cocos2d-x 坐标系

    原文地址:http://www.cnblogs.com/lyout/p/3292702.html. 首先我们添加两个测试精灵(宽:27,高:40)到场景里面: CCSprite *sprite1 = ...

  8. CM3存储器系统

    1.位带(Bit-Band):如1M的地址都可以用bit访问,然后用32M的地址对应这1M的地址.其中这32M地址的每个字的最低位对应那1M可bit寻址的每个位.

  9. 改变HTML中超链接的显示样式

    更详细的内容请参考:http://www.w3school.com.cn/tags/tag_a.asp HTML中的代码如下: <a class="news_title" t ...

  10. Mvc htmlhelper that generates a menu from a controller

    Simple menu system that grabs a list of actions from a single controller and creates an unordered li ...