I'm relatively new to Salesforce, and I've been tasked with cleaning upon some of our VisualForce pages as they are in a bit of a mess.

At the top of every page we are constantly including various static resources such as CSS and JavaScripts. I want to be able to move these imports into a global template, so I can define their import once, and they are automatically available for all pages. I can't seem to find how to do this in the Salesforce documentation.

Am I able to achieve this, or is the Salesforce standard to constantly copy the same static resource imports on all our VisualForce pages?

1 Answer 1


You can create templates using apex:composition, apex:insert, and apex:define.

Basically, create a template using something like this:

<apex:page id="templatePage">
    <apex:includeScript ...
    <apex:stylesheet ...
    <apex:insert name="body" />

Then, each of your other pages uses that as a template:

<apex:page ...>
    <apex:composition template="templatePage">
       <apex:define name="body">
           <!-- this page's unique content -->

There's not really a way to retrofit your template onto every page (you'll need to edit each page once), but the change is trivial; just copy the composition and define lines into your page right before the usual content, and then close the tag near the end. This should only be a few minutes work per page, including cleaning up the includes that will be removed as a result of the effort. You can include anything you want in your composition element, and your composition can be as complex as you'd like.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .