简体   繁体   English

模板化并尝试从CSS文件中引用上下文路径

[英]Templating and trying to reference context path from inside a CSS file

I'm working with JSF and XHTML templates , I'm using a CSS file in the templates, the background images being called like: 我正在使用JSFXHTML模板 ,我在模板中使用CSS文件,背景图像被称为:

background-image: url("../../images/imageFile.jpg");

Because I'm using templates I found out that I must keep same depth for both pages and styles/images to pages apply styles correctly, but the project had changes and now it requires variable depth for folders and pages, making this approach no longer viable. 因为我正在使用模板,我发现我必须为页面和样式/图像保持相同的深度才能正确应用样式页面,但项目有变化,现在它需要文件夹和页面的可变深度,使这种方法不再可行。

Then my question is: 然后我的问题是:

Is there some way to replace relative paths ( ../../ , ../ , etc) by the context path ( <%Request.getContextPath()%> , #{facesContext.requestContextPath} , etc), inside the CSS file? 是有一些方法来代替相对路径( ../../ ../ 等)由上下文路径( <%Request.getContextPath()%> #{facesContext.requestContextPath} 等等),则CSS内部文件?

-----UPDATE----- ----- -----更新

Absolute paths are out of the question. 绝对路径是不可能的。 I need my template-based pages (whichever depth they are) be able to find the style and image resources referenced from my CSS file. 我需要基于模板的页面(无论它们的深度)能够找到我的CSS文件引用的样式和图像资源。

Currently this is possible only if pages, styles and images share the same level of depth in the application's folder structure, but I can't keep any more this approach because the new project requirements prevent me to do this. 目前,只有当页面,样式和图像在应用程序的文件夹结构中共享相同的深度时才有可能,但我不能再保留这种方法,因为新的项目要求阻止我这样做。

Example of my project files structure, being <root> the path to application root: 我的项目文件结构的示例,是<root>应用程序根目录的路径:

CSS (depth-2): <root>/styles/global/myStyles.css Includes styles with depth-2 path references such as: CSS(depth-2): <root>/styles/global/myStyles.css包含具有深度2路径引用的样式,例如:

background-image: url("../../images/imageFile.jpg");

images (depth-2): <root>/images/basic/imageFile.jpg images(depth-2): <root>/images/basic/imageFile.jpg

templates (depth-2): <root>/template/general/template1.xhtml templates(depth-2): <root>/template/general/template1.xhtml

page (depth-2): <root>/pages/folder1/page1.xhtml (works OK) page(depth-2): <root>/pages/folder1/page1.xhtml (正常工作)

page (depth-N): <root>/pages/folder1/.../folderN/page2.xhtml (broken images and styles) 页面(深度-N): <root>/pages/folder1/.../folderN/page2.xhtml (损坏的图像和样式)

Those paths are relative to the request URL of the CSS file itself. 这些路径相对于CSS文件本身的请求URL。 It seems pretty trivial to me to group them at the same root level as the CSS file itself. 对于我来说,将它们分组到与CSS文件本身相同的根级别似乎是微不足道的。 Eg /css for CSS files and /css/images for CSS background images. EG /css的CSS文件和/css/images的CSS背景图像。 This way you can use url('images/name.ext') all the way. 这样你就可以一直使用url('images/name.ext')

I don't see how it is useful to change the paths everytime. 我不知道每次更改路径有什么用处。 Just keep them consistent and if necessary document it clearly so that it's clear to everyone now and in the future. 只需保持它们的一致性,并在必要时将其清楚地记录下来,以便现在和将来对每个人都清楚。

I have the same problem in a JSF2 project, in css file you can't access contextPath or similar. 我在JSF2项目中遇到同样的问题,在css文件中你无法访问contextPath或类似的东西。

In css you can define the same selector (#headerImg in my case) multiple times, the resulting css is a merge of the properties, in case of conflict the 'last' one loaded wins. 在css中,您可以多次定义相同的选择器(在我的情况下为#headerImg),生成的css是属性的合并,如果发生冲突,则“last”加载获胜。

so i leave in css file only the properties that doesn't need any kind of path, then modify the css selector in my 'root' xhtml template file, for example 所以我只在css文件中留下不需要任何路径的属性,然后修改我的'root'xhtml模板文件中的css选择器,例如

<load css files >
    <style type="text/css">
#headerImg {
    background: url("${request.contextPath}/resources/images/header.jpg") no-repeat scroll center center #FFCC3D;
}
[...]
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM