3

我经历了 Mustache教程

这个问题与Embed mustache template into another template略有关系

我得到了所有其他的东西,除了部分

我试着做

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

0 回答 0