Skip to content
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

Added support extends, block, and include. #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions Readme.md
Expand Up @@ -18,6 +18,8 @@ Embedded JavaScript templates.
* Filter support for designer-friendly templates * Filter support for designer-friendly templates
* Client-side support * Client-side support
* Newline slurping with `<% code -%>` or `<% -%>` or `<%= code -%>` or `<%- code -%>` * Newline slurping with `<% code -%>` or `<% -%>` or `<%= code -%>` or `<%- code -%>`
* Layouts with <% extends foo.html %> and <% block block_name %>
* Mixins with <% include %>


## Example ## Example


Expand Down Expand Up @@ -55,6 +57,58 @@ Which would make the following a valid template:


<h1>{{= title }}</h1> <h1>{{= title }}</h1>


## Layouts and Blocks

Layout for a view can be specified by using :

<% extends layout.html %>

This will insert the contents of layout.html at the location of the extends
statement. Extends are used with blocks to provide dynamic layout capabilities
with default behaviour.

For example, consider the following layout.html:

<div id="has-default">
<% block block1%>
<p>This is default value</p>
<% end %>
</div>
<div class="no-default">
<% block block2%>
<% end %>
</div>

In your ejs file, specifying:

<% extends layout.html %>
<% block block2 %><p>Block 2 value</p><%end%>

would cause the following to be rendered.

<div id="has-default">
<p>This is default value</p>
</div>
<div class="no-default">
<p>Block 2 value</p>
</div>

In the above _block1_ has a default value defined, if _block1_ is not passed in
the default value would be used. However, if _block1_ was specified, then it
would override the default value.

Please note that blocks must be terminated with an _end_ statement.

## Mixins

This effectively replace partials() from Express 2.x. You can include partiral
ejs by specifying:

<% include component.html %>

This would insert the contents of _component.html_ at the location of the include
statement.

## Filters ## Filters


EJS conditionally supports the concept of "filters". A "filter chain" EJS conditionally supports the concept of "filters". A "filter chain"
Expand Down Expand Up @@ -121,6 +175,7 @@ ejs.filters.last = function(obj) {
}; };
``` ```



## client-side support ## client-side support


include `./ejs.js` or `./ejs.min.js` and `require("ejs").compile(str)`. include `./ejs.js` or `./ejs.min.js` and `require("ejs").compile(str)`.
Expand Down