为什么我在Mustache中获取模板文件名而不是模板内容

时间:2013-04-22 16:19:53

标签: javascript node.js templates mustache hogan.js

我经历了Mustache tutorial

此问题与Embed mustache template into another template

略有关联

除了 partials

之外,其他所有工作都有效

我试着做

base.mustache:
{{testVar}}
<h2>Names</h2>
{{#names}}
  {{> user}}
{{/names}}

user.mustache:
<strong>{{name}}</strong>

但不是渲染用户模板或其内容即名称的值, 它呈现Names c:\blahFolder\user.mustache

我使用hogan-template-compiler来预编译我的模板。

你能弄清楚为什么会发生上述信息,或者你需要更多信息? 我假设,我的编译器代码很好,因为我正确地呈现了testVar值。

如果你真的需要看,这是我的 hoganCompiler.js

var partialsDir = __dirname + '/views/partials'
        , jsDir = __dirname + '/public/js/compiled';

var hogan = require('hogan.js')
        , path = require('path')
        , join = path.join

        , fs = require('fs')
        , onModify
        , relations = {}
        , watch;

onModify = function (filename)
{
    console.log('Changed', filename);
    if (relations[filename])
    {
        filename = relations[filename];
    }
    console.log('Which relate on', filename);

    var path, contents;

    path = join(partialsDir, filename);
    try
    {
        if (!filename.match(/\.mustache/) || !fs.statSync(path).isFile())
        {
            return;
        }
    }
    catch( err )
    {
        console.log( "Failed to stat file " + path + " - skipping");
        return;
    }

    contents = fs.readFileSync(path).toString();
    var templateName = filename.replace(/.mustache$/, '');
    // TODO: catch compiler exception..
    var compiledTemplate =
            templateName + " = new Hogan.Template("
                    + hogan.compile(contents, {asString: true}) + ");";

    var jsFilename = filename.replace(/mustache$/, 'js');
    fs.writeFileSync(join(jsDir, jsFilename), compiledTemplate );
}

watch = function (filename)
{
    if (relations[filename])
    {
        return;
    }

    var path;
    if (filename.charAt(0) == '/')
    {
        path = filename;
    }
    else
    {
        path = join(partialsDir, filename);
    }

//    fs.watch(path, function (event, f)
    fs.watchFile(path, function (event, f)
    {
        console.log('event is: ' + event);
        if( !f ) // fs.watch doesn't work on all platforms..
        {
            // fall back to just reprocessing all in dir
            fs.readdirSync(path).forEach(onModify);
        }
        else
        {
            onModify(f);
        }
    });
}

fs.readdirSync(partialsDir).forEach(onModify);
watch(partialsDir);


function getPartials(partials, dir)
{
    var baseDir = __dirname + '/views/';
    var files = fs.readdirSync(dir);
    for (var i in files)
    {
        if (files[i].match(/\.mustache/) && fs.statSync(join(dir, files[i])).isFile())
        {
            partials[files[i].replace(/.mustache$/, '')] =
                join(dir, files[i]).replace(baseDir, '');
        }
    }
    return partials;
}
var partials = {};
partials = getPartials(partials, partialsDir);
exports.partials = getPartials(partials, join(partialsDir, '..'));

0 个答案:

没有答案