Scripte und/oder CSS in den Seitenheader einfügen in MODx

von Thomas Jakobi am Dienstag, 18. Dezember 2007 um 15:52 Uhr. Kommentare (0)

Manchmal ist es sinnvoll nur auf einzelnen Seiten ein Javascript oder CSS-Code im <head>-Abschnitt einer Webseite einzufügen. Dies lässt sich entweder mit einer Template-Variable erreichen oder (falls es sich nur um eine Seite handelt) mit folgenden kleinen Snippets.

Beide Snippets erwarten den Parameter &addcode. Dieser enthält entweder den Namen eines Chunks (in dem das JavaScript bzw. CSS steht) oder den Pfad auf eine externe Javascript- bzw. CSS-Datei.

AddScript.snippet.php
<?php
// AddScript
// Puts the content of a chunk as a Javascript in the head of a document
// License GPL
// Written 12-2007 by thomas.jakobi@partout.info
//
// Parameter:
// &addcode – chunk containing a javascript (will be parsed) or the name of an extenal file
 
$addcode = (isset ( $addcode )) ? $addcode : '';
if ($addcode != '') {
	if ($modx->getChunk ( $addcode )) {
		$addcode = '<script type="text/javascript">
' . $modx->parseDocumentSource ( $modx->getChunk ( $addcode ) ) . '
</script>';
	}
	$modx->regClientStartupScript ( $addcode );
}
?>
AddCSS.snippet.php
<?php
// AddCSS
// Puts the content of a chunk as a CSS in the head of a document
// License GPL
// Written 12-2007 by thomas.jakobi@partout.info
//
// Parameter:
// &addcode – chunk containing CSS-code or the name of an external file
 
$addcode = (isset ( $addcode )) ? $addcode : '';
if ($addcode != '') {
	if ($modx->getChunk ( $addcode )) {
		$addcode = $modx->getChunk ( $addcode );
	}
	$modx->regClientCSS ( $addcode );
}
?>

Speichern auf digg Speichern auf slashdot Speichern auf del.icio.us Speichern auf technorati Speichern auf furl Speichern auf Google Speichern auf Mister Wong

Kommentar schreiben

  • Benötigte Felder sind mit einem Stern (*) markiert.
  • Es ist nicht möglich im Kommentar URLs und Links zu hinterlassen.