您可以在不使用相对路径的情况下从 css 引用图像吗? [英] Can you reference images from css without using relative paths?

查看:18
本文介绍了您可以在不使用相对路径的情况下从 css 引用图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有背景图片 css 的 div:

I'd like to create a div with a background image css:

#mydiv {
  background-image:url('/public/images/foo.png');
  background-repeat: repeat-x;
}

现在,我无法在 css 中使用路由,因此我必须使用相对路径,否则如果安装在非根路径上,我的应用程序将中断.

Now, I can't use routes in css, so as a result I have to use a relative path, or my app will break if installed at a non-root path.

有没有办法解决这个问题?类似的东西

Is there a way around this? Something like

background-image:url('@{publicFolder}/images/foo.png');

我确实从一年前找到了这个主题声称这是不可能的.还是这样吗?

I did find this thread from a year ago that claims it's impossible. Is this still the case?

编辑:我知道我可以在页面中内联 css,这不是一个真正可以接受的解决方案,而是一种解决方法.

Edit: I know I can inline the css in the page, that's not really an acceptable solution, but rather a work around.

推荐答案

绕过/is/的方法.

问题是 CSS 文件是静态的,所以 Play!不对它们做任何事情——除了为客户服务.

The problem is that CSS files are static, so Play! does not do anything on them - except serving them to the clients.

只需将您的 css 文件制作成视图,编写一个以文件名作为参数的通用控制器,它们将作为 Play!模板.

Just make your css files into views, write a generic controller that takes the filename as a parameter and they will be served as Play! Templates.

类似于(注意:伪代码!):

something in the lines of (beware: PSEUDOCODE!):

路线:

get /css/{filename}  Application.getCSS

控制器

class public void getCSS(filename)
{
     renderTemplate(filename);
}

然后在你的 css 中你可以使用任何你想要的 groovy 模板功能.安装路径为 http.path in the conf

Then in your css you can use any groovy template feature you want. The installation path is available as http.path in the conf

但这会非常低效,因此您必须添加 nginx 或类似前端来进行一些缓存并为这些文件设置高到期值.

This will be very inefficient though, so you will have to add nginx or similar frontend to do some caching and set high expiration values for those files.

我不确定这样做是否值得只是为了避免相对路径

I'm not sure if it's worth doing it just to avoid relative paths!

我绝对认为相对路径更适合通用部署,并且您更有可能使用这种动态绝对方法破坏事物.

I definitely think that the relative path is better suited generic deployment, and you are more likely to break things with this dynamic absolute approach.

还有一点很重要,如果您确实喜欢绝对 URL,那没有问题.其实我也是.

Anothe important point is that if you do like absolute URLs, there's no problem. I actually do too.

但是当我部署我的网络应用程序时(无论是玩游戏!django、纯 wsgi 还是其他任何东西)我总是放置一个网络前端.

But when I deploy my web applications (be them play!, django, pure wsgi, or whatever else) I always put a web frontend.

通常是 nginx.

这意味着您的播放框架将是一个上游服务器,您可以使用 :80 端口上的 URL 做您想做的任何事情 - 并使用子域或子文件夹管理多个服务...

This means that your play framework will be an upstream server, and you can do everything you want with the URLs on your :80 port - and manage several services with subdomains or subfolders...

但是当您通过自己的端口访问您的播放应用程序时,一切都将是绝对的.

but when you access your play application on its own port(s) everything will be absolute.

最后,如果您正在部署 Play!作为一场战争,我会感觉你选择了错误的框架或错误的部署模式!但是你的 jboss 或任何其他应用程序网络服务器会对子文件夹做一些魔术......

Finally, if you were deploying Play! as a war I would have the feeling you have chosen the wrong framework or the wrong deployment pattern! But then your jboss or whatever other application webserver will do some magic on the subfolders...

这篇关于您可以在不使用相对路径的情况下从 css 引用图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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