Back to StylesRenderer class

Method render

public string
render
(mixed $head, mixed $params = array(), mixed $content = null)
Renders the document stylesheets and style 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 stylesheets and style 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)
{
    $tab = $this->_doc->_getTab();
    $buffer = '';
    $wam = $this->_doc->getWebAssetManager();
    $assets = $wam->getAssets('style', true);
    // Get a list of inline assets and their relation with regular assets
    $inlineAssets = $wam->filterOutInlineAssets($assets);
    $inlineRelation = $wam->getInlineRelation($inlineAssets);
    // Merge with existing styleSheets, for rendering
    $assets = array_merge(array_values($assets), $this->_doc->_styleSheets);
    // Generate stylesheet links
    foreach ($assets as $key => $item) {
        $asset = $item instanceof WebAssetItemInterface ? $item : null;
        // Add href attribute for non Asset item
        if (!$asset) {
            $item['href'] = $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 stylesheet declarations
    foreach ($this->_doc->_style as $type => $contents) {
        // Test for B.C. in case someone still store stylesheet declarations as single string
        if (\is_string($contents)) {
            $contents = [$contents];
        }
        foreach ($contents as $content) {
            $buffer .= $this->renderInlineElement(['type' => $type, 'content' => $content]);
        }
    }
    return ltrim($buffer, $tab);
}