duanfei1975 2017-06-01 09:29
浏览 11
已采纳

当我在Yii 1.1中渲染视图时,如何将标题放在元素上方

I have a question in view, I add title and meta by code like below:

$this->pageTitle = 'testTitle';
Yii::app()->clientScript->registerMetaTag('testKeyWords','keywords');

And i get the result like this:

Example code

But i want the title above the keyword,how to do ?

Thanks for help!

  • 写回答

1条回答 默认 最新

  • doutingyou2198 2017-06-02 04:16
    关注

    At our web development company HeavyDots we've got into this requirement too, specially because we wanted to put SEO related tags first so that Google can find them right at the beginning.

    Basically this is what you need to:

    1) Extend the core ClientScript component at framework\web\CClientScript.php

    Create the file protected\components\CustomClientScript.php and put in the following contents:

    <?php
    
    /**
     * This class extends framework\web\CClientScript.php 
     * and overrides some of its methods.
     */
    class CustomClientScript extends CClientScript {
    
      /**
       * Inserts the scripts in the head section.
       * @param string $output the output to be inserted with scripts.
       */
      public function renderHead(&$output) {
        $html = '';
        foreach ($this->metaTags as $meta)
          $html .= CHtml::metaTag($meta['content'], null, null, $meta) . "
    ";
        foreach ($this->linkTags as $link)
          $html .= CHtml::linkTag(null, null, null, null, $link) . "
    ";
        foreach ($this->cssFiles as $url => $media)
          $html .= CHtml::cssFile($url, $media) . "
    ";
        foreach ($this->css as $css)
          $html .= CHtml::css($css[0], $css[1]) . "
    ";
        if ($this->enableJavaScript) {
          if (isset($this->scriptFiles[self::POS_HEAD])) {
            foreach ($this->scriptFiles[self::POS_HEAD] as $scriptFile)
              $html .= CHtml::scriptFile($scriptFile) . "
    ";
          }
    
          if (isset($this->scripts[self::POS_HEAD]))
            $html .= CHtml::script(implode("
    ", $this->scripts[self::POS_HEAD])) . "
    ";
        }
    
        if ($html !== '') {
          $count = 0;
    //      $output = preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is', '<###head###>$1', $output, 1, $count);
          $output = preg_replace('/(<\\/title\s*>)/is', "$1
    <###head###>", $output, 1, $count);
          if ($count)
            $output = str_replace('<###head###>', $html, $output);
          else
            $output = $html . $output;
        }
      }
    
    }
    

    2) Change your configuration so that the application will start using it:

    Add the new component in your configuration file protected\config\main.php:

    ...
    // application components
    'components' => array(
        ...
        'clientScript' => array(
          'class' => 'application.components.CustomClientScript',
        ),
        ...
    

    Additional notes

    Extending application components

    What we have just done is a very common way of customizing Yii without editing core files and as you see it's easy to implement.

    For every application component that you wish to extend just look for the original one in the core files and then make your own.

    We've placed this one in protected\components but you could have placed it wherever you want if your application is big and you want to have a better organized structure.

    As you can see in this component we've overriden the renderHead method and changed just one line that dictates where to insert the head-related code.

    We've made it save it just after </title> to obtain the exact requirement that you have asked for, but as you can guess you could play and change things if you want a different behaviour.

    What if you already had a custom client script

    In many of our projects we were already using a custom client script, for example, the minScript extension.

    In this case, in order to keep both we did this:

    1) Leave the minScript component in the configuration instead of our custom one

    'clientScript' => array(
      'class' => 'ext.minScript.components.ExtMinScript',
    ),
    //    'clientScript' => array(
    //      'class' => 'application.components.CustomClientScript',
    //    ),
    

    2) Make minScript component extend from ours

    //class ExtMinScript extends CClientScript {
    class ExtMinScript extends CustomClientScript {
    

    Or if you don't want to change minScript code:

    1) Autoload the minScript component class by adding this into the import section of the configuration:

    'ext.minScript.components.*',
    

    2) Extend your component from minScript component instead of core one

    Important: Since minScript also has its own implementation of renderHead method in order to keep both you will have to bring the minScript block of code to your component.

    <?php
    
    /**
     * This class extends framework\web\CClientScript.php 
     * and overrides some of its methods.
     */
    class CustomClientScript extends ExtMinScript {
    
      /**
       * Inserts the scripts in the head section.
       * @param string $output the output to be inserted with scripts.
       */
      public function renderHead(&$output) {
    
        // minScript block
        $this -> _minScriptProcessor('scripts', self::POS_HEAD);
        $this -> _minScriptProcessor('css');
        // end minScript block
    
        $html = '';
        foreach ($this->metaTags as $meta)
          $html .= CHtml::metaTag($meta['content'], null, null, $meta) . "
    ";
        foreach ($this->linkTags as $link)
          $html .= CHtml::linkTag(null, null, null, null, $link) . "
    ";
        foreach ($this->cssFiles as $url => $media)
          $html .= CHtml::cssFile($url, $media) . "
    ";
        foreach ($this->css as $css)
          $html .= CHtml::css($css[0], $css[1]) . "
    ";
        if ($this->enableJavaScript) {
          if (isset($this->scriptFiles[self::POS_HEAD])) {
            foreach ($this->scriptFiles[self::POS_HEAD] as $scriptFile)
              $html .= CHtml::scriptFile($scriptFile) . "
    ";
          }
    
          if (isset($this->scripts[self::POS_HEAD]))
            $html .= CHtml::script(implode("
    ", $this->scripts[self::POS_HEAD])) . "
    ";
        }
    
        if ($html !== '') {
          $count = 0;
    //      $output = preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is', '<###head###>$1', $output, 1, $count);
          $output = preg_replace('/(<\\/title\s*>)/is', "$1
    <###head###>", $output, 1, $count);
          if ($count)
            $output = str_replace('<###head###>', $html, $output);
          else
            $output = $html . $output;
        }
      }
    
    }
    

    Final note

    Remember to re-check your implementation if you update Yii or minScript in the future so there are no changes that will break your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题