Component Writer's Guide :: Preventing Double Header Code

From RadPHP XE2 Documentation
Jump to: navigation, search

RPCL is organized in a way your components are called to generate the appropiate code for every part of the HTML document, for example, the header.

You can override dumpHeaderCode() to dump anything you want to put on the header section of the document.

Because of this, if you place two components on a page, the dumpHeaderCode() for your component will be called twice, so if you generate static code on that method you only want to be inserted once, you need to write a check there to prevent this from happen.

We have a convention for that on the RPCL, we do it using defines, for example:

When writting a component, you may need to add a declaration line to the page header, and there are several ways to prevent include several times the same line when adding more components of the same class to the same page.

function dumpHeaderCode()
{
 if (!defined('DYNAPI'))
 {
 echo "<script type=\"text/javascript\" src=\"".RPCL_HTTP_PATH."/dynapi/src/dynapi.js\"></script>\n";
 define('DYNAPI', 1);
 }
}

This way, that code will be generated only once.

Personal tools