0

Hi I'm new to node and I'm trying to figure out how to compile partials from a file into a string using doT.js (http://olado.github.io/doT/index.html).

/controllers/system/index.js:

var util = require( 'util' ),
    fs = require( 'fs' ),
    dot = require( 'dot' );

function System( ) {
    this.name = 'system';
}

System.prototype._loadFile = function( path, def ) {
    var def = def || { },
        tpl, str;

    str = fs.readFileSync( process.argv[ 1 ].replace( /\/[^\/]*$/, path ) );
    tpl = dot.compile( str );
    str = tpl( def );

    return str;
};

System.prototype._getHeaderContent = function( ) {
    return this._loadFile( '/controllers/system/views/header.html', {
        name: this.name
    });
};

System.prototype._getBodyContent = function( ) {
    return this._loadFile( '/controllers/system/views/main.html' );
};

System.prototype._getFooterContent = function( ) {
    return this._loadFile( '/controllers/system/views/footer.html' );
};

System.prototype.index = function( req, res, next ) {
    res.render( __dirname + '/../system/views/template', {
        header: this._getHeaderContent( ),
        body: this._getBodyContent( ),
        footer: this._getFooterContent( )
    });

    next ? next( ) : '';
};

module.exports = System;

/controllers/system/views/template.html:

{{=it.header}}
{{=it.body}}
{{=it.footer}}

/controllers/system/views/header.html:

<!doctype html>
<html lang="en">
<head>
    <title>app | {{=it.name}}</title>
</head>
<body>

In System.index when I call res.render template.html is being compiled but header.html is not. From that I assume render doesn't recursively render files and I need to manually compile header.html before I pass it in to res.render.

I found this tutorial: http://olado.github.io/doT/tutorial.html#compile but both .template and .compile return functions? Attempting to pass them in to res.render throws an error.

How do I compile them into strings? Or am I going about this the wrong way?

Thanks!

Update- fixed and updated with working code thanks Andreas Hultgren!

JstnPwll
  • 8,585
  • 2
  • 33
  • 56
Jonathan
  • 543
  • 9
  • 23
  • 1
    You say .compile returns a function. Call that function and you should get the string, right? – Andreas Hultgren Sep 14 '13 at 15:33
  • I updated my post with what I think you meant and it appears to be working (page renders with variables replaced) but I'm getting an error in the console. Any ideas what I'm missing? – Jonathan Sep 14 '13 at 16:16
  • I can't see any cause of that in your code. It seems you're somewhere trying to set response headers after calling res.render (perhaps in another middleware?) – Andreas Hultgren Sep 14 '13 at 16:27
  • Found the second problem, the routing for my static files isn't working properly. Thanks for the help, if you want to add your post as an answer I'll mark it as complete. – Jonathan Sep 14 '13 at 16:42

1 Answers1

1

dot.compile()or dot.template() returns a compiled function from a template string. Call it with or without data (compiledTemplate(data)) to get the generated template as a string. As a side-note, most template engines work this way.

Andreas Hultgren
  • 14,763
  • 4
  • 44
  • 48