Writing dynamic CSS with Jade

后端 未结 4 1453
[愿得一人]
[愿得一人] 2021-01-11 11:44

I\'m trying to write out some dynamic CSS using Jade, like so:

style(type=\'text/css\')
    each item in colors
        .#{item.class} { background-color :          


        
4条回答
  •  天命终不由人
    2021-01-11 12:11

    Because style tags only allow text in Jade, it's treating your each item in colors as plain text. Then it encounters the #{item.class} and attempts to parse that, but it fails because it didn't define item on the previous line.

    Unfortunately, I'm not sure that there is a good way to do this in Jade. You might just have to define all of your css ahead of time in JS and then insert it like so:

    style(type='text/css')
        #{predefined_css}
    

    At that point though, it might be simpler to just move the styles to an external stylesheet that gets generated for each user and then serve it with some reasonable caching headers. That would avoid the difficulties with trying to make Jade do dynamic CSS and speed up subsequent page loads for your users.

提交回复
热议问题