标签云 显示一个日志标签列表,每个标签都可以通过可视化的方式反映其使用频度。
TagCloud 类 ¶我们在 /wwwroot/blog/protected/components/TagCloud.php 文件中创建 TagCloud 类。此文件内容如下:
Yii::import('zii.widgets.CPortlet'); class TagCloud extends CPortlet { public $title='Tags'; public $maxTags=20; protected function renderContent() { $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"; } } }
与 UserMenu portlet 不同, TagCloud portlet 不使用视图。它的前端表现是在 renderContent() 方法中完成的。这是因为其前端表现并不含有很多HTML标签。
我们把每个标签显示为指向带有此标签参数的日志索引页的链接。每个标签链接的文字大小是通过他们与其他标签的相对比重确定的。如果一个标签比其他标签有更高的使用频度,则它会以更大的字体显示。
TagCloud Portlet ¶TagCloud portlet 的使用非常简单。我们把布局文件 /wwwroot/blog/protected/views/layouts/column2.php 修改如下:
...... <div id="sidebar"> <?php if(!Yii::app()->user->isGuest) $this->widget('UserMenu'); <?php $this->widget('TagCloud', array( 'maxTags'=>Yii::app()->params['tagCloudCount'], )); </div> ......
Be the first person to leave a comment
Please login to leave your comment.