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

Adds CLI support for partials with relative paths #558

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 30 additions & 3 deletions bin/mustache
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ function readPartials (cb) {
var partialPath = partialsPaths.pop();
var partial = fs.createReadStream(partialPath);
streamToStr(partial, function onDone (str) {
partials[getPartialName(partialPath)] = str;

var keysArray = getPartialNames(partialPath);

keysArray.forEach(function addPartialNames (key) {
partials[key] = str;
});

readPartials(cb);
});
}
Expand Down Expand Up @@ -131,6 +137,27 @@ function hasVersionArg () {
});
}

function getPartialName (filename) {
return path.basename(filename, '.mustache');
function getPartialNames (filename) {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repo's code style is not to have a blank line in the beginning of a function. It also feels inconsistent if the function doesn't end with a blank line as well, but that might be my opinion. (same for the onDone function)

// get path relative to the template file
// in order to use e.g. {{> ../common/footer }}

// before, {{> footer }} used the first -p file with the filename 'footer'

// the second replace is for a consistent form between platforms
// without it, you would have to use {{> ..\\common\\footer }} for windows
// and {{> ../common/footer }} for *nix

// this can be extended to the mustache API by using the relative path as
// the key in the partials object
// e.g. mustache.render(template, view, { '../../common/footer': '...' })
var relativePath = path.
relative(templateArg, filename).
replace('.mustache', '').
replace(/\\{1,2}/g, '/');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dots on the left, like pointed out in the other PR.


return [
path.basename(filename, '.mustache'),
relativePath
];
}