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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab