Code Monkey home page Code Monkey logo

nunjucks-hapi's Introduction

nunjucks-hapi

A simple wrapper to let you use Nunjucks as a Hapi templating engine.

Nunjucks documentation

Hapi documentation

Usage

The following example will let you use Nunjucks templates, including template inheritance, inside Hapi. This assumes:

  • your templates are in a directory called "views"
  • they have the extension "html"
  • you have a template file in there called "mytemplate"
var Hapi = require('hapi')
var Path = require('path')
var NunjucksHap = require('nunjucks-hapi')

var server = new Hapi.Server()
server.connection({port:5000,host:'localhost'})

// set up templates
server.views({
  engines: {
    html: NunjucksHapi
  },
  path: Path.join(__dirname, 'views')
})

// Add a route
server.route({
  method: 'GET',
  path: '/test',
  handler: function (request, reply) {
    reply.view('mytemplate',{
      'myvariable': 'myvalue'
    })
  }
})

// start server
server.start()

Using Nunjucks filters and environments

If you want to go beyond the default configuration of Nunjucks, you need to configure an environment. Once that's done, you can do anything to the environment, like add filters or custom tags via extensions. This works just like the Nunjucks documentation says it does, with the exception that the path to templates is now required.

The example is otherwise the same as the above

var NunjucksHapi = require('nunjucks-hapi');

// set a common view path
var viewPath = Path.join(__dirname, 'views')

var env = NunjucksHapi.configure(viewPath)

// do anything you want to the env here
env.addFilter('somefilter', function(str, count) {
  // return some string
})

// set up templates with the same view path
server.views({
  engines: {
    html: NunjucksHapi
  },
  path: viewPath
})

nunjucks-hapi's People

Contributors

seldo avatar rodrigoespinosa avatar paulcpederson avatar

Watchers

James Cloos avatar Kevinleary.net avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.