This is a migrated thread and some comments may be shown as answers.

Using StyleSheetRegistrar with app_themes

5 Answers 259 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Brian Crosby
Top achievements
Rank 1
Brian Crosby asked on 22 Jan 2010, 06:34 PM
I would like to compress css files for themes as well and have come up with a solution but I think it is really ugly and wanted to know if anyone knows of a cleaner way of doing it. I have the code below in my master file. It just loops through the controls in the head section looking for paths:

    <script runat="server">  
        protected override void OnLoad(EventArgs e)  
        {  
            foreach (var link in Page.Header.Controls.OfType<HtmlLink>().ToList())  
            {  
                if (!link.Href.Contains("App_Themes")) continue;  
 
                Html.Telerik().StyleSheetRegistrar().StyleSheets(styles =>  
                    styles.AddGroup(this.Page.Theme + "Theme", group =>  
                        group.Add(link.Href)  
                            .Combined(true)  
                            .CacheDurationInDays(365)  
                            .Compress(true)  
                        )  
                    );  
 
                // Remove original link from header  
                link.Parent.Controls.Remove(link);  
            }  
        }
    </script> 

5 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 24 Jan 2010, 04:08 AM
You could create a shared asset group for each theme then only include the group that corresponds to the current theme.

For example, in your Global.asax.cs:

private void AddThemeStyles() 
   SharedWebAssets.StyleSheets(styles => styles 
                       .AddGroup("Theme1", group => group 
                                             .Add("~/App_Themes/Theme1/file1.css"
                                             .Add("~/App_Themes/Theme1/file2.css"
                                             .Combined(true)  
                                             .Compress(true
                                             .CacheDurationInDays(365} 
                                                       ) 
                        .AddGroup("Theme2", group => group 
                                             .Add("~/App_Themes/Theme2/file1.css"
                                             .Add("~/App_Themes/Theme2/file2.css"
                                             .Combined(true)  
                                             .Compress(true
                                             .CacheDurationInDays(365} 
                                                       ) 
                         ); 

Make sure that the name specified for each group (Theme1 and Theme2 in this case) matches what will be in this.Page.Theme in you view.

Then in your master page or view:

<head> 
... 
 
<% Html.Telerik().StyleSheetRegistrar().StyleSheets(styles =>
                                   styles.AddSharedGroup(this.Page.Theme)).Render(); %
> 
 
</head> 




This way only the files for the current theme are included and no change is required to any of your code when the theme is changed.

Hope this helps,
Jeff French


0
Brian Crosby
Top achievements
Rank 1
answered on 24 Jan 2010, 04:14 PM
But won't I still need to loop through the links in the heading and remove the styles added automatically by ASP.Net?
0
Jeff
Top achievements
Rank 1
answered on 24 Jan 2010, 05:32 PM
Sorry, I'm unfamiliar with using ASP.NET themes in MVC. I'm not even sure how or why you would do that as it seems to defeat one of the great advantages of MVC: better control of your markup. I guess you would still need to loop through and remove the links. However, if you combined that with my approach then your loop might perform a little better since you would only be removing the links and not building the stylesheet group on the fly.

-Jeff French
0
Accepted
Alex Gyoshev
Telerik team
answered on 25 Jan 2010, 09:32 AM
Hello guys,

@Brian: if you want to combine the files in the themes, you can use the approach that Jeff suggested and skip the App_Themes (i.e. place the files in a different folder, say ~/Content/themes/). This way, the files won't be registered automatically by ASP.NET, and you will be in full control.

Kind regards,
Alex Gyoshev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Doron
Top achievements
Rank 1
answered on 23 May 2011, 06:08 PM
Hello,

I want to do exactly what Jeff does but I am using Themes from the Microsoft.Web.Helpers library.
Therefore instead of:
<% Html.Telerik().StyleSheetRegistrar().StyleSheets(styles => 
                                   styles.AddSharedGroup(this.Page.Theme)).Render(); %
>
I want to write:
<% Html.Telerik().StyleSheetRegistrar().StyleSheets(styles => 
                                   styles.AddSharedGroup(Themes.CurrentTheme)).Render(); %
>

but it does not render anything.
Any idea?

Thanks
Tags
General Discussions
Asked by
Brian Crosby
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Brian Crosby
Top achievements
Rank 1
Alex Gyoshev
Telerik team
Doron
Top achievements
Rank 1
Share this question
or