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

includes now accept nested/paths or "absolute/.../paths" #157

Open
wants to merge 4 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ Embedded JavaScript templates.
<% include user/show %>
<% }) %>
</ul>
```

Starting with "/" will drive you to the absolute path.

```
<ul>
<% users.forEach(function(user){ %>
<% include /views/user/show %>
<% }) %>
</ul>
```

## Custom delimiters
Expand Down
9 changes: 6 additions & 3 deletions ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ var parse = exports.parse = function(str, options){
if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
if (!filename) throw new Error('filename option is required for includes');
var path = resolveInclude(name, filename);
var path = resolveInclude(name, filename, options.baseDir);
include = read(path, 'utf8');
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
buf += "' + (function(){" + include + "})() + '";
Expand Down Expand Up @@ -311,6 +311,7 @@ var compile = exports.compile = function(str, options){
* - `locals` Local variables object
* - `cache` Compiled functions are cached, requires `filename`
* - `filename` Used by `cache` to key caches
* - `baseDir` Used by `resolveInclude` to absolute paths
* - `scope` Function execution context
* - `debug` Output generated function body
* - `open` Open tag, defaulting to "<%"
Expand Down Expand Up @@ -379,8 +380,10 @@ exports.renderFile = function(path, options, fn){
* @api private
*/

function resolveInclude(name, filename) {
var path = join(dirname(filename), name);
function resolveInclude(name, filename, baseDir) {
name = name.replace(/(\'|\")/ig,"");
if (name[0] === '/') var absolute = (baseDir || process.cwd());
var path = join((absolute || dirname(filename)), name);
var ext = extname(name);
if (!ext) path += '.ejs';
return path;
Expand Down
9 changes: 6 additions & 3 deletions lib/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ var parse = exports.parse = function(str, options){
if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
if (!filename) throw new Error('filename option is required for includes');
var path = resolveInclude(name, filename);
var path = resolveInclude(name, filename, options.baseDir);
include = read(path, 'utf8');
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
buf += "' + (function(){" + include + "})() + '";
Expand Down Expand Up @@ -259,6 +259,7 @@ var compile = exports.compile = function(str, options){
* - `locals` Local variables object
* - `cache` Compiled functions are cached, requires `filename`
* - `filename` Used by `cache` to key caches
* - `baseDir` Used by `resolveInclude` to absolute paths
* - `scope` Function execution context
* - `debug` Output generated function body
* - `open` Open tag, defaulting to "<%"
Expand Down Expand Up @@ -327,8 +328,10 @@ exports.renderFile = function(path, options, fn){
* @api private
*/

function resolveInclude(name, filename) {
var path = join(dirname(filename), name);
function resolveInclude(name, filename, baseDir) {
name = name.replace(/(\'|\")/ig,"");
if (name[0] === '/') var absolute = (baseDir || process.cwd());
var path = join((absolute || dirname(filename)), name);
var ext = extname(name);
if (!ext) path += '.ejs';
return path;
Expand Down
2 changes: 1 addition & 1 deletion test/ejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ describe('includes', function(){
.should.equal(fixture('include.html'));
})

it('should work when nested', function(){
it('should work when nested or absolute', function(){
var file = 'test/fixtures/menu.ejs';
ejs.render(fixture('menu.ejs'), { filename: file, pets: users })
.should.equal(fixture('menu.html'));
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/menu.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@

<% var url = '/baz' -%>
<% var title = 'Baz' -%>
<% include includes/menu-item -%>
<% include includes/menu-item -%>

<% var url = '/boz' -%>
<% var title = 'Boz' -%>
<% include "/test/fixtures/includes/menu-item" -%>
3 changes: 2 additions & 1 deletion test/fixtures/menu.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<li><a href="//foo">Foo</a></li>
<li><a href="//bar">Bar</a></li>
<li><a href="//baz">Baz</a></li>
<li><a href="//baz">Baz</a></li>
<li><a href="//boz">Boz</a></li>