Plugin to fetch the raw source of your Express templates
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

View Template Source for Express

Node.js is pretty awesome -- you get to write server-side code in the same language that you use on the client. You can even use the same templating language to render HTML on the server and the client.

Wouldn't it be awesomer if you could seamlessly use the same templates in both places? This is a small plugin for Express that allows you to fetch the raw source of your templates (views/partials), taking advantage of Express' view caching (if enabled). You can then extend your favorite templating engine to include template source in your rendered views.


var express = require('express'),
    app = express.createServer();

We now extend our templating engine with a new tag. I'm using node-jqtpl:

var jqtpl = require('jqtpl');
app.register('.jqtpl',; = {
    open: "_.push(app.getTemplateSource('$1'));"

Now we can do this in a view:

<script type="text/x-jquery-tmpl" id="mytmpl">
{{inc hi-mom}}

Magic! The script block will be filled in with the contents of hi-mom.jqtpl, so rendering your template on the client is easy: