ASPX templates
Version 5.x > ASPX templates > Creating per page script and style section View modes: 
User avatar
Member
Member
llee-multnomah - 4/13/2011 4:15:06 PM
   
Creating per page script and style section
I would like to have the ability to customize certain pages with additional styles and javascript. It doesn't make sense to do this in the content->page tab because all other users are just interested in content. I would like to add a couple of textareas to the form tab, one for a style tag and one for a script tag. I would like that information then to be inserted into my template.

I "think" the necessary process would require adding to the document type and creating a couple of form types and then inserting some sort of call into the templates. I am just not sure what that specifically would look like. Can someone walk me through the process and/or point me to some sort of tutorial.

Thanks.

User avatar
Member
Member
kentico_michal - 4/14/2011 8:07:22 AM
   
RE:Creating per page script and style section
Hi,

You can add new custom fields to your document types (field Script, Styles etc.) which will cover your needs.

To include their content on the current document, you can use following code in the page layout:

<asp:literal runat="server" id="script_ltl" /> 
<asp:literal runat="server" id="styles_ltl" />

<script runat="server" >
protected void Page_PreRender(object sender, EventArgs e)
{
script_ltl.Text= CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("{%Script%}").ToString();
styles_ltl.Text= CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("{%Styles%}").ToString();
}
</script>


More information about context macros can be found here

Best regards,
Michal Legen

User avatar
Member
Member
LeRoy - 4/14/2011 12:51:54 PM
   
RE:Creating per page script and style section
So far this is looking great. I was wondering if this has to take place in prerender? I do most work in Page_Load. Would it work there as well?

User avatar
Member
Member
kentico_michal - 4/15/2011 8:08:36 AM
   
RE:Creating per page script and style section
Hi,

Yes, it should be possible to achieve this also in the Page_Load method

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
script_ltl.Text= CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("{%Script%}").ToString();
styles_ltl.Text= CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("{%Styles%}").ToString();
}
</script>



Best regards,
Michal Legen