Blog Tutorial

Hi Guys,

Hopefully this will be a nice easy one.

I’ve been learning Yii for the past 2 weeks, swapping between “Agile Web Application Development with Yii1.1 and PHP5” and the blog tutorial, I’ve almost finished both. I’ve got to the “Creating TagCloud Class” section of the blog tutorial and I’ve hit a problem that is extremely annoying, I don’t know if I’ve been over doing it and can’t see the wood for the trees.




Yii::import('zii.widgets.CPortlet');


class TagCloud extends CPortlet

{

	public $title='Tags';

	public $maxTags=20;


	protected function renderContent()

	{

		//$this->maxTags = 50; //works

		echo "The max tags in the tag cloud is " . $this->maxTags; //test line.

		echo "The max tags in the tag cloud is " . $this->title; //test line.

		$tags=Tag::model()->findTagWeights($this->maxTags);

		foreach($tags as $tag=>$weight)

		{

			$link=CHtml::link(CHtml::encode($tag), array('post/index','tag'=>$tag));

			echo CHtml::tag('span', array(

				'class'=>'tag',

				'style'=>"font-size:{$weight}pt",

			), $link)."\n";

		}

	}

}



This is my code almost exactly the same as in the tutorial but the $maxTags variable just isn’t working. No errors are shown and if I set $maxTags within the function it works fine. When I echo the title in exactly the same way “tags” is displayed correctly.

I would appreciate it if someone can show me where I have been stupid.

Many thanks

Yogibear

Hi yogibear, welcome to the forum.




class TagCloud extends CPortlet

{

	public $title='Tags';

	public $maxTags=20;


	...



These are the attributes of the portlet with their default values.

And you can override them when you create it.




    <?php $this->widget('TagCloud', array(

        'maxTags'=>Yii::app()->params['tagCloudCount'],

    )); ?>



Yii uses consistently the same syntax in defining the attributes when creating an object.

Hi softark

Thank you for your reply.

Am I correct in thinking that




echo "The max tags in the tag cloud is " . $this->maxTags; //test line.



should display 20 (the default value) as I am not overriding it. If I am correct, any ideas why it doesn’t display anything other than “The max tags in the tag cloud is”?

Many thanks

Yogi

Although this is my first thread/post, this forum has already saved me countless times especially with “Agile Web Application Development with Yii1.1 and PHP5” :lol:

Yes.

I think you may be accidentally overriding the value with "" (empty).

Try a constant of “30” instead of “Yii::app()->params[‘tagCloudCount’]” when you create the portlet.




<?php $this->widget('TagCloud', array(

//      'maxTags'=>Yii::app()->params['tagCloudCount'],

        'maxTags'=>30,

    )); ?>



If it has successfully result in “The max tags in the tag cloud is 30”, then you should check your ‘params’ values in your /protected/config/main.php .

Thank you softark you really know your stuff. Its all working now and best of all I understand why.

Thanks again.

Best wishes

Yogi