2

Is there a PHP include() for Visualforce/Force.com?

I am creating a website using force.com and I need a way to insert possibly other visualforce codes into the top and bottom of the page without having to write it out manually for each page.

PHP includes a function called include() that allows me to insert code from a seperate file into the existing file when executed.

1

2 Answers 2

1

PHP is a Server Side Scripting Language, while Apex is an object oriented, strongly typed language. Salesforce uses an implementation of the MVC framework to create custom UI. What this means, is the way you structure code is different.

While PHP you would you typically just have 1 file that runs server logic and writes out HTML, with MVC you will have 3 distinct layers of logic.

  • Model: The salesforce objects in this case. Could also be any custom DTO you have created in apex
  • View: The Visual Force pages. Used to created HTML. The model can be binded directly into the VF page using {!} sytanx.

  • Controller: The apex class that handles what models and actions are available to the view.

So, to answer you questions, there are two (main) scenarios for PHP include:

  1. To reference a code library: In this case, all you have to do is reference the Apex class you want to invoke in your code.

  2. To include another PHP script that writes HTML (what you are asking): in this case, the most equivalent would be to use Apex:Include. However, it is considered better practice to create an Apex:Component for any reusable parts of the UI. I believe Apex:Include is embedded in the page using an iFrame, which can cause issues.

The visual force developer guide will explain in great detail how to use both Apex:Include and Apex:component

3

Imran

you can either use

apex:include - to include another existing visualforce page

or

apex:component - to include a component into your page

and here's a good discussion related to their usage..

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

3
  • Where is the include itself stored? I understand components are stored but its not quite clear with include Oct 2, 2014 at 11:49
  • what do you mean by "stored" ? Oct 2, 2014 at 11:53
  • Sorry I mean where do you create them? i.e for components its located at Settings > Develop > Components Oct 2, 2014 at 15:25

You must log in to answer this question.

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