Using html files instead of dot files with doT.js - node.js

I have a node application and i'm using express and the doT.js template engine.
Everything is working but my template views have to be files with the extension .dot. I would prefer to have my views be html files because html is more ubiquitous.
Here is some of my current code:
var app = express()
app.engine('dot', express_dot.__express);
app.set('views', path.join(__dirname, './public/dot_views'));
app.set('view engine', 'dot');
what can I add or change so that my views don't have to be .dot files and can be .html files instead?

All you should need to do is rename the view engine identifier from dot to html, as that's what express is using for the file extension:
app.engine('html', express_dot.__express);
app.set('views', path.join(__dirname, './public/dot_views'));
app.set('view engine', 'html');
You are configuring the engine explicitly, so, as long as it's changed in both places, express will match up the html view engine with the doT engine you've configured as html.

Related

Can't render new Handlebar view because it redirects by the engine

I need to render a new .hbs page without being redirected by the app.engine
My project is using the handlebar as View Engine, so every .hbs that i call, it redirects to the main page.
app.engine('hbs', engine({
extname: 'hbs',
layoutsDir: path.resolve(__dirname, "./views/layouts"),
partialDir: path.resolve(__dirname, "./views/partials")
}))
app.set('view engine', 'hbs');
app.set('views', './views/');
But every time I try to render a new .hbs page, it redirects to the main page.
I tried with .handlebars files, because i needed a PUG and EJS page too and tried the same way to set them, but it doesnt work
app.set('view engine', 'handlebars');
app.set('view engine', 'pug');
app.set('view engine', 'ejs');
It throws:
Error: Module "handlebars" does not provide a view engine.
This is my first post, so any recommendations, will be appreciated!

How to render wordpress instead of default layout.hbs (handlebars) in Nodejs?

Builtin nodejs is using layout.hbs by default. But have to render wordpress instead of hbs
piece of code from app.js
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.set('env', config.env || 'development');
How can i change my default layout to wordpress?
A long way to render a wordpress page may be this:
Install the WordPress REST API plugin on your Wordpress site;
Recreate individual pieces of the wordpress template in hbs format;
On nodejs server, use the necessary wordpress APIs for composing the template and render it.

Does setting view engine in ExpressJS cause res.render() to only look in views folder for templates?

Does setting this
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
always cause
res.render([path]);
to start in the views directory? In other words, is the views directory always implied for res.render() after setting up the view engine.
Yes, that will make render look in 'views'
http://expressjs.com/4x/api.html#app.render
If you scroll down to application settings it will tell you the default setting for view is
process.cwd() + '/views'

How to use .html file extensions for handlebars in express?

So I was wondering how I could use .html extensions instead of .handlebars or .hbs extensions. I am doing this so I can develop using regular html so that my frontend developers can seamless edit files in their IDEs without any extra configuration. Plus it will help installing html templates much faster into our express applications.
So I was able to do this by changing three things in my app.js file I hope this helps everyone out as much as it helped me!
var express = require('express'),
exphbr = require('express3-handlebars'), // "express3-handlebars"
helpers = require('./lib/helpers'),
app = express(),
handlebars;
// Create `ExpressHandlebars` instance with a default layout.
handlebars = exphbr.create({
defaultLayout: 'main',
helpers : helpers,
extname : '.html', //set extension to .html so handlebars knows what to look for
// Uses multiple partials dirs, templates in "shared/templates/" are shared
// with the client-side of the app (see below).
partialsDir: [
'views/shared/',
'views/partials/'
]
});
// Register `hbs` as our view engine using its bound `engine()` function.
// Set html in app.engine and app.set so express knows what extension to look for.
app.engine('html', handlebars.engine);
app.set('view engine', 'html');
// Seperate route.js file
require("./routes")(app, express);
app.listen(3000);
Agree with JemiloII,
extname: '.myext' in the config while creating the expr-HBS instance (exphbr.create()) according to https://www.npmjs.org/package/express3-handlebars#-extname-handlebars-
binding the expr-HBS engine to the extension: app.engine('myext', handlebars.engine); according to http://expressjs.com/3x/api.html#app.engine
set the extension as view engine: app.set('view engine', 'myext'); - unfortunately no link to how it works.
Regards

Rendering `html` when using `jade` templating engine, in Express

How do you render an html file while keeping the templating engine as jade ?
app.set('view engine', 'jade'); is where i've set the templating engine as jade and I want to do something like
app.get('/world', function(req,res){
res.render('profile.html', );
To render the html file.
I'm programming in node.js using express.js.
Note: i've already require html using var html=require('html');
Edit:
I understand res.render need not be used as html is already rendered. res.send(profile.html); gives error of profile is undefined
You can render jade files (.jade) but not html files as the result will be the html. What do you mean by rendering the html file?
Replace your res.render('profile.html') by res.sendfile('[path_to_the_file]profile.html')
I think you can use consolidate and mustache templating engine
npm install consolidate mustache
var engines = require('consolidate');
app.engine('html', engines.mustache);
app.set('view engine', 'html');
if you want to test the .html files, the easiest way to do it is to temporarily change the engine and add consolidate and swig modules
npm install consolidate
npm install swig
cons = require('consolidate'),
app.engine('html', cons.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
Then you can simply render like this:
app.get('/', function(req, res){
res.render("index");
});

Resources