| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- /**
- * Author: lf
- * Blog: https://blog.feehi.com
- * Email: job@feehi.com
- * Created at: 2016-07-08 14:23
- */
- namespace common\widgets;
- use yii\web\View;
- use yii\widgets\Block;
- use yii\base\InvalidParamException;
- class JsBlock extends Block
- {
- /**
- * @var null
- */
- public $key = null;
- /**
- * @var int
- */
- public $pos = View::POS_END;
- /**
- * 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 InvalidParamException("Not implemented yet ! ");
- }
- $block = trim($block);
- /*
- $jsBlockPattern = '|^<script[^>]*>(.+?)</script>$|is';
- if(preg_match($jsBlockPattern,$block)){
- $block = preg_replace ( $jsBlockPattern , '${1}' , $block );
- }
- */
- $jsBlockPattern = '|^<script[^>]*>(?P<block_content>.+?)</script>$|is';
- if (preg_match($jsBlockPattern, $block, $matches)) {
- $block = $matches['block_content'];
- }
- $this->view->registerJs($block, $this->pos, $this->key);
- }
- }
|