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

时间: 2024-10-05 00:24:16

[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 your application needs errors rendered in HTML? This lesson shows how to implement friendly HTML error messages using hapi extension events. const Hapi

[Hapi.js] Serving static files

hapi does not support serving static files out of the box. Instead it relies on a module called Inert. This lesson will cover serving static files using Inert's custom handlers. 'use strict' var Hapi = require( 'hapi' ); var Boom = require('boom'); v

[Hapi.js] Request Validation with Joi

hapi supports request validation out of the box using the joi module. Request path parameters, payloads, and querystring parameters can be validated with joi's simple, 'use strict' const Hapi = require('hapi') const Joi = require('joi') const server

[Hapi.js] Managing State with Cookies

hapi has built-in support for parsing cookies from a request headers, and writing cookies to a response, making state management easy and straight-forward. It even has built in support for cookie encryption and auto detects when a cookie contains JSO

[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 you can hook-in to provide additional functionality, called "extension events". This lesson will give you an introduction to using extension even

[Hapi.js] Route parameters

Routing is a fundamental aspect of any framework. In this lesson, you'll learn how to use path parameters in hapi's router. We'll also touch on how the router uses specificity to order routes internally. Router Param: http://localhost:8000/ztw // Wil

[Hapi.js] Replying to Requests

hapi's reply interface is one of it's most powerful features. It's smart enough to detect and serialize objects, buffers, promises and even streams. This post will demonstrate first hand how to use the reply method to send your data to the client, no

[Hapi.js] Logging with good and good-console

hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes everything needed to configure logging for your application. This post will introduce good, a process monitor for hapi, and good-console, a reporter for g

docker+mysql(8.0.15)+node.js(hapi.js)构建容器(命令行)

安装docker, 准备一个node.js项目,项目中包含数据库配置. 一.将node.js项目创建为image 在项目中创建.dockerignore文件和dockerfile文件(https://github.com/hanxiaoer1992/docker_nodejs_cmd) cd 项目文件夹下(cd ....../docker_nodejs_cmd) docker image build -t dockertest:0.0.1 . 二.拉取mysql docker pull mysq