|Number of votes:||6|
A couple of weeks ago, I got a question from a developer who was wondering what the RequiredClientResources web controls in the Alloy templates were used for. If you take a look Alloy’s MasterPage, you’ll find one inside the head tag, and one at the bottom. Notice how they are marked with different values for the RenderingArea property:
This is useful if you want to keep your client resource rendering clean and tidy. But there are other uses as well. Add-ons and modules are not directly linked to your templates, but some of them might require some client resources to be rendered on your pages. As an example, the google analytics add-on uses this to add a tracking script to your page.
If you are developing your own add-on and want to render client resources, you can try this code as an example:
Note: you also have a PageData object available, which will give you the current page being rendered, which can be useful if you need to affect the rendering based on the current page.
Note 2: Since this is a (hypothetical ) add-on, I use Paths.ToClientResource to make sure that the path to the script points to the right place – add-on locations may differ.
So how do I make my site add-on friendly?
Implement the client resource rendering with both Header and Footer area and you should be fine. It should only take you a few minutes. This is also marked as a step for upgrading your site from 6 to 7 in the SDK so you should consider it kinda important.
For the curious ones out there, the client resources are registered just before the Render event in Web Forms (OnPreRenderComplete).
What if I’m using MVC?
If you’re using MVC, you can use the following (you’ll find the code for this in the MVC templates as well):
The EPiServer PageController has a RequireClientResources action filter to allow the register process to run for your views.