Sitecore Code Editor

Binding Media to Content

Adding javascript and stylesheet files to the media library isn’t very useful unless they can be associated with specific content items. The follow steps show how easy it is to add two scripts a and stylesheet to the layout for the home page so that the title blinks and the background is blue.

We begin by seeing that a new template called MediaResource has been added to the User Defined templates.

image alt text

Next we need to add the MediaResource template to the Sample Item template.

image alt text

Now on the Home item, which inherits from the Sample Item template, we associate the new uploaded files.

image alt text

Next we open up the layout and add the following markup as seen in the screenshot below:

Source Code
<%@ Register tagPrefix="ce" assembly="Sitecore.SharedSource.CodeEditor"
    namespace="Sitecore.SharedSource.Web.UI.WebControls" %>
<ce:ResourceWebControl ID="ResourceWebControl1" runat="server"
    ResourceType="Style" Cacheable="true">
</ce:ResourceWebControl>
<ce:ResourceWebControl ID="ResourceWebControl2" runat="server"
    ResourceType="Script" Cacheable="true">
</ce:ResourceWebControl>

image alt text

The results are amazing!

Blink.

image alt text

Blink.

image alt text

Finally, the proof that the resource were injected into the page.

image alt text

The resources will be minified while compilation settings in the web.config are set to debug="false”.