chrisdempsey
10/12/2016 - 4:00 PM

activateAssets.php

<?php
/**
 * activateAssets
 *
 * DESCRIPTION
 *
 * Activates assets registered in placeholder.
 *
 * PROPERTIES:
 *
 * None, works from placeholder created by registerAssets
 *
 * USAGE:
 *
 * [[-activateAssets]]
 *
 */

if (!isset($modx)) return;

// head.js handles css and js at the same time
$files = $modx->getPlaceholder('placeholderAssets');

if (!empty($files)) {
    // write filenames to log for debugging
    // $modx->log(modX::LOG_LEVEL_ERROR, '[placeholderAssets] activated files: ' . $files);
    
    // remove trailing pipes from $files string
    $files = rtrim($files,'||');
    
    // strip white spae from $files
    $files = preg_replace('/\s+/', '', $files);
    
    // create array of files by splitting file_list on pipes
    $arr_files = explode("||", $files);
    
    // loop through array and register file by type
    foreach ($arr_files as $file) {
        $output .= "head.load( '$file' );\n";
    }
}

// if output has been generated register ClientHTMLBlock and inject before the closing BODY tag
if (!empty($output)) $modx->regClientHTMLBlock("<script type='text/javascript'>\n$output\n</script>");