[Hapi.js] View engines
JS View
2023-09-14 08:59:20 时间
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.
相关文章
- js获取现在时间_js中如何动态显示日期时间
- 【说站】js策略模式是什么
- 【说站】js中闭包如何理解
- JS面试题-js新增基本数据类型BigInt
- Detalk.js —— 具有高自定义性、简洁、轻量的开源评论系统
- 原生 JS 实现 HTML 转 Markdown ,html2md.js
- 【源码】PDF.js批注注释插件库(纯JS). 创建和保存PDF批注(PDF 高亮/签名/插图/截屏/文本框/画笔/多边形)-pdf.js
- JS种offsetTop、scrollTop、clientTop等各个属性的介绍详解编程语言
- Linux上的JS压缩工具(js压缩工具linux)
- 利用 JS 实现 Redis 的连接(js连接redis)
- 使用 JS 操作 Redis:实现强大数据存储.(js调用redis)
- 使用Oracle和JS开发新一代应用仿真世界(oracle js)
- JS面向对象编程之对象使用分析
- js正则表达式之$1$2$3$4$5$6$7$8$9属性,返回子匹配的结果
- Android中父View和子view的点击事件处理问题探讨
- 用js实现table单元格高宽调整,兼容合并单元格(兼容IE6、7、8、FF)实例
- js得到文件后缀(通过正则实现)
- 不提示直接关闭网页窗口的JS示例代码
- js与jquery获取父级元素,子级元素,兄弟元素的实现方法
- js获取当前地址JS获取当前URL的示例代码
- JS下载文件|无刷新下载文件示例代码