Dynamic path for themed images for NopCommerce 2.0 with MVC Razor

Posted: September 09, 2011 at 11:33 PM Quote #48377
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="" />
This post/answer is useful
2
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: September 09, 2011 at 11:58 PM Quote #48378
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!
This post/answer is useful
4
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Please use the VOTE functionality to recognize helpful or inappropriate posts.

Skyler Severns
http://csharpwebdeveloper.com/
Posted: January 16, 2012 at 11:07 PM Quote #59930
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.
This post/answer is useful
2
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Posted: October 10, 2012 at 2:41 PM Quote #80736
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 post/answer is useful
0
This post/answer is not useful

Please login or register
to vote for this post.

(click on this box to dismiss)
Michael D. Woffenden, Founder and President | Information Results Corporation | http://www.InformationResults.com
Premium support services
  • Dedicated premium support services provided by core developers are intended for persons who run mission critical websites, work on projects with tight deadlines, or want to get dedicated support.
Professional services
  • Want to open a new store? Want to take your store to the next level? Need a custom extension? We can customize nopCommerce to fit your store perfectly. Request a quote to get started.