You can. I frequently use a snippet or a TV to load Javascript and CSS into a page so that it won’t be cluttering up the template if it’s not being used on the page. For example, if I’m using Jot/Notes to manage a list of some sort, I’ll enable the drag/sort feature for manager users only:
<?php
$js = '<script type="text/javascript">
$("#one").dragsort({ dragSelector: "li", dragEnd: saveOrder, dragBetween: false, placeHolderTemplate: "<li></li>" });
function saveOrder() {
var data = new Array();
$("div#sortable li").each(function(i, elm) { data[i] = $(elm).attr("id"); });
$.post("[~18~]", { "ids[]": data });
};
</script>';
if($_SESSION['mgrValidated']) {
$modx->regClientStartupScript('assets/js/jquery.dragsort-0.3.10.min.js');
$modx->regClientScript($js);
}
return;
?>
Or for a page with video, just have this snippet somewhere in the content to load all the javascript and CSS for the video player, as well as loading and preparing a listing of the video files themselves, generated by a desktop application that reads Vimeo and other video storage services:
<?php
// load up the css and js files
$modx->regClientCSS('assets/media/engine/css/videolightbox.css');
$modx->regClientCSS('<style type="text/css">#videogallery a#videolb{display:none}</style>');
$modx->regClientCSS('assets/media/engine/css/overlay-minimal.css');
$modx->regClientStartupScript('assets/media/engine/js/jquery.tools.min.js');
$modx->regClientStartupScript('assets/media/engine/js/swfobject.js');
$modx->regClientStartupScript('assets/media/engine/js/videolightbox.js');
// read and parse the generated file
$filename = $modx->documentObject['alias'];
$file = file_get_contents("assets/media/$filename.html");
$gallery = explode('BODY', $file);
$div = $gallery[1];
$div = ltrim($div, 'section -->');
$div = rtrim($div, '<!-- End VideoLightBox.com');
$div = str_replace('src="data', 'src="assets/media/data', $div);
return $div;
?>
http://wiki.modxcms.com/index.php/API:regClientStartupScript
http://wiki.modxcms.com/index.php/API:regClientScript
http://wiki.modxcms.com/index.php/API:regClientCSS
http://wiki.modxcms.com/index.php/API:regClientStartupHTMLBlock
(regClientHTMLBlock is not documented, but behaves the same as regClientScript)