【问题标题】:handlebar partials not fully showing up in layout车把部分未完全显示在布局中
【发布时间】:2014-08-03 09:47:08
【问题描述】:

我无法让locations 数据显示在我的最终 HTML 输出中。发生的情况是,weather2.handleblocks 的 {{#each}} 块之外的所有 HTML 都显示得很好,但块内的所有内容都没有显示。 这是我希望在主布局中显示的数据。我用我的应用程序文件中定义的函数包装了数据。

function getWeatherData(){
    return {
        locations: [
        {
        name: 'Portland',
        forecastUrl: 'http://www.wunderground.com/US/OR/Portland.html',
        iconUrl: 'http://icons-ak.wxug.com/i/c/k/cloudy.gif',
        weather: 'Overcast',
        temp: '54.1 F (12.3 C)'
    },
    {
        name: 'Bend',
        forecastUrl: 'http://www.wunderground.com/US/OR/Bend.html',
        iconUrl: 'http://icons-ak.wxug.com/i/c/k/partlycloudy.gif',
        weather: 'Partly Cloudy',
        temp: '55.0 F (12.8 C)'
    },
    {
        name: 'Manzanita',
        forecastUrl: 'http://www.wunderground.com/US/OR/Manzanita.html',
        iconUrl: 'http://icons-ak.wxug.com/i/c/k/rain.gif',
        weather: 'Light Rain',
        temp: '55.0 F (12.8 C)'
        }
    ]
};
}

我将返回的对象分配给我的应用程序文件中的partials 中的matrix,如下所示:

app.use(function (req, res, next) {
if (!res.locals.partials) res.locals.partials = {};  
res.locals.partials.matrix = getWeatherData();      
next();                                          
});

名为weather2.handlebars的部分文件:

<div class="weatherWidget">
{{#each partials.matrix.locations}}
    <div class="location">
        <h3>{{name}}</h3>
        <a href="{{forecastUrl}}">
            <img src="{{iconUrl}}" alt="{{weather}}">
            {{weather}}, {{temp}}
        </a>
    </div>
 {{/each}}
 <small>Source: <a href="http://www.wunderground.com">Weather
        Underground</a></small>
</div>

这进入视图 home.handlebars:

<h1>Welcome to Meadowlark Travel</h1>
{{> weather2}}

最后是主布局 main.handlebars:

<!DOCTYPE html>

<html lang="en">
<head>

    <meta charset="utf-8" />
    <title>Meadowlark Travel</title>

</head>
<body>
    <header><img src="/img/cat.png" alt="Meadowlark Travel Logo"></header>
    {{{body}}}

</body>
</html>

编辑:这是最终输出:

<html lang="en"><head>
     <script src="/vendor/jquery-2.1.1.min.js"></script>
    <meta charset="utf-8">
    <title>Meadowlark Travel</title>

</head>
<body>
    <header><img src="/img/cat.png" alt="Meadowlark Travel Logo"></header>
    <h1>Welcome to Meadowlark Travel</h1>
<div class="weatherWidget">

 <small>Source: <a href="http://www.wunderground.com">Weather
        Underground</a></small>
</div>




</body></html>

我也无法正确显示部分(与部分类似的问题),但我改天再问。

编辑:一些附加信息:我正在使用最新的 express4 和车把 + express3-车把 https://www.npmjs.org/package/express3-handlebars

【问题讨论】:

  • 我不熟悉 express 但你需要在某个地方打电话给render,对吧?你也可以分享那部分代码吗?
  • render 将 home.handlebars 视图加载到主布局中。 weather2.handlebars 继续这个主视图app.get('/', function (req, res) { res.render('home'); });

标签: javascript express handlebars.js


【解决方案1】:

这就是我解决问题的方法:

在应用文件中已经定义的所有其他中间件之上定义中间件(我被提醒为什么代码中中间件的顺序非常重要,这要归功于这个问题)。

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    相关资源
    最近更新 更多