kegerator

0.1.0 • Public • Published

Kegerator

Kegerator provides a declarative way to create server-rendered web sites.

Basic Usage

Install kegerator using npm.

npm install kegerator

Application Structure

By default, kegerator uses a file hierarchy like the following:

+-- <app>
    |
    +-- lib/
    |   |
    |   +-- pages/
    |   |   |
    |   |   +-- templates/
    |   |   |   |
    |   |   |   +-- header.html
    |   |   |   |
    |   |   |   +-- footer.html
    |   |   |   |
    |   |   |   +-- welcome.html
    |   |   |   |
    |   |   |   +-- about.html
    |   |   |
    |   |   +-- welcome.js
    |   |   |
    |   |   +-- about.js
    |   |
    |   +-- app.js
    |
    +-- test/
    . . .

The following code would create a basic website using Express and the file hierarchy above:

var express   = require("express"),
    kegerator = require("kegerator");

var app  = express(),
    brew = kegerator();

app.get("/", brew.pages.welcome);
app.get("/about", brew.pages.about);

app.listen(3000);

Page Definitions

kegerator pages are Node modules. Each page is loaded and passed an API object that allows the page to declare various attributes. For example, the following is the implementation of the welcome page above:

module.exports = function (page) {
    page.template   = "welcome.html";
    page.partials   = {
        header : "header.html",
        footer : "footer.html"
    };
    
    page.controller = function () {
        return { date: Date.now() };
    };
};

The page is rendered using the welcome.html template. That template has the ability to make use of the header.html and footer.html partials. Similarly, a rendering context is generated by a declared controller that returns the current date.

For the time being, all templates are mustache templates.

Usage Details

kegerator([options])

Load all defined pages and create a new kegerator instance.

+ **options** - _Optional_. A hash of options to configure the behavior of
    the `kegerator` instance.

The following options are supported:

+ **optimizeScripts** - _Defaults to true._ When set to `true`, this option
    causes all `<script>` tags to be moved to the end of the `<body>`.
+ **optimizeStylesheets** - _Defaults to true._ When set to `true`, this
    options causes all stylesheet declarations to be moved to the end of
    the `<head>`.
+ **pages** - the directory (relative to the calling module) containing the
    page modules.

page.controller

Optional. When defined, provides a function to generate the rendering context based on the request object. A controller may be either synchronous or asynchronous as follows.

Synchronous controller

page.controller = function (request) {
    . . .
    if (error) {
        throw new Error("An error occurred.");
    }
    . . .
    return {
        name : "me",
        date : Date.now()
    };
};

Asynchronous controller

page.controller = function (request, callback) {
    . . .
    if (error) {
        callback(new Error("An error occurred.");
        return;
    }
    . . .
    callback(null, { name: "me", date: Date.now() });
    return;
};

page.partials

Optional. Defines a hash of partial names to files (relative to the <pages>/templates directory. Each partial is effectively a template fragment.

page.scripts

Optional. Defines a single script or list of scripts to insert into the page.

Single script

page.scripts = "/static/js/jquery.min.js";

Multiple scripts

page.scripts = [
    "http://code.jquery.com/jquery-2.0.2.min.js",
    "/static/js/bootstrap.min.js"
];

Note: scripts are loaded in the order declared.

page.stylesheets

Optional. Defines a single stylesheet or list of stylesheets to insert into the page.

Single stylesheet

page.scripts = "/static/css/jquery.min.js";

Multiple scripts

page.scripts = [
    "/static/js/jquery.min.js",
    "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"
];

Note: stylesheets are loaded in the order declared.

page.template

Required. This attribute defines which template in the templates/ subdirectory should be used to render the page. Currently only mustache templates are supported.

Readme

Keywords

none

Package Sidebar

Install

npm i kegerator

Weekly Downloads

0

Version

0.1.0

License

none

Last publish

Collaborators

  • jagoda