- Yii 1.1 Application Development Cookbook
- Alexander Makarov
- 304字
- 2021-04-02 18:40:58
Configuring widget defaults
In Yii, code pieces commonly used in views are placed into widgets. For example, a widget can render a tag cloud or provide a custom form input type. Core widgets are highly configurable and are used in views as follows:
<?$this->widget('CLinkPager', array( 'pages' => $pages, 'pageSize' => 15, ))?>
In the preceding code, we are using $this->widget
that calls a CLinkPager
widget with an array of parameters to display a pagination. pages
and pageSize
are both assigned to the corresponding public properties of CLinkPager
before it is being rendered.
Note that we have changed the count of items per page to 15 in our example. If we want our pagination to display 15 items per page on all pages of our application, then we will need to provide a pageSize
parameter with value 15 for all CLinkPager
widget calls. Is there a better way? Definitely, yes.
How to do it…
A Yii web application provides a bunch of components. One of them is a widget factory that since Yii 1.1.3 can be used to set widget defaults.
- Let's use it to set
pageSize
application-wide. We will need to edit the application configuration filemain.php
as follows:return array( … 'components'=>array( 'widgetFactory'=>array( 'widgets'=>array( 'CLinkPager'=>array( 'pageSize'=>15, ), … ), ), … ), );
- Now, the default value for
CLinkPager
'spageSize
will be 15, so if we omit this parameter for all the applicationCLinkPager
s then it will be 15, application-wide. - Moreover, we still can override the
pageSize
value for a specific widget:<?$this->widget('CLinkPager', array( 'pages' => $pages, 'pageSize' => 5, ))?>
This works much like the CSS cascade. You are setting the default overall style in an external file, but are still able to override this through inline styles for individual widgets.
See also
- The recipe named Configuring components in this chapter
- ModelSim電子系統分析及仿真(第3版)
- 突破平面Photoshop UI界面設計與制作剖析
- 剪映:短視頻剪輯/字幕/動畫/AI從新手到高手(手機版+電腦版)
- Photoshop 平面廣告設計從入門到精通
- Moodle JavaScript Cookbook
- 3ds Max/MaxScript印象 腳本動畫制作基礎與應用
- 零基礎學數碼攝影后期
- Unity 3D\2D手機游戲開發:從學習到產品(第4版)
- AutoCAD 2022中文版建筑設計從入門到精通
- Building Websites with PHP/Nuke
- Spark Cookbook 中文版
- VR策劃與編導
- 中文版3ds Max 2022基礎教程
- Flash CC動畫制作與應用(第3版)
- Excel數據處理與分析:數據思維+分析方法+場景應用