Jade 中的过滤器嵌套失败 [英] Filter nesting failed in Jade

查看:65
本文介绍了Jade 中的过滤器嵌套失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些过滤器:

var jade = require('jade');
jade.filters.Posts = function(block) {
    return '{block:Posts}'+jade.render(block)+'{/block:Posts}';
};
jade.filters.Audio = function(block) {
    return '{block:Audio}'+jade.render(block)+'{/block:Audio}';
};
jade.filters.Video = function(block) {
    return '{block:Video}'+jade.render(block)+'{/block:Video}';
};

并有一些输入

:Posts
    Posts
        :Audio
            | Audio
        :Video
            | Video

所以我有一个错误:

 >> unknown filter ":Audio"

我可以处理或解决这个问题吗?

Can I handle or fix this problem?

PS 你可以看看这个 repository 中的代码——我正在使用 grunt 和 grunt-contrib-jade 插件,但要强制 grunt-contrib-jade 使用过滤器,您应该编辑 ./node_modules/grunt-contrib-jade/tasks/jade.js 以反映 更改从此拉取请求.

PS You can look at the code in this repository — I'm using grunt and grunt-contrib-jade plugin, but to force grunt-contrib-jade work with filters you should edit ./node_modules/grunt-contrib-jade/tasks/jade.js to reflect changes from this pull request.

PS2:我发现了绊脚石.当我在过滤器中使用 render() 方法时,我从本地 jade 实例调用它,它对过滤器一无所知,但全局 jade 实例(来自 Gruntfile.js)有关于它的所有信息过滤器.这就是为什么主要问题是:如何将全局 Jade 实例扔到带有过滤器的文件中?

PS2: I found the stumbling block. When I use render() method inside filter, I invoke it from local jade instance, which is don't know anything about filters, but global jade instance (from Gruntfile.js) have all information about that filters. That's why the main question is: how can I throw global Jade-instance to file with filters?

PS3:我不知道如何为这种情况创建小提琴.但是你可以克隆我的Hampi repo,实现将 grunt-contrib-jade 从我的 PR 更改为他们,然后在开始时运行 npm i.要编译模板,请运行 grunt jade.注意正文中的这些行.jade 和 评论部分在过滤器中.

PS3: I don't know how create fiddle for such case. But you can clone my Hampi repo, implement changes to grunt-contrib-jade from my PR to them, then at start run npm i. To compile templates run grunt jade. Pay attention to these line in body.jade and commented section in filters.

PS4.我在不同的范围内找到原因和它.我在此处详细描述了它.你能解决这个问题吗?

PS4. I find the reason and it in different scope. I describe it with details here. Can you solve this issue?

我愿意接受其他答案,我将接受 jade 核心的修复(如果需要).

推荐答案

我们应该像这样将全局 jade 实例绑定到过滤器:

We just should bind global jade instance to filters like this:

var jade = require('jade');
if (options.filters) {
  // have custom filters
  Object.keys(options.filters).forEach(function(filter) {
    if (_.isFunction(data)) {
      // have custom options
      jade.filters[filter] = options.filters[filter].bind({jade: jade, locals: data()});
    } else {
      // have no custom options
      jade.filters[filter] = options.filters[filter].bind({jade: jade });
    }

  });
}

在此处查看实现 - 在此提交

See implementation here — in this commit

这篇关于Jade 中的过滤器嵌套失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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