我的主题中有图像(在 Content/ 目录中),我想在不同的地方显示它们。
我当前的方法是: 
这有效,但不是很可维护的(如果我想切换主题等怎么办)。
是否有一个内置方法,例如GetCurrentThemeDirectory(),它会返回目录,以便我可以执行类似的操作?
编辑:从 mdm 的评论中,我意识到改变主题并不是一个有效的问题。我真的只是想避免为每个参考文献输入网址
I have images in my theme (in the Content/ directory) and I want to show them in different places.
My current approach is : <img src="@Url.Content("~/Themes/MyTheme/Content/Images/image.gif")" />
This works, but is not very maintainable (what if I want to switch themes, etc).
Is there an built in method, something like GetCurrentThemeDirectory() that would return the directory so I could do or something like that?
Edit: from mdm's comment, I realize that changing the theme isn't a valid concern. I really just want to avoid typing out the url for every reference
发布评论
评论(1)
您从哪里引用图像?模块?另一个主题?
如果是来自具有图像的主题,那么您无需担心切换主题。如果它来自另一个主题,那么该图像应该是该主题的一部分。如果它来自模块,那么将图像存储为模块的一部分或在主题中覆盖它是有意义的(见下文)。
如果您想让图像作为主题的一部分,那么您可以让模块返回“默认”形状,然后在主题中覆盖该形状。实际上不应该有任何理由从模块引用主题的图像,反之亦然。
编辑后进行编辑
在我编写的主题中,我遵循了 Orchard 作者的做法。图像不是使用
![]()
标签,而是放置在Styles/images
中。然后可以使用 CSSbackground-image
属性引用它们。在
Views/Branding.cshtml
中:然后在
site.css
中:Themes/TheAdmin/site.css
包含此方法的更多示例。Where are you referencing the image from? Module? Another theme?
If it is from the theme that has the image, then you don't need to worry about switching themes. If it is from another theme, then the image should be a part of the theme. If it is from a module, then it would make sense to store the image as part of the module or override it in the theme (see below).
If you wanted to have the image as part of the theme, then you could have the module return a 'default' shape and then override that in the theme. There really shouldn't be any reason to reference the theme's images from a module or vice versa.
Edit after your edit
In the themes I've written, I've followed what the Orchard authors do. Rather than using
<img>
tags, images are placed inStyles/images
. They can then be referenced using the CSSbackground-image
attribute.In
Views/Branding.cshtml
:And then in
site.css
:Themes/TheAdmin/site.css
contains plenty more examples of this method.