How do you use javascript variables when writing inline CSS styles in a Jade template

StackOverflow https://stackoverflow.com/questions/8814785

  •  26-10-2019
  •  | 
  •  

I'm trying to dynamically insert snippets of CSS. Ideally, this would work:

style(type='text/css')
    #header a#logo { background:url(constants.logo) no-repeat; }
    @media only screen and (-webkit-min-device-pixel-ratio: 1.5) {
        #header a#logo { background-image: url(constants.logo_2x); }
    }

Unfortunately, constants.logo is /literally/ placed in the DOM. What does work is the following:

!= "<style type='text/css'>"
!= "#header a#logo { background:url('"+constants.logo+"') no-repeat; }"
!= "@media only screen and (-webkit-min-device-pixel-ratio: 1.5) {"
!= "    #header a#logo { background-image: url('"+constants.logo_2x+"'); }"
!= "}</style>"

Is there a better way?

有帮助吗?

解决方案

Use #{variable} to print a variable within an element's text.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top