Google +1

Hello,

I want to implement the Google +1 plugin.

It’s easy to register the script file with


app()->clientScript->registerScriptFile('https://apis.google.com/js/plusone.js');

result is in my header


<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

But I want to use the language option and need to add {lang: ‘de’}


<script type="text/javascript" src="https://apis.google.com/js/plusone.js">  {lang: 'de'}  </script>

I don’t know how to do it with the yii api.

Has someone had a similar problem ?

It is not possible with registerClientScript, it accepts only two parametres: url and position.

Solution is to add your javascript library directly in layout or view file.

Does anybody understand what the google guys do? I’ve never seen scrip tags that define both src attribute and a script value. I just looked it up, and even w3c states that

(see: http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1)

The second thing I don’t understand is, how do they automatically execute scripts defined in the head-section?

Third: Is it a good idea to embed xml-elements (g:plusone) into a non-xhtml page? Even if those elements get replaced by valid html-elements like divs and forms, doesn’t the browser parse the html before executing javascript? And won’t unknown/ invalid elements cause trouble? For years we were told to write valid html pages, and now google comes up with that?

Many people complain about this, I really don’t understand why they didn’t spend more time tring to make code that passing validations.

http://www.google.com/support/forum/p/Webmasters/thread?tid=02a105ac1d76a017&hl=en

It not validates in HTML 5 too.

I made some modification to my yii sources 1.1.7

first I added a new function to CClientScript.


public function registerScriptFileText($url, $text, $position=self::POS_HEAD)

	{

		$this->_hasScripts=true;

		$this->scriptFiles[$position][$url]=array('url'=>$url, 'text'=>$text);

		$params=func_get_args();

		$this->recordCachingAction('clientScript','registerScriptFileText',$params);

		return $this;

	}

Then in CClientScript a modified the functions renderHead, renderBodyBegin, renderBodyEnd and renderCoreScripts. a bit.

At the end some change to CHtml::scriptFile


	public static function scriptFile($url, $text = '')

	{

                return '<script type="text/javascript" src="'.self::encode($url).'">'.$text.'</script>';

	}

To get now something like this


<script type="text/javascript" src="https://apis.google.com/js/plusone.js">  {lang: 'de'}  </script

simply call


app()->clientScript->registerScriptFileText("https://apis.google.com/js/plusone.js", "{lang: 'de'}");

I know it’s not a good idea to change yii sources but may be it’s a nice feature to implement or not ?

You should extend CClientScript and made modifications there. thisway, you will not be able to update your yii framework to future versions.

Yes, but would it not better to contribute the code ?

Hi there,

Yes, I have had the same problem. If you still need to add the Google +1 button to you site I’ve got great news for you: I’ve created an extension that allows you to do exactly what you need. Check it out: yii-google-plusone

In order to use it with german, simply copy paste the following code where you’d like the button to appear:




<?php

$this->widget('ext.yii-google-plusone.GPlusoneButton', array(

              'width'=>'290',

              'annotation'=>'inline',

              'href'=>'YOUR_DOMAIN',

              'lang'=>'de_DE'

));

?>



Comments are welcome as well as reports of any bugs found. Please rank!

Alejandro.