Salut,
MODx offre déjà ce type de fonctionnalités
Voilà un bout de code que j’utilise et qui provient de eForm (auteur Tobyl)
<?php
//Add js and css stuff in front end
//taken form eform
//author Jaelle Jabber => Tobyl
function efLoadTemplate($key){
global $modx;
if( strlen($key)>50 ) return $key;
$tpl = false;
if( is_numeric($key) ) { //get from document id
//try unpublished docs first
$tpl = ( $doc=$modx->getDocument($key,'content',0) )? $doc['content'] :false;
if(!$tpl )
$tpl = ( $doc=$modx->getDocument($key,'content',1) )? $doc['content'] : false;
}elseif( $key ){
$tpl = ( $doc=$modx->getChunk($key) )? $doc : false;
//try snippet if chunk is not found
if(!$tpl) $tpl = ( $doc=$modx->runSnippet($key) )? $doc : false;
}
return $tpl;
}
# registers css and/or javascript to modx class
function efRegisterStartupBlock($src_array,$noScript=false){
global $modx;
if(!array($src_array)) return;
foreach($src_array as $item){
list($src,$type) = $item;
//skip scripts if told to do so
if($type=='javascript' && $noScript) continue;
//try to load from document or chunk
if( $tmp = $this->efLoadtemplate($src) )
$src = $tmp;
$src=trim($src);
if ( $type=='css' )
$modx->regClientCSS($src);
elseif ( $type == 'javascript' )
$modx->regClientStartupScript($src);
else
$modx->regClientStartupHTMLBlock($src);
}//end foreach
}
Les fonctions sont => regClientStartupScript et regClientStartupHTMLBlock
Dans mon snippet
<?php
//En debut de script
//JS and CSS stuff taken form eform author Jaelle Jabber A.K.A tobyl
//added eFormCSS and eFormJS parameters
//these will be added to the HEAD section of the document when the form is displayed!
if($cssStyle){
$cssStyle = ( strpos($cssStyle,',') && !strpos($cssStyle,'<style') ) ? explode(',',$cssStyle) : array($cssStyle);
foreach( $cssStyle as $tmp ) $startupSource[]= array($tmp,'css');
}
if($jScript){
$jScript = ( strpos($jScript,',') && !strpos($jScript,'<script') ) ? explode(',',$jScript) : array($jScript);
foreach( $jScript as $tmp )
$startupSource[]= array($tmp,'javascript');
}
//et enfin
//register css and/or javascript
if(isset($startupSource) ) $tveditor->functions->efRegisterStartupBlock($startupSource);
:-)