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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。