[TYPO3-dev] Best practice for including JS / CSS from an extension

Jigal van Hemert jigal at xs4all.nl
Tue Jun 14 12:35:51 CEST 2011


Hi,

On 14-6-2011 11:10, Fabien Udriot wrote:
> What would be your practice whenever including JS / CSS files in the
> frame of an _extension_ ?

 From the list of possible approaches I conclude that you mean an 
Extbase based extension.

Maybe you can use the approach I always use for pi_base extensions 
(thanks to Dmitry for this):

- Put the necessary HTML in the template:
   <!-- ###HEADER_PARTS### begin-->
   <link rel="stylesheet" type="text/css" 
href="###SITE_REL_PATH###res/pi1.css" />
   <!-- ###HEADER_PARTS### end-->

- In the extension code I use a small function to add it to the page:
protected function addHeaderPart() {
   $key = 'EXT:'.$this->extKey.md5($this->templateCode);
   if (!isset($GLOBALS['TSFE']->additionalHeaderData[$key])) {
     $headerParts = $this->cObj->getSubpart(
       $this->templateCode,
       '###HEADER_PARTS###'
     );
     if ($headerParts) {
       $headerParts = $this->cObj->substituteMarker(
         $headerParts,
         '###SITE_REL_PATH###',
         t3lib_extMgm::siteRelPath($this->extKey)
       );
       $GLOBALS['TSFE']->additionalHeaderData[$key] = $headerParts;
     }
   }
}

This way the CSS / JS is only present when the extension is present on a 
page. Maybe this is an alternative (after being adapted to the Extbase way)?

-- 
Kind regards / met vriendelijke groet,

Jigal van Hemert.




More information about the TYPO3-dev mailing list