Last active
October 28, 2021 11:12
-
-
Save mayoz/6f70ef0366cc128ef977 to your computer and use it in GitHub Desktop.
Extend Laravel Blade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace Blade\Extend; | |
| /** | |
| * Added additional features for Laravel Blade template engine | |
| * https://gist.github.com/mayoz/6f70ef0366cc128ef977 | |
| * | |
| * @package Templates | |
| * @version 4.x | |
| * @author Sercan Cakir <[email protected]> | |
| * @link http://www.sercancakir.com | |
| * @license GNU | |
| */ | |
| use Blade; | |
| /** | |
| * Set variable in blade | |
| * | |
| * <code> | |
| * {? $variable = "whatever" ?} | |
| * </code> | |
| * | |
| * @param string | |
| * @param Illuminate\View\Compilers\BladeCompiler | |
| * @return string | |
| */ | |
| Blade::extend( | |
| function($value, $blade) { | |
| return preg_replace('/\{\?(.+)\?\}/', '<?php ${1} ?>', $value); | |
| } | |
| ); | |
| /** | |
| * Use break control structure in blade | |
| * | |
| * <code> | |
| * @break | |
| * </code> | |
| * | |
| * @param string | |
| * @param Illuminate\View\Compilers\BladeCompiler | |
| * @return string | |
| */ | |
| Blade::extend( | |
| function($value, $blade) { | |
| $pattern = $blade->createPlainMatcher('break'); | |
| return preg_replace($pattern, '$1<?php break; ?>$2', $value); | |
| } | |
| ); | |
| /** | |
| * Use continue control structure in blade | |
| * | |
| * <code> | |
| * @continue | |
| * </code> | |
| * | |
| * @param string | |
| * @param Illuminate\View\Compilers\BladeCompiler | |
| * @return string | |
| */ | |
| Blade::extend( | |
| function($value, $blade) { | |
| $pattern = $blade->createPlainMatcher('continue'); | |
| return preg_replace('/(\s*)@continue(\s*)/', '$1<?php continue; ?>$2', $value); | |
| } | |
| ); | |
| /** | |
| * Use continue control structure in blade | |
| * | |
| * <code> | |
| * @foreach($items as $item) | |
| * <div class="{{ @choice('odd', 'even') }}"> | |
| * ... | |
| * </div> | |
| * @endforeach | |
| * </code> | |
| * | |
| * @param string | |
| * @return string | |
| */ | |
| Blade::extend( | |
| function($value, $blade) { | |
| $pattern = $blade->createMatcher('choice'); | |
| return preg_replace($pattern, "$1Blade\Extend\Choice::init$2;", $value); | |
| } | |
| ); | |
| /** | |
| * Choice class | |
| * | |
| * @author Sercan Cakir <[email protected]> | |
| * @link http://laravel.com | |
| * @license GNU | |
| */ | |
| class Choice { | |
| /** | |
| * Keep data and some secret variable | |
| * | |
| * @access private | |
| * @var array | |
| */ | |
| private static $data = []; | |
| /** | |
| * Get current index and increment index | |
| * | |
| * @access private | |
| * @param string $index | |
| * @return string | |
| */ | |
| private static function get($index) | |
| { | |
| $current = self::$data[$index]['index']++; | |
| if (self::$data[$index]['index'] > self::$data[$index]['total'] - 1) { | |
| self::$data[$index]['index'] = 0; | |
| } | |
| return self::$data[$index]['args'][$current]; | |
| } | |
| /** | |
| * Init static alternator class | |
| * | |
| * @param array $param, $param... | |
| * @see self::get | |
| * @see http://www.php.net/manual/en/function.debug-backtrace.php | |
| * @return string | |
| */ | |
| public static function init() | |
| { | |
| $trace = debug_backtrace(); | |
| $caller = array_shift($trace); | |
| $index = $caller['file'].$caller['line']; | |
| if (!isset(self::$data[$index])) { | |
| self::$data[$index] = array( | |
| 'args' => func_get_args(), | |
| 'index' => 0, | |
| 'total' => func_num_args(), | |
| ); | |
| return self::get($index); | |
| } | |
| return self::get($index); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment