in node.js, how to pass variables to :stylus filter from jade?

笑着哭i 提交于 2019-12-23 18:18:36

问题


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?


回答1:


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):

  • Jade: Pass markdown filter a variable.
  • :markdown filter processing the text of a String variable
  • :markdown with variable


来源:https://stackoverflow.com/questions/15584506/in-node-js-how-to-pass-variables-to-stylus-filter-from-jade

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!