From 6de11ddc9f665aeb88a75dac144fea723fb08de6 Mon Sep 17 00:00:00 2001 From: Patrick Steele-Idem Date: Mon, 6 Jul 2015 10:08:21 -0600 Subject: [PATCH] Fixes #96 - Allow dynamic relative paths --- runtime/helpers.js | 7 ++++++- taglibs/core/core-tag-transformer.js | 1 + .../templates/include-dynamic-relative/expected.html | 1 + .../include-dynamic-relative/include-target.marko | 4 ++++ .../templates/include-dynamic-relative/template.marko | 2 ++ test/fixtures/templates/include-dynamic-relative/test.js | 1 + 6 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/templates/include-dynamic-relative/expected.html create mode 100644 test/fixtures/templates/include-dynamic-relative/include-target.marko create mode 100644 test/fixtures/templates/include-dynamic-relative/template.marko create mode 100644 test/fixtures/templates/include-dynamic-relative/test.js diff --git a/runtime/helpers.js b/runtime/helpers.js index a27263baa..4246f0ac2 100644 --- a/runtime/helpers.js +++ b/runtime/helpers.js @@ -117,8 +117,13 @@ module.exports = { /** * Loads a template */ - l: function(path) { + l: function(path, req) { if (typeof path === 'string') { + if (path.charAt(0) === '.') { // Check if the path is relative + // The path is relative so use require.resolve to fully resolve the path + path = req.resolve(path); + } + if (markoRegExp.test(path)) { return runtime.load(path); } else { diff --git a/taglibs/core/core-tag-transformer.js b/taglibs/core/core-tag-transformer.js index cf52e34bc..1f651ebbf 100644 --- a/taglibs/core/core-tag-transformer.js +++ b/taglibs/core/core-tag-transformer.js @@ -398,6 +398,7 @@ module.exports = function transform(node, compiler, template) { attr, 'string', attrDef) + + ', require' + // Include the "require" variable to allow relative paths to be resolved ')'); } else { // Resolve the static string to a full path only once diff --git a/test/fixtures/templates/include-dynamic-relative/expected.html b/test/fixtures/templates/include-dynamic-relative/expected.html new file mode 100644 index 000000000..ba72408b2 --- /dev/null +++ b/test/fixtures/templates/include-dynamic-relative/expected.html @@ -0,0 +1 @@ +Hello Frank! You have 20 new messages. \ No newline at end of file diff --git a/test/fixtures/templates/include-dynamic-relative/include-target.marko b/test/fixtures/templates/include-dynamic-relative/include-target.marko new file mode 100644 index 000000000..49f7c93cf --- /dev/null +++ b/test/fixtures/templates/include-dynamic-relative/include-target.marko @@ -0,0 +1,4 @@ + + + +Hello $name! You have $count new messages. \ No newline at end of file diff --git a/test/fixtures/templates/include-dynamic-relative/template.marko b/test/fixtures/templates/include-dynamic-relative/template.marko new file mode 100644 index 000000000..b55ff2655 --- /dev/null +++ b/test/fixtures/templates/include-dynamic-relative/template.marko @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/test/fixtures/templates/include-dynamic-relative/test.js b/test/fixtures/templates/include-dynamic-relative/test.js new file mode 100644 index 000000000..c4013b344 --- /dev/null +++ b/test/fixtures/templates/include-dynamic-relative/test.js @@ -0,0 +1 @@ +exports.templateData = {};