block/template/extend support for ejs
See original GitHub issueCurrently ejs doesn’t have any block/template/extend features.
existing solutions:
https://github.com/seqs/ejs-blocks
neat javascript grammar, however it only support raw strings as block content.
https://github.com/tj/ejs/pull/142 https://github.com/User4martin/ejs/blob/plugin-snippets/docs/plugin-snippet.md
they invents several preprocessor directives like
<%block header%>
,<%/block header%>
<%+ template%>
,<%* snippet name %>
,<%* /snippet %>
, thus not very easy to learn, this is against ejs’s design goals.
this approach:
page implementation (home.ejs):
<!-- define block contents by functions, it should be able to access the same locals data & context -->
<% var head = () => { %>
<%- include('./include.css') %>
<title>Hello EJS Template</title>
<% } -%>
<% var body = () => { %>
<div>
you have an message: <%= message.toLowerCase() %>
</div>
<% } -%>
<!-- a single "include" finally, and its contents are passed by locals -->
<%- include('./layout', {body, head}) %>
template/layout declaration (layout.ejs):
<!-- NOTE: template/layout can be nested -->
<html>
<head>
<% if (!head) { %>
<title>default title</title>
<% } else { %>
<!-- NOTE: this is the only one thing changed for ejs users, ejs "include" function now accept function as its first argument -->
<%- include(head) %>
<% } %>
</head>
<body>
<h1>This is a layout</h1>
<% if (!body) { %>
<small>default content</small>
<% } else { %>
<!-- same above -->
<%- include(body) %>
<% } %>
</body>
</html>
advantages
- pure javascript gramma, with ES6 arrow function, the code looks nice too
- it breaks nothing
- like the original “include”, it can be nested
- functions can have its parameters, so include can handle function-local variables as well as context variables, example: https://github.com/mde/ejs/issues/252#issuecomment-428576331
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:16 (2 by maintainers)
Top Results From Across the Web
EJS language support - Visual Studio Marketplace
Extension for Visual Studio Code - 2019 - EJS language support for Visual Studio ... Syntax highlighting for EJS, Javascript, and HTML tags....
Read more >EJS -- Embedded JavaScript templates
EJS is a simple templating language that lets you generate HTML markup with plain JavaScript. ... We're happy to answer your questions or...
Read more >Visual Studio Code isn't recognising EJS - Stack Overflow
Finally, I found the cause of this problem. Foremost of all, I installed the EJS language support extension ...
Read more >How To Use EJS to Template Your Node Application
Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer...
Read more >Using EJS Template Engine With Express.js - Topcoder
Iterators: Iterators are something that help to loop over different objects in Python. Iterator goes over all ... Read More ...
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
One way to use extends/block in existing versions:
page.ejs
base.ejs
This is why I like ejs