ejs icon indicating copy to clipboard operation
ejs copied to clipboard

Add functionality for dynamic includes #93

Open jasper-lyons opened this issue 11 years ago • 11 comments

As per the issue raise #93 and with some amendments to corrects the suggest fix. Variables can now be used with includes so that the contents of the variable is evaluated to a string and then the include statement is evaluated as normal with the value of the string as the file name (without extension or path) to search for.

jasper-lyons avatar Jan 18 '14 11:01 jasper-lyons

+1 Why this very old issue still has not solved by this solution? And, yes, this pull request has an issue. Variable "path" is declared twice. You need just to remove "var" keyword inside the "if" statement

phplego avatar Jun 25 '14 17:06 phplego

you'd have to pass it via options? seems a little weird, unless I'm misreading that, but it's not that simple, need caching etc, I haven't seen a good solid solution yet that's why there's no merge

tj avatar Jun 25 '14 18:06 tj

The var path isn't declared twice, only once since the code goes either in the if or else statement. That's not an issue. To make things look better we also could declare the var path; before the if statement.

Edit: Seems like I'm wrong:

This would be because of javascript variables all being hoisted to the top of the scope and, as an if statement doesn't create a new scope, it is in fact defined twice.

Vadorequest avatar Jul 19 '14 07:07 Vadorequest

Is there a reason this can't be as simple as changing var path = resolveInclude(name, filename); to var path = resolveInclude(options[name] ? options[name] : name, filename); and leave off the options change? Am I missing something obvious?

jjackoway avatar Aug 04 '14 17:08 jjackoway

so, without this merge or one like it, is there a way to get dynamic include paths in ejs? I need to do includes based on a portion of the path being dynamic, and have wired ejs DEEP into my app's architecture.

nikmartin avatar Sep 24 '14 02:09 nikmartin

I'd be happy to merge this PR or one like it if it doesn't break existing tests. I'm taking a look at it now, buit the merge might go faster if the authors of the PR get tests passing before submitting it.

mde avatar Dec 30 '14 21:12 mde

@mde I've fixed this PR so that existing test pass while still providing the required functionality. If there are any other changes you'd like me to make, just let me know. :)

jasper-lyons avatar Dec 31 '14 16:12 jasper-lyons

@jasper-lyons I'm probably missing something, but I'm not clear on what that block of code with the switch is for in this last commit.

I made a simpler change yesterday in the master branch which passes all the tests, and added a test for the variable-based include. Can you take a look at it and see if it handles what you'd say the uses-cases are for this?

mde avatar Dec 31 '14 17:12 mde

@jasper-lyons Ah, I was looking at the changes to the built file. Looks like your change is pretty much what I implemented in master. Can you verify if it works for you?

mde avatar Dec 31 '14 18:12 mde

I can verify that it does and passes all test for me.

I added a simple test case here at line 304-308 with some fixtures, simply outlining an automated test for the use-case.

I would be happy to work on future extensions to this functionality if required.

jasper-lyons avatar Dec 31 '14 19:12 jasper-lyons

@jasper-lyons, I am doing a version 2.0 (using my preexisting EJS implementation from Geddy and previous) that handles includes as a simple function call at runtime: https://github.com/mde/ejs Could you take a look at it and let me know if it works for you? I believe I've gotten parity with all the existing functionality -- except filters, which I'd really like to deprecate.

mde avatar Jan 02 '15 21:01 mde