Dynamic path for themed images for NopCommerce 2.0 with MVC Razor

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Does anyone know the best way to generate the dynamic path for themed images in NopCommerce 2.0 ??
I currently have the following code as part of a plugin, however I would like for the images to be themable based on the current site theme, which would mean that it would search for the images in the /themes/ folder.

I found another post but it was for the NopCommerce 1.x versions and doesn't seem to apply to MVC razor.
Any help would be appreciated!

Current cshtml code:
<img src="@Url.Content("~/ ?? THEME PATH ?? /Content/images/ico-facebook.png")" alt="" />
12 years ago
ScottMc101 wrote:
Does anyone know the best way to generate the dynamic path for themed images in NopCommerce 2.0 ??
I currently have the following code as part of a plugin, however I would like for the images to be themable based on the current site theme, which would mean that it would search for the images in the /themes/ folder.

I found another post but it was for the NopCommerce 1.x versions and doesn't seem to apply to MVC razor.
Any help would be appreciated!

Current cshtml code:
<img src="@Url.Content("~/ ?? THEME PATH ?? /Content/images/ico-facebook.png")" alt="" />




Scott,

You can use dependency injection to get the active themeContext (this contains the theme used by the current user). You could create an extension method that resolves the theme context, and based on the "WorkingTheme" it builds the url.


//Example method call
Url.NewExtensionMethod("~/{0}/Content/Images/ico-facebook.png")



//Example Extension method
public static string NewExtensionMethod(this Url u, string url){
var ctx = EngineContext.Current.Resolve<IThemeContext>();
return string.Format(url, ctx.WorkingTheme);
}


edit

p.s. You would want to take advantage of the "u" parameter so you render the correct url. Also there is a lot of room for improvement (it probably will not compile like this since it is just an example), but this will get you started. Good luck!
12 years ago
Thanks you for your solution, skyler.severns

ScottMc101 wrote:
Does anyone know the best way to generate the dynamic path for themed images in NopCommerce 2.0 ??
I currently have the following code as part of a plugin, however I would like for the images to be themable based on the current site theme, which would mean that it would search for the images in the /themes/ folder.

I found another post but it was for the NopCommerce 1.x versions and doesn't seem to apply to MVC razor.
Any help would be appreciated!

Current cshtml code:
<img src="@Url.Content("~/ ?? THEME PATH ?? /Content/images/ico-facebook.png")" alt="" />


I updated as following and it worked.

My project was using current theme named CustomTheme.I put this in file EditorExtensions.cs (located in Presentation\Nop.Web\Extensions)

public static string CurrentThemePath(this UrlHelper u, string url)
        {
            var ctx = EngineContext.Current.Resolve<IThemeContext>();
            url = string.Format(url, ctx.WorkingTheme);
            return UrlHelper.GenerateContentUrl(url, HttpContext.Current.Request.RequestContext.HttpContext);
        }



On view, I call it as following example:

<img src='@Url.CurrentThemePath("~/Themes/{0}/Content/Images/logo.png")' alt="@T("Common.Logo")" width="150" height="170" />


That will be renderred as:

<img width="150" height="170" alt="logo" src="/[myProjectName]/Themes/CustomTheme/Content/Images/logo.png" />

Wish this post help some others.
11 years ago
Guys - thank you!!!  Brilliantly simple solution and works like a charm.  Been trying to solve this one for hours.

Just to note that in the current release (2.65), WorkingDesktopTheme is required.

        public static string CurrentThemePath(this UrlHelper u, string url)
        {
            var ctx = EngineContext.Current.Resolve<IThemeContext>();
            url = string.Format(url, ctx.WorkingDesktopTheme);
            return UrlHelper.GenerateContentUrl(url, HttpContext.Current.Request.RequestContext.HttpContext);
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.