How to use JavaScript code inside templates?
See original GitHub issueI 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
<body>
<div class="{% if(isMobile()) { %}mobile-class {% else{ %} desktop-class {% } %}">
<!-- content here -->
</div>
</body>
Please help.
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
The Content Template element - HTML - MDN Web Docs
The <template> HTML element is a mechanism for holding HTML that is not to be rendered immediately when a page is loaded but...
Read more >Run JavaScript code in your templates
Run JavaScript code in your templates. Write simple JavaScript code into your templates to automate your note-taking, such as "Moving tasks ...
Read more >Explaining Javascript Templates and How We're Using Them ...
Popularized by John Resig, Javascript templating is a fast and efficient technique to render client-side templates with Javascript using a JSON ...
Read more >Best Practices When Working With JavaScript Templates - Code
All JavaScript templates obviously start out as text. The most popular (and natural) place to store them is within your HTML document -...
Read more >JavaScript Template Literals - W3Schools
Back-Tics Syntax · Quotes Inside Strings · Multiline Strings · Interpolation · Variable Substitutions · Expression Substitution · HTML Templates · Browser Support....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Usually most of the logic is embedded in code, not templates. Compared with many other template engines, Nunjucks is quite powerful.