[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.
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的更多相关文章
- [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 ...
- js 实现angylar.js view层和model层双绑定(改变view刷新 model,改变model自动刷新view)
近段时间研究了下angular.js 觉得它内部实现的view和model层之间存在很微妙的关系,如下图 如上图说的,view的改变会update 数据层model, 数据层会update视图层vie ...
- [Hapi.js] Up and running
hapi is a rock solid server framework for Node.js. Its focus on modularity and configuration-over-co ...
- [Hapi.js] Managing State with Cookies
hapi has built-in support for parsing cookies from a request headers, and writing cookies to a respo ...
- [Hapi.js] Request Validation with Joi
hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...
- [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 ...
- [Hapi.js] POST and PUT request payloads
hapi makes handling POST and PUT payloads easy by buffering and parsing them automatically without r ...
- [Hapi.js] Serving static files
hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...
- [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 ...
随机推荐
- MVC5富文本编辑器CKEditor配置CKFinder
富文本编辑器CKEditor的使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- 使用Dataset
string sqlStr="Select * from Tb_news"; SqlDataAdapter myDa=new SqlDataAdapter(SqlStr,myCon ...
- ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
转帖:原文地址http://blog.csdn.net/panys/article/details/3838846 archive log 日志已满ORA-00257: archiver error. ...
- Tomcat 7.0 进入项目管理页面时的密码问题
tomcat7 这个版本,官方网下载的原始包项目管理页面的权限和之前版本的配置有点区别. 到Tomcat的conf文件夹下找到tomcat-users.xml文件,有配置权限的配置文件. ma ...
- oracle 数据库 if...elsif...语句
CREATE OR REPLACE FUNCTION "UFN_GETIDS" ( OPEKIND IN VARCHAR2,-- 查询类型 PARAMS IN ...
- html5新特性--音频视频,拖放
1.音频 <audio controls> <source src="aaa.ogg" type="audio/ogg"> <so ...
- ESP8266固件烧录方法
今天拿到ESP8266的板子,第一步是进行烧录固件. 首先是使用官方自带的参考文档,进行操作.发现每次烧录均卡在等待同步上电. 之后发现是烧录方法错误. 正确的烧录方法: 先按下FLASH不放,再按烧 ...
- 初学QML之QML和C++混合方法
混合使用QML和C++的方法 1加载一个QML组件,然后从 C++对其进行操作: 2直接将一个C++对象及其属性嵌入到QML组件: 3定义一个新的QML元素(通过基于QOject的C++类)并在QML ...
- 解决mini2440开发板和虚拟机相互ping不通
很奇怪的事,前段时间使用都还是好好的,但今天不知什么原因开发板和虚拟机怎么也无法PING通. 虚拟机用的:fedora14 开发板IP:192.168.0.250 虚拟机IP:192.168.0.10 ...
- hadoop配置文件的加载机制
hadoop通过Configuration类来保存配置信息 1.通过Configuration.addResource()来加载配置文件 2.通过Configuration.get***()来获取配置 ...