Strong (-like) template for Express?

Is there a template engine for Express (node.js) that is based on Mustache or uses a similar syntax?

All I could find was haml , jade , ejs , jquery templates and one based on CoffeeScript (I am writing simple JS).

I want to write "normal" html, so only ejs and jqtpl . I already used mustache with backbone , so it would be better to use it on the server side using Node.js

+4
source share
8 answers

You may have added Mustache as a rendering engine by following the Express manual :

Viewing file names takes the form ".", Where the module name> is required. For example, the layout.ejs view tells the viewer> require ('ejs), the loadable module must export the export.compile (str,> options) method and return a function to match Express.

Edit: From the Oral Meaning Guide to the Usage section:

The following is a brief example of using mustache.js:

 var view = { title: "Joe", calc: function () { return 2 + 4; } }; var output = Mustache.render("{{title}} spends {{calc}}", view); 

In this example, the Mustache.render function takes two parameters: 1) a mustache template> and 2) a presentation object containing the data and code necessary to render the template>.

From the above, I suspect that you can simply export Mustache.render, but I have not tested it. The object literals used as data look the same, but if they are really different from each other, perhaps you can just build Mustache.render into a function that formats it correctly.

Edit: The reference to the Xomby wrapper contains an example of how to transfer descriptors for an expression, Mustache should be similar.

+5
source

Try Hogan.js http://twitter.github.com/hogan.js/

I think that uses Twitter and LinkedIn in production.

+4
source

I just stumbled upon this ancient thread, but no one mentioned the .js union, which seems to be the β€œright” way in Express Express (see http://expressjs.com/faq.html ). It also provides an easy way to switch / experiment with template systems.

Here is a simple example - http://invitingthebell.com/2012/12/24/mustache-templates-in-express-3-0/ .

Code, in case of its disappearance:

 var express = require('express') , cons = require('consolidate') , app = express(); // assign the mustache engine to .html files app.engine('html', cons.mustache); // set .html as the default extension app.set('view engine', 'html'); app.set('views', __dirname + '/views'); // test mustache app.get('/', function(req, res){ var viewdata = { 'test' : 'Hey now.'}; res.render('index', viewdata); }); app.listen(3000); 

The index.html file in the views directory:

 <html> <head><title>Some CMS</title></head> <body> <h1>Mustache</h1> <p>What do you say?</p> <p>{{test}}</p> </body> </html> 
+3
source

Here is a working example / tutorial on using NodeJS, ExpressJS and MustacheJS Template Engine:

http://devcrapshoot.com/javascript/nodejs-expressjs-and-mustachejs-template-engine

You can create a complete web page as usual by placing the mustacheJS fields wherever you want. Use express to go to the page, use node fs.readFileSync (); To get an html file, use a mustache to update the data on the page, and then pop it to the client.

This is something neat. Hope this helps!

-A -

+2
source

Have you tried stache yet ? It is no longer supported, but you can follow some links and get more recent content.

+1
source

I found Handlebars.js , which is a Mustache template / system extension.

And there is a really simple wrapper to use with Express.

+1
source

Of course, the best way to do this is with a post here:

http://iamtherockstar.com/blog/2011/11/21/using-mustache-templates-express-apps/

So far, this has worked great for me. The only problem I discovered was not using partitions in the root path for the views. For example, partial in a view / partial - by default, the engine finds only partial views. Let me know if you find out!

+1
source

Check out Handlerbars. "Handlebars provides the power you need to efficiently create semantic templates without any frustration. The steering is largely compatible with Mustache templates. In most cases, you can change the Mustache to Handlebars and continue to use your current templates. Full details can be found here. " - Handlebars

0
source

Source: https://habr.com/ru/post/1391270/


All Articles