Back to ScriptsRenderer class

Method render

public string
render
(mixed $head, mixed $params = array(), mixed $content = null)
Renders the document script tags and returns the results as a string
Parameters
  • string $head (unused)
  • array $params Associative array of values
  • string $content The script
Returns
  • string The output of the script
Since
  • 4.0.0

Method render - Source code

/**
 * Renders the document script tags and returns the results as a string
 *
 * @param   string  $head     (unused)
 * @param   array   $params   Associative array of values
 * @param   string  $content  The script
 *
 * @return  string  The output of the script
 *
 * @since   4.0.0
 */
public function render($head, $params = array(), $content = null)
{
    // Get line endings
    $lnEnd = $this->_doc->_getLineEnd();
    $tab = $this->_doc->_getTab();
    $buffer = '';
    $wam = $this->_doc->getWebAssetManager();
    $assets = $wam->getAssets('script', true);
    // Get a list of inline assets and their relation with regular assets
    $inlineAssets = $wam->filterOutInlineAssets($assets);
    $inlineRelation = $wam->getInlineRelation($inlineAssets);
    // Merge with existing scripts, for rendering
    $assets = array_merge(array_values($assets), $this->_doc->_scripts);
    // Generate script file links
    foreach ($assets as $key => $item) {
        // Check whether we have an Asset instance, or old array with attributes
        $asset = $item instanceof WebAssetItemInterface ? $item : null;
        // Add src attribute for non Asset item
        if (!$asset) {
            $item['src'] = $key;
        }
        // Check for inline content "before"
        if ($asset && !empty($inlineRelation[$asset->getName()]['before'])) {
            foreach ($inlineRelation[$asset->getName()]['before'] as $itemBefore) {
                $buffer .= $this->renderInlineElement($itemBefore);
                // Remove this item from inline queue
                unset($inlineAssets[$itemBefore->getName()]);
            }
        }
        $buffer .= $this->renderElement($item);
        // Check for inline content "after"
        if ($asset && !empty($inlineRelation[$asset->getName()]['after'])) {
            foreach ($inlineRelation[$asset->getName()]['after'] as $itemBefore) {
                $buffer .= $this->renderInlineElement($itemBefore);
                // Remove this item from inline queue
                unset($inlineAssets[$itemBefore->getName()]);
            }
        }
    }
    // Generate script declarations for assets
    foreach ($inlineAssets as $item) {
        $buffer .= $this->renderInlineElement($item);
    }
    // Generate script declarations for old scripts
    foreach ($this->_doc->_script as $type => $contents) {
        // Test for B.C. in case someone still store script declarations as single string
        if (\is_string($contents)) {
            $contents = [$contents];
        }
        foreach ($contents as $content) {
            $buffer .= $this->renderInlineElement(['type' => $type, 'content' => $content]);
        }
    }
    // Output the custom tags - array_unique makes sure that we don't output the same tags twice
    foreach (array_unique($this->_doc->_custom) as $custom) {
        $buffer .= $tab . $custom . $lnEnd;
    }
    return ltrim($buffer, $tab);
}