Last active
October 14, 2017 03:52
-
-
Save gogl92/c2e947faac3af163078c35e4198d6db8 to your computer and use it in GitHub Desktop.
Easily register CSS in Yii2
This file contains 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 | |
/** | |
* User: yiqing | |
* Date: 2014/12/15 | |
* Time: 0:21 | |
*/ | |
namespace year\widgets; | |
use yii\widgets\Block; | |
class CssBlock extends Block | |
{ | |
/** | |
* @var null | |
*/ | |
public $key = null; | |
/** | |
* @var array $options the HTML attributes for the style tag. | |
*/ | |
public $options = []; | |
/** | |
* Ends recording a block. | |
* This method stops output buffering and saves the rendering result as a named block in the view. | |
*/ | |
public function run() | |
{ | |
$block = ob_get_clean(); | |
if ($this->renderInPlace) { | |
throw new \Exception("not implemented yet ! "); | |
// echo $block; | |
} | |
// $block = trim($block) ; | |
$block = static::unwrapStyleTag($block); | |
$this->view->registerCss($block, $this->options, $this->key); | |
} | |
/** | |
* @param $cssBlock | |
* @return string | |
*/ | |
public static function unwrapStyleTag($cssBlock) | |
{ | |
$block = trim($cssBlock); | |
/* | |
$jsBlockPattern = '|^<script[^>]*>(.+?)</script>$|is'; | |
if(preg_match($jsBlockPattern,$block)){ | |
$block = preg_replace ( $jsBlockPattern , '${1}' , $block ); | |
} | |
*/ | |
$cssBlockPattern = '|^<style[^>]*>(?P<block_content>.+?)</style>$|is'; | |
if (preg_match($cssBlockPattern, $block, $matches)) { | |
$block = $matches['block_content']; | |
} | |
return $block; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment