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.
Related
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.
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’m building an electron app. In it, I have a webview with a preload script. Inside said script, I’d like to use sweetalert.
I installed sweetalert with npm install --save sweetalert. Inside my script I load it with require('sweetalert') and call it with swal("Hello world!");. I now notice it doesn’t look right, as the alert is missing its required CSS file. But I’m loading it with require('sweetalert'), which is great since sweetalert can just remain in its directory inside node_modules and I don’t have to care for it, but its CSS is an integral part of it, and is not getting pulled the same way.
Now, what is the recommended way of solving this? Keep in mind I’m inside a javascript file and would like to remain that way. Do I really have to go get the CSS file and inject it in some way? And how would I do it correctly, since it is inside node_modules? After testing it, it seems like it can’t be done in this particular case due to Content Security Policy.
Either way, that seems so clunky in comparison to the require statement, it’d seem weird for a simpler solution to not be available.
You'll have to include it like you would normally do in a browser, for example in index.html. Copy it out of the module folder into your css folder if you have one and link it with the link tag. It depends on if you're using plain electron or some other boilerplate template with there is a gulp/grunt workflow on where to stick it but that's it really, electron is just a browser that's running your JS/html so it's really the exact same process. require only loads the JS module but not the styles.
if you wanted to include it dynamically you could use the same techniques as a regular browser for example (ex. document.write/create element).
I'm not familiar with sweetalert, but hopefully this helps.
Your syntax for require should be something similar to this.
var sweetalert = require('sweetalert')
You should then be able to access methods on the sweetalert object using the following syntax.
sweetalert.someMethod()
Remember requiring just returns a javascript object. Those objects usually have methods that will allow certain functionality. If you want to add sweetalert to your page, you will either need to inject it within the html, or the javascript within the sweetalert module will need to dynamically create html where the css is included. I hope that clarifies some things and helps you get a better sense of some of the inner workings.
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).