gpt4 book ai didi

javascript - 添加创建 mixin 并使用它 keystonejs

转载 作者:太空宇宙 更新时间:2023-11-04 00:59:36 25 4
gpt4 key购买 nike

我正在尝试为 keystonejs 创建一个 jade mixin,这是我在 index.jade 上想到的

extends ../layouts/default
block bodycontent
include ../mixins/focus.jade

和 mixin Jade 文件

mixin focus(focus)
section#focus.focus
div.container
div.service-icon= focus.image

这是 javascript View 文件

var keystone = require('keystone'),
Focus = keystone.list('Focus');

exports = module.exports = function(req, res) {

var view = new keystone.View(req, res),
locals = res.locals;

// Set locals
locals.section = 'focus';

locals.data = {

};

view.on('init', function(next) {

Focus.model.find()
.populate('author')
.sort('label')
.limit(4)
.exec(function(err, fields) {
// do something with posts
locals.data.fields = fields;
console.log(fields);
next(err);
});
});

view.render('focus');

};

当我渲染index.jade 文件时,焦点部分丢失。我缺少什么?列表模型存在,因为我能够将其保存到数据库中。

最佳答案

与include不同,你需要在声明它们之后调用mixin。它们就像函数。

请参阅此处的文档:http://jade-lang.com/reference/mixins/

然后,假设 locals 中有一些数据定义了要关注的内容,您首先要包含定义 mixin 的文件,然后调用 mixin 并向其传递要渲染的数据.

我猜测了一下,因为我看不到你的整个项目结构,但类似这样的东西可能就是你想要的:

路由处理程序

var keystone = require('keystone'),
Focus = keystone.list('Focus');

exports = module.exports = function(req, res) {

var view = new keystone.View(req, res),
locals = res.locals;

locals.section = 'focus';

locals.data = {};

view.on('init', function(next) {

Focus.model.find()
.populate('author')
.sort('label')
.limit(4)
.exec(function(err, results) {
locals.data.results = results;
next(err);
});
});

view.render('focus');

};

混合

mixin focus(focus)
section#focus.focus
div.container
div.service-icon= focus.image

模板

extends ../layouts/default
block bodycontent
include ../mixins/focus.jade
each i in data.results
+focus(i)

关于javascript - 添加创建 mixin 并使用它 keystonejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27626402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com