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.
Related
I want to use a variable provided from my CMS to reference an include template like this:
extends layout
include layouts/#{data[0].fields.layout}.pug
This just looks for layouts/#{data[0].fields.layout}.pug rather than what I wan, e.g. main.pug. Is there a way to do this?
Unfortunately, this is not possible at the moment. Dynamic includes are currently not supported by Pug (see this SO answer and this Github issue).
I want to build a blog with krakenjs and want to use the i18n feature from it (makara): https://github.com/krakenjs/makara
Makara requires to put all locales into static files. eg. locales/US/en/
How can I use it with dynamic content from e.g. contentful.com
Here's the excerpt from the makara readme that explains how to do this:
If you have runtime values to be inserted, use dust brace to select the value from the dust template context as in the index.greeting line. Note that there is no restriction on inserting HTML tags into the messages. They are just another string of characters as far as the content processing is concerned.
So you would have a property like so:
index.dynamic={dynamicConent}
The value of dynamicContent will be picked up at run-time from the dust template context.
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.
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).
I'm using Require.js and the text plugin. I'm trying to create a template that has something like this:
<%= somefunction(displayvalue) %>
I need the somefunction(...) to be available to multiple templates. How is the best way to wrap the function to use in multiple places? I was thinking of making it a module, but I don't know how to pass it to a template.
After you compile a template, you find yourself with a function to call, and you pass in some arguments. So just pass your function as argument:
var tpl = _.template( tplString );
tpl({ someFunction: function( val ) { /* do something */ } });
Although, I think you're probably better if you only pass value inside your template data. Template are actually way easier to debug and maintain if they're almost logic less. So, instead, I'd go like this:
tpl({ someValue: someFunction( aValue ) });
In other javascript templating engine (like Handlebars), you can actually register helpers functions who'll be mostly available globally to execute action on your template data. If you really need to use the same function inside multiple templates, I'd think about switching template engine. Underscore provide a micro-templating engine, and as so, is somehow limited around helpers functions - although being able to contain way too much logic...
In my opinion, underscore template works well for small project and should be use carefully. Keep them as simple as possible: if/else, printing data, and that should be it. If you need more, go for a more complete template engine.
But even there, all logic you do inside a template is hard to debug.
So! Keep it simple.