8000 Add namespace support to `source` function by willrowe · Pull Request #823 · twigjs/twig.js · GitHub
[go: up one dir, main page]

Skip to content

Add namespace support to source function #823

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

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Call expandNamesapce from parsePath
  • Loading branch information
willrowe committed Aug 30, 2022
commit c5cd6c4cef25b0336c3dc401e7f52b58847b6c1d
29 changes: 5 additions & 24 deletions src/twig.path.js
Original file line nu A7E5 mber Diff line number Diff line change
Expand Up @@ -34,36 +34,17 @@ module.exports = function (Twig) {
* @return {string} The canonical version of the path
*/
Twig.path.parsePath = function (template, _file) {
let k = null;
const {namespaces} = template.options;
let file = _file || '';
const file = _file || '';
const hasNamespaces = namespaces && typeof namespaces === 'object';

if (hasNamespaces) {
for (k in namespaces) {
if (!file.includes(k)) {
continue;
}

// Check if keyed namespace exists at path's start
const colon = new RegExp('^' + k + '::');
const atSign = new RegExp('^@' + k + '/');
// Add slash to the end of path
const namespacePath = namespaces[k].replace(/([^/])$/, '$1/');
let path = (hasNamespaces ? Twig.path.expandNamespace(namespaces, file) : file);

if (colon.test(file)) {
file = file.replace(colon, namespacePath);
return file;
}

if (atSign.test(file)) {
file = file.replace(atSign, namespacePath);
return file;
}
}
if (path === file) {
path = Twig.path.relativePath(template, file);
}

return Twig.path.relativePath(template, file);
return path;
};

/**
Expand Down
0