Issue
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:
But i want the title above the keyword,how to do ?
Thanks for help!
Solution
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) . "\n";
foreach ($this->linkTags as $link)
$html .= CHtml::linkTag(null, null, null, null, $link) . "\n";
foreach ($this->cssFiles as $url => $media)
$html .= CHtml::cssFile($url, $media) . "\n";
foreach ($this->css as $css)
$html .= CHtml::css($css[0], $css[1]) . "\n";
if ($this->enableJavaScript) {
if (isset($this->scriptFiles[self::POS_HEAD])) {
foreach ($this->scriptFiles[self::POS_HEAD] as $scriptFile)
$html .= CHtml::scriptFile($scriptFile) . "\n";
}
if (isset($this->scripts[self::POS_HEAD]))
$html .= CHtml::script(implode("\n", $this->scripts[self::POS_HEAD])) . "\n";
}
if ($html !== '') {
$count = 0;
// $output = preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is', '<###head###>$1', $output, 1, $count);
$output = preg_replace('/(<\\/title\s*>)/is', "$1\n<###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) . "\n";
foreach ($this->linkTags as $link)
$html .= CHtml::linkTag(null, null, null, null, $link) . "\n";
foreach ($this->cssFiles as $url => $media)
$html .= CHtml::cssFile($url, $media) . "\n";
foreach ($this->css as $css)
$html .= CHtml::css($css[0], $css[1]) . "\n";
if ($this->enableJavaScript) {
if (isset($this->scriptFiles[self::POS_HEAD])) {
foreach ($this->scriptFiles[self::POS_HEAD] as $scriptFile)
$html .= CHtml::scriptFile($scriptFile) . "\n";
}
if (isset($this->scripts[self::POS_HEAD]))
$html .= CHtml::script(implode("\n", $this->scripts[self::POS_HEAD])) . "\n";
}
if ($html !== '') {
$count = 0;
// $output = preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is', '<###head###>$1', $output, 1, $count);
$output = preg_replace('/(<\\/title\s*>)/is', "$1\n<###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.
Answered By - Alexandru Trandafir Catalin
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.