Comments (3)
This sounds like it could be a pretty cool feature. EJS doesn't have blocks per se, so I've been looking at options for adding equivalent structure. Would be great to see how you're thinking about implementing.
Do you have time to put together a pull request?
from grunt-ejs-static.
Not sure about pull request right now, sorry :(
I've found http://nunjucks.jlongster.com/ template engine that already has perfect template inheritance and extending. Nunjucks fits my needs, so I'm adapting to use it. Maybe EJS itself should have extending feature, shouldn't it? Or you're going to implement this absent feature only in grunt-ejs-static task?
from grunt-ejs-static.
Thanks for the feedback Oleg! Took a look at how Nunjucks handles inheritance http://jinja.pocoo.org/docs/templates/#template-inheritance
In Nunjucks, the base template contains blocks, which are chunks of the template that child templates can override.
The same thing can be accomplished with this project, though the implementation is different. The equivalent to the base template is the layout file, which can be re-used multiple times.
First, create a layout file default.ejs
<% include ../templates/global/head %>
<% include ../templates/global/header %>
<% include ../templates/variable_content/router %>
<% include ../templates/global/footer %>
<% include ../templates/global/scripts %>
<% include ../templates/global/foot %>
Next create a router template router.ejs
. EJS does not seem to like switch statements, return, or if elses, so assigning file_name to false is used instead of of one of those options.
// case 1
<% if (file_name === 'contact') { %>
<% include ../contact/body %>
<% file_name = false; %>
<% } %>
// end case 1
// case 2
<% if (file_name === 'about') { %>
<% include ../about/body %>
<% file_name = false; %>
<% } %>
// end case 2
// default case
<% if (file_name) { %>
<% include ../variable_content/default_content %>
<% } %>
// end default case
The variable file_name is the key for each file which is defined in the main data file (in the demo, data/data.json). For example, in data.ejs
, you would define the layout like this
"contact": {
"path_to_layout": "demo/dev/layouts/default.ejs",
"meta": {
"title": "Contact",
"description": "Compile static html from ejs templates",
"keywords": "gruntplugin, ejs, static html, static site generator"
}
},
Thanks again and good luck with your project!
from grunt-ejs-static.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grunt-ejs-static.