[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 ...
随机推荐
- [RxJS] Basic DOM Rendering with Subscribe
While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...
- 住javaWeb分页实现(模拟百度首页)
本文来源于 http://blog.csdn.net/tjpu_lin/article/details/41050475 近期在开发一个项目,项目中有非常多数据展示的模块.所以要用到分页,网上搜了非常 ...
- hdu2767 Proving Equivalences --- 强连通
给一个图,问至少加入�多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入�最少的边连成环,就是把图上入度为0和出度为0的点连 ...
- Linux 统计代码行数命令
wc -l `find . -name '*.js'` wc -l `find . -regex ".*\.js"`
- Java性能漫谈-数组复制之System.arraycopy
当我还年幼的时候,我很任性,复制数组也是,写一个for循环,来回倒腾,后来长大了,就发现了System.arraycopy的好处. 为了测试俩者的区别我写了一个简单赋值int[100000]的程序来对 ...
- 《第一行代码》学习笔记19-广播接收器Broadcast_Receiver(2)
1.解决广播的安全性问题,Android引入了一套本地广播机制,使用该机制发出的广播只能够在应用程序内部进行传递,并且广播接收器只能 接收来自本应用程序发出的广播. 2.本地广播无法通过静态注册来接收 ...
- iOS 无限轮播图的两种实现
首先说一下实现的思想: 用UIScrollView实现,在scrollView上添加3个UIImageView,分别用来显示上一张图片,当前显示的图片,下一张图片.scrollView在不滑动的时候永 ...
- SDOI HH的项链 HEOI采花
题目大意: SDOI求一个区间内只出现一次的数的个数.多组询问. HEOI 求一个区间内出现至少两次的数的个数.多组询问. SDOI HH'neckplace如果每次询问都是1..r的话,那么我们只要 ...
- poj2709 贪心基础
D - 贪心 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bi ...
- Angularjs 日期格式转换
我自己的随笔,记录我编码的点滴. <!DOCTYPE HTML><html><head> <meta charset="utf-8" ...