Jade模板似乎无法从Express获取数据 [英] Jade template does not seem to be getting data from Express

查看:43
本文介绍了Jade模板似乎无法从Express获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在get处理程序中,我从数据源创建一个对象 plans .然后我这样做:

In the get handler, I create an object, plans, from my data source. Then I do:

console.log(plans);
res.render('dashboard', plans);

它打印:

{ '0': 'mobile_basic',
  '1': 'mobile_basic',
  '2': 'landline_basic',
  '3': 'landline_mid',
  '4': 'internet_mid' }

这意味着该对象正在传递到模板中.在 dashboard Jade模板中,我有:

This means that this object is being passed into the template. In the dashboard Jade template, I have:

if plans
  ul
    each val, index in plans
      li= index + ': ' + val

这永远不会出现.在模板的上下文中未定义 plans .我想念什么?

This never shows up. plans is undefined in the template's context. What am I missing?

P.S.我尝试使用 res.render('dashboard',{data:plans}); 并将模板中的 plans 替换为 data.plans .仍然不起作用.

P.S. I have tried using res.render('dashboard', {data: plans}); and replacing plans in the template with data.plans. Still doesn't work.

推荐答案

保存数据的变量的名称实际上并未通过 res.render()传递给视图引擎.只有其值有效.

The name of the variable holding your data doesn't actually get passed along through res.render() to the view engine. Only its value does.

视图可以使用的是在 locals 中指定的键/属性:

What the view can use are the keys/properties specified within the locals:

res.render('dashboard', { plans: plans });
//                        ^^^^^

这些键将成为视图中的局部变量.

These keys will become the local variables within the view.

当您尝试使用时:

res.render('dashboard', { data: plans });

该视图可能反过来又使用了 data 来从 plans 中访问集合:

The view could have in turn used data to access the collection from plans:

if data # <--
  ul
    each val, index in data # <--
      li= index + ': ' + val

这篇关于Jade模板似乎无法从Express获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆