-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use JavaScript code inside templates? #655
Comments
index.nunjucks <body>
<div class="{% if isMobile() %}mobile-class{% else %}desktop-class{% endif %}">
<!-- content here -->
</div>
</body> Then pass the function object to your environment's env.render( 'index.nunjucks', { isMobile: isMobile } ); |
@ricordisamoa Thanks. This looks like an hack to me. Why can't I use JavaScript code in a straight way? compiling is happening at runtime then definitely I must be able to use JavaScript straightforward right? (just like lodash templates) It is just a doubt I have. :) |
|
hmm seems like a drawback for this template engine. Thanks. |
Usually most of the logic is embedded in code, not templates. Compared with many other template engines, Nunjucks is quite powerful. |
I do completely see the elegance of not having code logic inside your templates and for the majority of cases this is great. However, it would be nice if there was a specific tag or escape syntax that would allow you to evaluate code for the particular implementation's backing language, in this case JavaScript where nunjucks gets out of the way and just says, OK do your thing, then comes back. For example, something simple like {{ eval( 'new Date()).getFullYear()') }} so it was intentionally inconvenient to write inline yet for small things, it made sense. The environment has the ability to evaluate string of code and requires escaping as well as being on a single line which promotes very tiny evaluations and makes long lines of inline code unmaintainable and ugly. |
I want to create a ternary operator, like in Twig, where you can say Here is the Twig syntax of what I'm trying to accomplish in the code example below: This is the code I have set up in my nunjucks:
I've tried setting it up like so: I have two partials included in my index.njk file. If I set bg_color on the first partial... the second partial seems to inherit the styles from the partial before it. I just want to be able to have a default background color of white for all partials, unless I set the bg_color variable.
|
I was wondering whether there is a way to manipulate the template using JavaScript functions or statements inside nunjucks templating.
Suppose, for example, I have a nunjucks file where I need to apply different classes based on resolution and I have already have a function
isMobile()
which I am using in many places in JS files. So, I thought to use the same function inside the template file. something as shown below:index.nunjucks
Please help.
The text was updated successfully, but these errors were encountered: