Add support of extend and block in EJS #142

wants to merge 4 commits into

@visionmedia, fyi:
Add features of extend and block, which could support
(1) multilayer inheritance, fully support layout in EJS.
(2) Also could work with include.
The examples and test has also been add!

ictliujie added some commits Oct 14, 2013
@ictliujie ictliujie Add support of extend and block in EJS
add features of extend and block, could support multilayer inheritance,
could fully support the layout in EJS. Also could work with include. The
examples and test has also been add!
@ictliujie ictliujie modify the descriptions of extend in the ffba40a
@ictliujie ictliujie Add the comparison between extend and include
the test in benchmark shows that the layout using extend is 33% faster
than that using include

How to use the extend and block feature?


Oh, I found it.
Extend file with <%+ file-to-be-extended %>
Blocks <%block blockname%>content comes here<%/block%>, cooperate with extend


@welkang, yes, that is the way. You could also refer to the examples in the source code. Have fun

@tj tj commented on the diff Nov 26, 2013
@@ -112,7 +119,55 @@ var parse = exports.parse = function(str, options){
, filename = options.filename
, compileDebug = options.compileDebug !== false
, buf = "";
+ //the extend symbol must be in the first place of the file if exist
+ if(str.trim().indexOf(open + "+") == "0"){
tj added a line comment Nov 26, 2013

the rest looks fine and the tests are good but we'd definitely want to try and make this chunk more declarative it's not super obvious and it's a lot to cram into the one function

ictliujie added a line comment Nov 28, 2013

yes, you are right. I used too many temp variables in the function. I'll spend my weekend to optimize the code, make it more declarative. we can talt about it later.
The extend feature is very useful and necessary. So I advise strongly that it could be merged into the trunk sooner or later. thanks!

tj added a line comment Nov 28, 2013

there's also #147 which is similar, it would be great if you could maybe check out that PR and help review :D at a glance they both look really similar

ictliujie added a line comment Nov 29, 2013

ok, I'd like to :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ictliujie ictliujie reorganize the code related to extend
reorganize the code related to extend , make it more declarative

I am excited about this feature returning to EJS, but I'd like to point out that 'extend' and 'layout' are really two different features. With 'layout', you are requesting that the entire content of the current view be wrapped in a particular view. With 'extend', you are instead defining certain blocks that will override the corresponding blocks in the extended view. While possible to do with both, generally extended views can stack many generations deep, while layouts typically don't. While I would love to see both features in EJS, I think it's important to recognize the commonalities and differences between them. Both this and #147 seem to be describing 'extend', but #147 refers to it as 'layout'.

Also, while I'm here, I may as well mention a feature with 'extend' that I've seen (and love) in other template engines: the ability to supplement content in a block, either by explicitly defining 'prepend' or 'append' in the tag or by defining a sub-tag within the block that defines where the ancestor's content should live.


yes, I agree that extend and layout are two different features. And in practice,
(1) we usually use the feature of extend to implement the layout
(2) I don't recommend that multi-layer extend in EJS(or any other template engine), evet if it has been supported. Actually 2-3 layers is enough, which is more clear and efficient.
(3) prepend & append are good features in extend. Considering the great Filter functions in EJS has come up to the two features, I don't realize them in extend.



@AdamPuzio You're right, #147 is describing "extend" as well. My use of the term layout is just my own improper use of terminology. I'll try to stop that to avoid confusion :-).

I don't see the layout feature you describe as anything more than a specialized use case of extend. I agree with @ictliujie that I don't typically recommend more than 2-3 layers in extend, though maybe your needs do call for it.

I like the idea of prepend/append, and @ictliujie notion of using Filters seems promising. I'll give it some thought for #147. @visionmedia Is this something you would want in an initial launch of templates?


Any news on this?



@visionmedia plans to merge this?






@visionmedia It's been a while since EJS has received any development attention. Are you going to allow people to start taking over this project or is EJS dying?


Hey guys. I found the extend and block feature I wanted here:
Just do: npm install ejs-locals
layout = extend
block = block


@freeauto is the also a block append / prepend feature?
i'm looking for it since weeks.


@ilanbiala I see many pull request waiting for months. I'm going to find another template node module, EJS is clearly not good enough, there is not even what are the basics for me, coming from Ruby I have the feeling that I can't work with EJS.


@Vadorequest, I am the new maintainer of this module, and I hope to get things moving a bit faster once I've familiarized myself more with the codebase.

I am also the maintainer of the Geddy Web app framework for Node (, which has its own bundled EJS implementation ( that also has a couple of features missing here. I hope you'll be patient and give me a chance to ramp up.

As far as "being from the Ruby world," EJS is almost precisely the same as ERB, which was the default in Rails for years. (I haven't followed to see if they've switched to HAML recently or not.)


@mde Nice! Do you have any idea about what you will start to add/fix in EJS?




Very interested in this particular functionality. +1

Just to add to the discussion does some of this as well.


@Vadorequest @mde nothing's moved since May 1st, when TJ made the last commit. I've pretty much moved away from EJS, and there are plenty of other template engines out there.

@Vadorequest Vadorequest referenced this pull request in mde/ejs Mar 21, 2015

Support for blocks #78


what is the progress on this?

@dev-rke dev-rke referenced this pull request in lega911/angular-light Jan 27, 2016

Templates #131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment