我可以使用 Express.js 动态渲染 css 吗?
我想渲染在每个请求时动态生成的 css。
例如,在 Express.js 中,我有一条路线:
/clients/:clientId/style.css
当我收到匹配的请求时,我想从我的存储库中查找客户端并提取它们的文本颜色。然后我想把这个颜色推送到 css 响应中。
是否有一个模板引擎可以渲染文本文件,例如 style.less,我可以用 less/stylus 进行解析?
有什么想法、替代策略吗?
干杯, 戈登
I want to render css which is generated on the fly at each request.
For example, in Express.js I have a route:
/clients/:clientId/style.css
When I get a matching request, I want to look up the Client from my repository and pull out their text colour. Then I want push this colour out to the css response.
Is there a templating engine that can render a text file, say style.less, that I can parse with less/stylus?
Any ideas, alternative strategies?
Cheers,
Gordon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您还没有意识到这一点,我想您已经意识到了,您可以在 Stylus 中使用
define(name, function)
和define(name, variable)
来 < a href="https://github.com/LearnBoost/stylus/blob/master/docs/js.md" rel="nofollow noreferrer">在 CSS 中提供动态内容。假设您使用 Express 并调用命令
./node_modules/express/bin/express -t james -c stylus
到 创建一个骨架站点,它应该处理模板引擎 - Stylus 将处理 CSS 和 Jade(或您的 view_engine)选择)处理其余的事情;我不明白 view_engine 在这里有多重要(你能解释一下你对它的评论,防止在样式标签中插入数据吗?)可能是你将样式标签直接嵌入到 html 页面中?如果是这种情况,请尝试将其提取到自己的模板文件中并让 Stylus 拥有它。
如果您想手动设置,则需要在
app.configure()
内部进行类似的操作:它看起来像 这篇文章可能有替代策略,具体取决于您的具体需求。
希望这能给您一些想法,即使不是完整的解决方案:)
If you haven't realized this yet, I'm thinking you have, you can use
define(name, function)
anddefine(name, variable)
in Stylus to provide dynamic content within the CSS.Assuming you use Express and call the command
./node_modules/express/bin/express -t jade -c stylus
to create a skeleton site, it should take care of the templating engines--Stylus would handle the CSS and Jade (or the view_engine of your choice) handles the rest; I don't see how the view_engine matters here (could you explain your comment about it preventing interpolation of data in a style tag?)It might be that you're embedding the style tag directly into the html page? Try extracting it to its own template file and letting Stylus have it if that's the case.
If you want to set this up manually, you need something like this inside of
app.configure()
:It looks like this SO post may have an alternative strategy, depending on your specific needs.
Hope this gives you some ideas, if not a complete solution :)
如果它是CSS,您只需将其添加到页面底部,浏览器就会拾取它并渲染页面
jQuery 包含 css
if its a css, you can just add it to the bottom of the page, browser will pick it up and render the page
jQuery include css