apex:include vs. Visualforce component, when and why?

I recently had a reason to look more closely at the standard apex:include component in Visualforce, and I’m intrigued by the intent behind this component. Would anyone be willing to share some perspectives on when and why it would be more appropriate to use apex:include vs. a custom Visualforce component?

It seems to me that Visualforce custom components do the same thing as an apex:include element, and Visualforce custom components have the added benefit of being able to accept custom attributes. The only use case I can imagine right now for using apex:include is to bring in a page from a managed package.

Answer

apex:include was the original attempt at re-using content between Visualforce pages – its been superseded by components as they are properly re-usable as discrete standalone items.

Doug Chasman, the architect of Visualforce, posted the following on the developer forums in 2008 in response to a question regarding problems with apex:include:

Why are you using apex:include instead of just creating a custom
component? apex:include predates custom components and is only still
around for backward compatibility. A custom component has a well
defined mechanism for specifying the inputs to the included content
via attributes and also has the added advantage of passing objects and
expressions instead of just query params/name value pairs…

(You can read the original post here)

One reason you might want to use apex:include is that if the main page and the included page use the same controller, they will share the same instance of that controller, and can influence each other’s behaviour. I wouldn’t recommend it though, as it can make the page structure and effects of changing code quite difficult to understand.

As an aside, composition templates are a way to provide a bunch of boilerplate content and functionality across a set of pages, while allowing each page to inject some custom content into certain areas. The poster child use case for this is a web site where each page injects its specific content into the main body, while the header/sidebar etc is provided by the template.

TL;DR – always use components over apex:include

Attribution
Source : Link , Question Author : Marty C. , Answer Author : Bob Buzzard

Leave a Comment