Concatenating Partial Path in Jade Template - node.js

I have a Jade template on an Express project that looks something like this (simplified for brevity):
!!! 5
head
...
body
include path/to/partial
What I am trying to figure out is dynamically generate the partial's path so that I could easily swap out the partial based on a server variable. Since path/to/partial isn't a string, is there a way to provide the concatenation?

No way to do that with Jade, it's static (and by design as per TJ).

Related

how to give dynamic route path in include in pug template

want to make dynamic include
example-
h1
include path/#{object}
or
include path/+{object}+{a:true,b:11}
something like above and if any one know how to do with Mixins in pug please give an example for include
A dynamic path for extends, include is not possible, because the pug compiler expects the path as a string rather than interpolating the path.

How to pass variables to layout from handlebars?

I am using handlbars with express/nodejs, and is possible to pass variables individually to the views, but i dont find a way to pass to general layout. How i can do this ?
What is general layout for you?)
As I know Handlebars is a templates engine.
We have a template, we pass variables to it, we got a string with hardcoded variables inside. Templates don't have common scope or something like this. Template engine just helps you to fill string content easier. (html is a string)
But you can create a scope inside of your code and use the same variables for different templates.

Node.js & Express: template engine for plain html code

Using Node.js and express in a MEAN environment, I am looking for a simple and straightforward template engine, meeting these requirements:
does not dictate me to only use its own weird syntax but allows me to keep writing webpages using pure/plain html and js
supports conditional includes
works with express
operates on server-side (Node.js/Express)
executes freakin' fast ;)
Basically I just want to slice my webpage into several modules (e. g. header, footer, ...) and include those now and then based on simple conditions. I don't want to entirely (re-)rebuild all webpages using a proprietary template language but rather prepare a few html modules that I concatenate at runtime (comparable to PHP where I just use the include instruction to paste prepared html code).
I had a look at http://garann.github.io/template-chooser/ and https://github.com/nodejs/node-v0.x-archive/wiki/modules#templating but the sites seem outdated and according to them, there ain't no template engine available fully meeting my requirements!?
Suggestions anyone?
I think ejs is more natural for what you are looking for https://scotch.io/tutorials/use-ejs-to-template-your-node-application, but jade can work also. The example in the link uses partials, which you dont need to use if ur just rendering a single page

Custom HTML in Jade Template

I'd like to accomplish something in jade, but not sure on the best approach. I want to have several product pages on a website, and most of these product pages will have the same layout and design, so I'm going to create one jade template. However, I would love to be able to insert some custom HTML for a couple of product pages in particular.
Can I use an include or something to optionally add custom HTML?
You could use jade partials to show different snippets depending on some conditions in the templating context:
if user.description
!=partial(template name[, options])
Or you might use template inheritance and render a different template depending on the same conditional, but this time in your controller. I would probably do the latter.

Handlebars with Express.js - Should I transform raw data in templates or elsewhere?

I'm going to make a web app using Express, and Handlebars seems to be a good templating engine choice, but I wonder where raw JSON data should be made human-readable: in the templates or in actual JS code?
Let's say I have this piece of JSON:
{
raw_item_name: "Whatever",
item_quality: 2
}
item_quality could represent "Unusual". The human-readable version of this JSON would be "Unusual Whatever", and that's what I want the end user to see on the page. My question is, should I pass this JSON directly to the template and use helpers to transform it, or transform it elsewhere and pass the final string to the template to have it directly display it?

Resources