瀏覽代碼

Adds CLI support for partials with relative paths

pull/558/head
kookookchoozeus 10 年之前
父節點
當前提交
a123e94331
共有 1 個檔案被更改,包括 30 行新增3 行删除
  1. +30
    -3
      bin/mustache

+ 30
- 3
bin/mustache 查看文件

@@ -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);
});
}
@@ -131,6 +137,27 @@ function hasVersionArg () {
});
}

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

// 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, '/');

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

Loading…
取消
儲存