More in this section

Forums / General Discussions / how can I reference "current theme" image from master page

how can I reference "current theme" image from master page

3 posts, 0 answered
  1. Kevin
    Kevin avatar
    19 posts
    Registered:
    30 Sep 2010
    27 Jan 2011
    Link to this post
    I need to reference a theme-specific image in my master page.

    The image lives in ~/App_Data/Sitefinity/WebsiteTemplates/MyTemplate/App_Themes/MyTheme/Images/

    It works if I reference it like this from the master page:
    <img src="/Sitefinity/WebsiteTemplates/MyTemplate/App_Themes/MyTheme/Images/image.png" />

    But that hard-codes the theme name and path, whereas I need to be able to dynamically switch which theme is applied to the template via the admin settings.

    Isn't there a way to reference the source of the image so that it changes when the theme changes?

    FWIW, this master page lives at ~/App_Data/Sitefinity/WebsiteTemplates/MyTemplate/App_Master/MyTemplate.master

    Thanks,
    Kevin
  2. Jason Green
    Jason Green avatar
    53 posts
    Registered:
    18 Sep 2009
    27 Mar 2011
    Link to this post
    Did you ever get a response or figure this one out?  I want to do the same thing from a custom user control.
  3. Kevin
    Kevin avatar
    19 posts
    Registered:
    30 Sep 2010
    28 Mar 2011
    Link to this post
    Jason,

    Here's some code to get the name of the current theme, which you can use in image source path.

    public static class Utility
    {
            public static string GetCurrentTheme()
            {
                Guid currentPageId = GetCurrentPageId();
                PageNode pn = GetPageNode(currentPageId);
                return GetPageTheme(pn);       
            }
      
            public static Guid GetCurrentPageId()
            {
                Guid pageId;
                PageSiteNode psn = SiteMapBase.GetActualCurrentNode();
                if (psn == null)
                {
                    PagesConfig pagesConfig = Config.Get<PagesConfig>();
                    pageId = pagesConfig.HomePageId;
                }
                else
                {
                    pageId = psn.Id;
                }
                return pageId;
            }
      
            public static PageNode GetPageNode(Guid pageId)
            {
                var pf = App.WorkWith().Page(pageId);
                return pf.Get();
            }
      
            public static string GetPageTheme(PageNode pn)
            {
                return (pn.Page.Template != null) ? pn.Page.Template.Theme : "";
            }
      
    }
3 posts, 0 answered