- 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
- 持續(xù)演進(jìn)的Cloud Native:云原生架構(gòu)下微服務(wù)最佳實(shí)踐
- Excel 2013使用詳解(修訂版)
- Flash CS6標(biāo)準(zhǔn)教程(全視頻微課版)
- 中文版Premiere Pro CC入門教程
- 改變思維:菜鳥也能做出震撼PPT(全彩版)
- ChronoForms 3.1 for Joomla! site Cookbook
- Adobe創(chuàng)意大學(xué)InDesign CS5 版式設(shè)計(jì)師標(biāo)準(zhǔn)實(shí)訓(xùn)教材
- After Effects CS6入門與提高
- 輕松玩轉(zhuǎn)3D One AI
- AutoCAD 2022中文版建筑設(shè)計(jì)從入門到精通
- ANSYS Workbench中文版超級(jí)學(xué)習(xí)手冊(cè)
- 三維建模與3D打印從入門到精通
- 3D打印輕松實(shí)踐:從材料應(yīng)用到三維建模
- SharePoint Designer Tutorial: Working with SharePoint Websites
- CINEMA 4D新手快速進(jìn)階實(shí)例教學(xué)