Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Included files from the variables #86

Closed
wants to merge 1 commit into from

8 participants

@Zig1375

include var_name
include user/show.ejs

@Zig1375 Zig1375 Include files by variable
include var_name
include user/show.ejs
515b210
@ForbesLindesay

The problem is that the options given to parse are not the same as the locals given to render. It's perfectly acceptable to cache the results of parsing and compiling a function. In addition to that, it wouldn't obey the context rules of JavaScript, which could be confusing:

<% var temp = 'foo' %>
<% include temp %>

The temp would not count as being defined because it's not part of options.
@phplego

For support <% include var_name %>
I suggest this simple solution.
I suggest change this code in lib/ejs.js:

  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);
    include = read(path, 'utf8');
    include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
    buf.push("' + (function(){" + include + "})() + '");
    js = '';
  }

To this code:

  if (0 == js.trim().indexOf('include')) {
    var name = js.trim().slice(7).trim();
    if (!filename) throw new Error('filename option is required for includes');
    // If it is not path, but variable name (Added)
    if(options[name])
         var path = resolveInclude(options[name], filename);
    else
         var path = resolveInclude(name, filename);
    include = read(path, 'utf8');
    include = exports.parse(include, options); // Added transfer whole options
    buf.push("' + (function(){" + include + "})() + '");
    js = '';
  }

What do you think about it? It works fine for me...

@tj
Owner
tj commented

this sort of thing would only work while caching is disabled, since the template function generated is not idempotent. The caching mechanism in Express would have to be more complex to allow the engine to define what identifies the template vs the simple filename caching we have now

@phplego

Okay, what about put in that place a function like include() which result are also cached?
Or how can we use dynamic include in other way?

@medanat

+1

@tj tj closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 13, 2012
  1. @Zig1375

    Include files by variable

    Zig1375 authored
    include var_name
    include user/show.ejs
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +2 −0  lib/ejs.js
View
1  .gitignore
@@ -2,3 +2,4 @@
*.sw[a-z]
vim/.netrwhist
node_modules
+.idea
View
2  lib/ejs.js
@@ -154,6 +154,8 @@ var parse = exports.parse = function(str, options){
if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
+ if (options[name]) name = options[name];
+
if (!filename) throw new Error('filename option is required for includes');
var path = resolveInclude(name, filename);
include = read(path, 'utf8');
Something went wrong with that request. Please try again.