yii\web\View::renderBodyEndHtml PHP Method

renderBodyEndHtml() protected method

The content is rendered using the registered JS code blocks and files.
protected renderBodyEndHtml ( boolean $ajaxMode ) : string
$ajaxMode boolean whether the view is rendering in AJAX mode. If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions will be rendered at the end of the view like normal scripts.
return string the rendered content
    protected function renderBodyEndHtml($ajaxMode)
    {
        $lines = [];
        if (!empty($this->jsFiles[self::POS_END])) {
            $lines[] = implode("\n", $this->jsFiles[self::POS_END]);
        }
        if ($ajaxMode) {
            $scripts = [];
            if (!empty($this->js[self::POS_END])) {
                $scripts[] = implode("\n", $this->js[self::POS_END]);
            }
            if (!empty($this->js[self::POS_READY])) {
                $scripts[] = implode("\n", $this->js[self::POS_READY]);
            }
            if (!empty($this->js[self::POS_LOAD])) {
                $scripts[] = implode("\n", $this->js[self::POS_LOAD]);
            }
            if (!empty($scripts)) {
                $lines[] = Html::script(implode("\n", $scripts), ['type' => 'text/javascript']);
            }
        } else {
            if (!empty($this->js[self::POS_END])) {
                $lines[] = Html::script(implode("\n", $this->js[self::POS_END]), ['type' => 'text/javascript']);
            }
            if (!empty($this->js[self::POS_READY])) {
                $js = "jQuery(document).ready(function () {\n" . implode("\n", $this->js[self::POS_READY]) . "\n});";
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
            }
            if (!empty($this->js[self::POS_LOAD])) {
                $js = "jQuery(window).on('load', function () {\n" . implode("\n", $this->js[self::POS_LOAD]) . "\n});";
                $lines[] = Html::script($js, ['type' => 'text/javascript']);
            }
        }
        return empty($lines) ? '' : implode("\n", $lines);
    }

Usage Example

Beispiel #1
0
 /**
  * @inheritdoc
  */
 protected function renderBodyEndHtml($ajaxMode)
 {
     $this->getJsData();
     $lines = parent::renderBodyEndHtml($ajaxMode);
     $paths = $this->getRequireJsPaths();
     $requireJsConfig = $this->getRequireJsConfig($paths);
     $jsCode = $this->getRequireJsCode();
     $lines = "<script type=\"text/javascript\">var require = {$requireJsConfig};</script>\n" . $lines;
     $modulesJsArray = json_encode(array_keys($paths));
     $lines .= "<script type=\"text/javascript\">require(['jquery'], function() { require({$modulesJsArray}, function() { {$jsCode} }); });</script>\n";
     return $lines;
 }