CSS / Theme Selector
How would I go about creating a control for users changing the theme of a site on the frontend, similar to the theme button when editing a page?
Hello Kristian,
Here is a sample code that illustrates how to change the Theme at runtime
var manager = PageManager.GetManager();
SiteMapNode currentNode = SiteMapBase.GetCurrentProvider().CurrentNode;
Guid pageId = ((PageSiteNode)currentNode).PageId;
var draft = manager.EditPage(pageId,
true
);
draft.Theme =
"test2"
;
manager.PublishPageDraft(draft.Id,
true
);
manager.SaveChanges();
SiteMapBase.Cache.Flush();
Response.Redirect(currentNode.Url);
Thank you,
Hello Kristian,
Here is a sample code that illustrates how to change the template theme, but there are issues with setting this at runtime and you have to edit the page and then save it.
var manager = PageManager.GetManager();
var templateID = manager.GetTemplates().Where(t => t.Title ==
"Copy of Default"
).SingleOrDefault().Id;
var editTemplate = manager.EditTemplate(templateID,
true
);
editTemplate.Theme =
"test2"
;
manager.SaveTemplateDraft(editTemplate.Id);
manager.PublishTemplateDraft(editTemplate.Id);
manager.SaveChanges();
SiteMapBase.Cache.Flush();
I'm not sure if I have this working correctly. I set both my template and page to use no theme on the backend. Then I click on the button on my page to change the template theme. I check the template under page templates and i see the new theme is reflected in the editor. I click publish on the theme and then refresh the page i wanted the theme to change. but it still appears as no theme.
Hi Kristian,
Editing the page should apply the theme on it properly. As I wrote there are issues with this process when you are working programatically and we have logged this issue for fixing.
Greetings,
Ivan Dimitrov
the Telerik team
finally got back around to this but receive a 403 when trying to retrieve the css
HtmlLink link =
new
HtmlLink();
link.Href =
"~/App_Data/Sitefinity/WebSiteTemplates/UserDefined/App_Themes/Red/Global/main.css"
;
link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString(),
"stylesheet"
);
Page.Header.Controls.Add(link);
Hello Kristian,
It looks like you have the same problem as this one discussed here.
Sincerely yours,
Ivan Dimitrov
the Telerik team
Thanks for the help, i solved it by just starting the path from ~/Sitefinity instead of ~/App_Data
How come certain widgets do not work the same with a custom theme, for example the lightbox option for an image gallery?
Hello Kristian,
You have something in your theme/template that breaks the widget. Is there any information in the browser console - js error, css loading issues etc.
Kind regards,
Ivan Dimitrov
the Telerik team