symfony /src/Symfony/Components/Templating/Helper/StylesheetsHelper.php

Language PHP Lines 113
MD5 Hash 5483f5763956c09de6fd01118a512914
Repository https://github.com/come/symfony.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php

namespace Symfony\Components\Templating\Helper;

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * StylesheetsHelper is a helper that manages stylesheets.
 *
 * Usage:
 *
 * <code>
 *   $this->stylesheets->add('foo.css', array('media' => 'print'));
 *   echo $this->stylesheets;
 * </code>
 *
 * @package    Symfony
 * @subpackage Components_Templating
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 */
class StylesheetsHelper extends Helper
{
    protected $stylesheets = array();
    protected $assetHelper;

    /**
     * Constructor.
     *
     * @param AssetsHelper $assetHelper A AssetsHelper instance
     */
    public function __construct(AssetsHelper $assetHelper)
    {
        $this->assetHelper = $assetHelper;
    }

    /**
     * Adds a stylesheets file.
     *
     * @param string $stylesheet A stylesheet file path
     * @param array  $attributes An array of attributes
     */
    public function add($stylesheet, $attributes = array())
    {
        $this->stylesheets[$this->assetHelper->getUrl($stylesheet)] = $attributes;
    }

    /**
     * Returns all stylesheet files.
     *
     * @return array An array of stylesheet files to include
     */
    public function get()
    {
        return $this->stylesheets;
    }

    /**
     * Returns HTML representation of the links to stylesheets.
     *
     * @return string The HTML representation of the stylesheets
     */
    public function render()
    {
        $html = '';
        foreach ($this->stylesheets as $path => $attributes) {
            $atts = '';
            foreach ($attributes as $key => $value) {
                $atts .= ' '.sprintf('%s="%s"', $key, htmlspecialchars($value, ENT_QUOTES, $this->charset));
            }

            $html .= sprintf('<link href="%s" rel="stylesheet" type="text/css"%s />', $path, $atts)."\n";
        }

        return $html;
    }

    /**
     * Outputs HTML representation of the links to stylesheets.
     *
     */
    public function output()
    {
        echo $this->render();
    }

    /**
     * Returns a string representation of this helper as HTML.
     *
     * @return string The HTML representation of the stylesheets
     */
    public function __toString()
    {
        return $this->render();
    }

    /**
     * Returns the canonical name of this helper.
     *
     * @return string The canonical name
     */
    public function getName()
    {
        return 'stylesheets';
    }
}
Back to Top