jb-router-dust

Middleware for jb-router rendering of dustjs-linkedin templates

npm install jb-router-dust
2 downloads in the last week
5 downloads in the last month

jb-router-dust

jb-router-dust is a plugin for jb-router to render dustjs-linkedin templates.

Example

First, install with npm:

npm install jb-router
npm install jb-router-dust

Create two templates:

./templates/layout.dust

<html>
<head>
    <script src="/js/abcd.js"></script>
    {+HeaderExtension/}
</head>
<body>
    {+Body/}
</body>
</html>

./templates/home.dust

{>"./templates/layout.dust" /}

{<HeaderExtension}
<script src="/js/another.js"></script>
<title>home page</title>
{/HeaderExtension}

{<Body}
<h1>my body content</h1>
{/Body}

Create the server file:

var Router = new (require("jb-router"))();
var Dust = require("jb-router-dust");

function RenderTemplate(TemplateName)
{
    return function(Request, Response)
    {
        Response.RenderDustTemplate("./templates/" + TemplateName + ".dust");
    }
}

Router.Get(/^\/js(\/.+)/, Router.StaticHandler("./static/js"));
Router.Get("/", Dust.Middleware, RenderTemplate("home"));

Router.Listen({ "Port": 4000 });

When a user requests /, the page served will be

<html>
<head>
    <script src="/js/abcd.js"></script>
    <script src="/js/another.js"></script>
    <title>home page</title>
</head>
<body>
    <h1>my body content</h1>
</body>
</html>

Reference

jb-router-dust exposes one function:

Middleware

This is a Router::RequestHandler which adds the method RenderDustTemplate to the Router::Response. It should be added to a route before the template needs to be rendered.

Example: see above.

Rendering a template

After a route has executed jb-router-dust.Middleware, the Router::Response object will have a method RenderDustTemplate

RenderDustTemplate(TemplatePath, Context)
  • TemplatePath: required string, the path to the Dust template file, relative to the working directory. If the provided path does not have the .dust extension, it will be appended. Therefore, one can pass layout instead of layout.dust, but the file itself must have a .dust extension.
  • Context: a javascript object containing variables that will be exposed to the template.

Example

./page.dust {#Navigation} {Name} {/Navigation}

server.js var Router = new (require("jb-router"))(); var Dust = require("jb-router-dust"); var Navigation = [ { "Path": "/", "Name": "Home" }, { "Path": "/about", "Name": "About me" }, { "Path": "/contact", "Name": "Contact" } ];

Router.Get
(
    "/",
    Dust.Middleware,
    function(Request, Response)
    {
        Response.RenderDustTemplate("./page.dust", { "Title": "my page", "Navigation": Navigation });
    }
);

Router.Listen({ "Port": 4000 });

The served page: Home About me Contact

Template inheritance/Partials/Blocks

The first example in this readme demonstrates template inheritance. ./templates/layout.dust includes blocks HeaderExtension and Body. It is optional to define the blocks: if they are not defined they will compile to empty strings.

Note that ./templates/home.dust includes layout.dust with the full path {>"./templates/layout.dust" /} Paths of imported templates are always specified relative to the working directory of the application.

npm loves you