Overview

Namespaces

  • Charcoal
    • View
      • Mustache
      • Php
      • Twig

Classes

  • Charcoal\View\AbstractEngine
  • Charcoal\View\AbstractLoader
  • Charcoal\View\AbstractView
  • Charcoal\View\GenericView
  • Charcoal\View\Mustache\AssetsHelpers
  • Charcoal\View\Mustache\MustacheEngine
  • Charcoal\View\Mustache\MustacheLoader
  • Charcoal\View\Mustache\TranslatorHelpers
  • Charcoal\View\Php\PhpEngine
  • Charcoal\View\Php\PhpLoader
  • Charcoal\View\Renderer
  • Charcoal\View\Twig\TwigEngine
  • Charcoal\View\Twig\TwigLoader
  • Charcoal\View\ViewConfig
  • Charcoal\View\ViewServiceProvider

Interfaces

  • Charcoal\View\EngineInterface
  • Charcoal\View\LoaderInterface
  • Charcoal\View\Mustache\HelpersInterface
  • Charcoal\View\ViewableInterface
  • Charcoal\View\ViewInterface

Traits

  • Charcoal\View\ViewableTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\View\Mustache;
  4: 
  5: use \Mustache_LambdaHelper as LambdaHelper;
  6: 
  7: /**
  8:  * Mustache helpers for rendering CSS and JavaScript.
  9:  */
 10: class AssetsHelpers implements HelpersInterface
 11: {
 12:     /**
 13:      * A string concatenation of inline `<script>` elements.
 14:      *
 15:      * @var string $js
 16:      */
 17:     private static $js = '';
 18: 
 19:     /**
 20:      * An array of `<script>` elements referencing external scripts.
 21:      *
 22:      * @var array $jsRequirements
 23:      */
 24:     private static $jsRequirements = [];
 25: 
 26:     /**
 27:      * A string concatenation of inline `<style>` elements.
 28:      *
 29:      * @var string $css;
 30:      */
 31:     private static $css = '';
 32: 
 33:     /**
 34:      * An array of `<link>` elements referencing external style sheets.
 35:      *
 36:      * @var array $cssRequirements
 37:      */
 38:     private static $cssRequirements = [];
 39: 
 40:     /**
 41:      * Retrieve the collection of helpers.
 42:      *
 43:      * @return array
 44:      */
 45:     public function toArray()
 46:     {
 47:         return [
 48:             'addJs' => function($js, LambdaHelper $helper) {
 49:                 $this->addJs($js, $helper);
 50:             },
 51:             'js' => function() {
 52:                 return $this->js();
 53:             },
 54:             'addJsRequirement' => function($js, LambdaHelper $helper) {
 55:                 $this->addJsRequirement($js, $helper);
 56:             },
 57:             'jsRequirements' => function() {
 58:                 return $this->jsRequirements();
 59:             },
 60:             'addCss' => function($css, LambdaHelper $helper) {
 61:                 $this->addCss($css, $helper);
 62:             },
 63:             'css' => function() {
 64:                 return $this->css();
 65:             },
 66:             'addCssRequirement' => function($css, LambdaHelper $helper) {
 67:                 $this->addCssRequirement($css, $helper);
 68:             },
 69:             'cssRequirements' => function() {
 70:                 return $this->cssRequirements();
 71:             }
 72:         ];
 73:     }
 74: 
 75:     /**
 76:      * Enqueue (concatenate) inline JavaScript content.
 77:      *
 78:      * Must include `<script>` surrounding element.
 79:      *
 80:      * @param string       $js     The JavaScript to add.
 81:      * @param LambdaHelper $helper For rendering strings in the current context.
 82:      * @return void
 83:      */
 84:     public function addJs($js, LambdaHelper $helper = null)
 85:     {
 86:         if ($helper !== null) {
 87:             $js = $helper->render($js);
 88:         }
 89:         self::$js .= $js;
 90:     }
 91: 
 92:     /**
 93:      * Get the saved inline JavaScript content and purge the store.
 94:      *
 95:      * @return string
 96:      */
 97:     public function js()
 98:     {
 99:         $js = self::$js;
100:         self::$js = '';
101:         return $js;
102:     }
103: 
104:     /**
105:      * Enqueue an external JavaScript file.
106:      *
107:      * Must include `<script>` surrounding element.
108:      *
109:      * @param string       $js     The JavaScript to add.
110:      * @param LambdaHelper $helper For rendering strings in the current context.
111:      * @return void
112:      */
113:     public function addJsRequirement($js, LambdaHelper $helper = null)
114:     {
115:         $js  = trim($js);
116:         $key = md5($js);
117: 
118:         if (!isset(self::$jsRequirements[$key])) {
119:             if ($helper !== null) {
120:                 $js = $helper->render($js);
121:             }
122: 
123:             self::$jsRequirements[$key] = $js;
124:         }
125:     }
126: 
127:     /**
128:      * Get the JavaScript requirements and purge the store.
129:      *
130:      * @return string
131:      */
132:     public function jsRequirements()
133:     {
134:         $req = implode("\n", self::$jsRequirements);
135:         self::$jsRequirements = [];
136:         return $req;
137:     }
138: 
139:     /**
140:      * Enqueue (concatenate) inline CSS content.
141:      *
142:      * Must include `<style>` surrounding element.
143:      *
144:      * @param string       $css    The CSS string to add.
145:      * @param LambdaHelper $helper For rendering strings in the current context.
146:      * @return void
147:      */
148:     public function addCss($css, LambdaHelper $helper = null)
149:     {
150:         if ($helper !== null) {
151:             $css = $helper->render($css);
152:         }
153:         self::$css .= $css;
154:     }
155: 
156:     /**
157:      * Get the saved inline CSS content and purge the store.
158:      *
159:      * @return string
160:      */
161:     public function css()
162:     {
163:         $css = self::$css;
164:         self::$css = '';
165:         return $css;
166:     }
167: 
168:     /**
169:      * Enqueue an external CSS file.
170:      *
171:      * Must include `<link />` or surrounding `<style>` element.
172:      *
173:      * @param string       $css    The CSS requirements.
174:      * @param LambdaHelper $helper For rendering strings in the current context.
175:      * @return void
176:      */
177:     public function addCssRequirement($css, LambdaHelper $helper = null)
178:     {
179:         $css = trim($css);
180:         $key = md5($css);
181: 
182:         if (!isset(self::$cssRequirements[$key])) {
183:             if ($helper !== null) {
184:                 $css = $helper->render($css);
185:             }
186: 
187:             self::$cssRequirements[$key] = $css;
188:         }
189:     }
190: 
191:     /**
192:      * Get the CSS requirements and purge the store.
193:      *
194:      * @return string
195:      */
196:     public function cssRequirements()
197:     {
198:         $req = implode("\n", self::$cssRequirements);
199:         self::$cssRequirements = [];
200:         return $req;
201:     }
202: }
203: 
API documentation generated by ApiGen