Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include server-side script in Jade template?

I've just installed moment.js; now I want to access moment inside my Jade template. Example:

.main-content
    .container
        .access-details.clearfix
            .left
                div Logged in: <b>#{user.name}</b>
                div Access Lvl: #{user.accessLevel}
            .right
                div= moment().format("dddd, MMMM Do YYYY, h:mm:ss a")

To be clear, I want the date to be formatted server-side and then sent to the client as a rendered string.

So how do I make a JavaScript library available inside of a Jade template?


I should probably note that I'm using this with Express:

var server = express.createServer();
server.configure(function () {
    server.set('view engine', 'jade');

Is there some options I have to pass in there somehow to tell it which libraries to include?


Just occured to me that this is absolutely no different than passing in a local variable. e.g.,

server.get('/', function (req, res) {
    res.render('index', {
        locals: {
            moment: require('moment')
        }
    });
});

But I don't want to pass this in to every view; if I ever forgot it my app would break as it's used in the main layout. So how do I make sure it's always available?

like image 449
mpen Avatar asked Jan 14 '13 00:01

mpen


People also ask

Can you use JavaScript on server-side?

js is a runtime environment to allow JavaScript to not only be run in the browser, but also on the server (or almost any environment, really). That also expanded the types of applications that could be built with the language since it wasn't tied to only the client-side anymore.

How JavaScript is executed on server-side?

How Does JavaScript Run On A Server? Node. js works on a v8 environment – it is a virtual machine or a JavaScript engine that runs the JavaScript code, so for hosting you can't use the ordinary web hosts. You will need the ones that have the v8 environment.

Is Jade template engines can be used with node js?

Jade is a template engine for Node. js. Jade syntax is easy to learn. It uses whitespace and indentation as a part of the syntax.

What is server side JavaScript called?

Server Side JavaScript (SSJS) is an extended version of JavaScript that enables back-end access to databases, file systems, and servers. Server side javascript, is javascript code running over a server local resources , it's just like C# or Java, but the syntax is based on JavaScript. A good example of this is Node.


2 Answers

Helpers are removed from Express 3 or 4.

Kindly use app.locals

app.locals.moment = require('moment');

The app.locals object is a JavaScript Object. The properties added to it will be exposed as local variables within the application.

So you can use app.locals in any view files, Jade or EJS;

moment().format('YYYY-MM-DD h:mm:ss');
like image 96
lijinma Avatar answered Sep 20 '22 17:09

lijinma


This answer got my pointed in the right direction, although the documentation on dynamicHelpers seems to have mysteriously disappeared from the Express documentation.

Also, I didn't need a dynamic helper, just a static one (no access to request/response). So I took a stab in the dark at what it would be called:

server.helpers({
    moment: require('moment')
});

Works like a charm! moment is now available in all my views.

like image 29
mpen Avatar answered Sep 20 '22 17:09

mpen