在 node.js 中,如何将变量从 jade 传递给 :stylus 过滤器? [英] in node.js, how to pass variables to :stylus filter from jade?

查看:74
本文介绍了在 node.js 中,如何将变量从 jade 传递给 :stylus 过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jade 中创建一个我希望 stylus 过滤器使用的变量.

使用 #{var} 似乎不起作用.例如,这段代码:

<前>- var color1 = '蓝色'手写笔:div背景色粉红色颜色 #{color1}

给出结果错误:

<前>/home/data/tnt/server/node/www/tech/cool.jade:21|div2|背景色粉红色> 3|颜色 #{color1}4|预期缩进",得到缩进"

如何让 jade 变量 color1stylus 过滤器中可见?

解决方案

过滤器(如手写笔、markdown 等)在编译时"期间执行,并且仅执行一次.

但是文本替换是在运行时完成的,因此过滤器无法访问变量.

过滤器只能访问原始文本,例如.color #{color1} 而不是 color blue

作为一种解决方法,您可以创建一个辅助方法,您可以按照在多个地方的建议进行手写笔模板化和渲染(我只找到了使用 Markdown 过滤器的示例,但过滤器的工作方式相同,因此它们也适用于 <代码>手写笔过滤器):

creating a variable within jade that I wish the stylus filter to use.

using #{var} does not appear to work. for example, this code:

  - var color1 = 'blue'
  stylus:
    div
      background-color pink
      color #{color1}

gives resulting error:

/home/data/tnt/server/node/www/tech/cool.jade:2
   1| div
   2|   background-color pink
 > 3|   color #{color1}
   4|   

expected "indent", got "outdent"

how do I get the jade variable color1 visible within the stylus filter?

解决方案

Filters (like stylus, markdown etc.) are executed during "compile-time", and only once.

But text replacement is done during run-time, so filters don't have access to variables.

Filters can only access the raw text eg. color #{color1} and not color blue

As a workaround you can create a helper method where you do stylus templating and rendering yourself as suggested in multiple places (I've only found examples which are used the markdown filter but filters are working the same so they apply also the stylus filter):

这篇关于在 node.js 中,如何将变量从 jade 传递给 :stylus 过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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