sfWidgetFormChoice呈现为无序列表 [英] sfWidgetFormChoice rendered as an unordered list
问题描述
我正在使用symfony 1.4.3
I'm using symfony 1.4.3
是否可以通过某种方式将sfWidgetFormChoice呈现为无序列表?
Is there some way to render a sfWidgetFormChoice as an unordered list?
API中有一个名为"renderer_class"的选项,但我找不到有关它的任何文档或示例.
In the API there is an option called 'renderer_class' but I can't find any documentation or example about it.
谢谢!
推荐答案
以lib/vendor/symfony/lib/widget/sfWidgetFormSelect.class.php
为例.基本上,您需要做的是实现一个扩展sfWidgetFormChoiceBase
的类,并在其中编写方法render()
.一个最小的示例如下所示:
take a look at lib/vendor/symfony/lib/widget/sfWidgetFormSelect.class.php
for example. Basically, what you need to do is implement a class that extends sfWidgetFormChoiceBase
and write a method render()
therein. A minimal example would look like this:
<?php
class sfWidgetFormChoiceUnordered extends sfWidgetFormChoiceBase
{
public function render($name, $value = null, $attributes = array(), $errors = array())
{
$result = '<ul>'
$choices = $this->getChoices();
foreach ($choices as $choice) {
$result .= '<li>' . $choice . '</li>';
}
return $result .= '</ul>';
}
}
您可以将其放在/lib/widget/sfWidgetFormChoiceUnordered.class.php
中.然后,您需要像已经发现的那样在sfWidgetFormChoice
小部件上设置renderer_class
选项.将其设置为我们刚刚编写的类的名称:sfWidgetFormChoiceUnordered
.
You can put this in /lib/widget/sfWidgetFormChoiceUnordered.class.php
. Then you need to set the renderer_class
option on the sfWidgetFormChoice
widget like you already found out. Set it to the name of the class we just wrote: sfWidgetFormChoiceUnordered
.
示例:
...
$this->addWidget('choice', new sfWidgetFormChoice(array(
'renderer_class' => 'sfWidgetFormChoiceUnordered'
));
...
有关render()
参数的文档,请查看我上面发布的示例类.
For documentation on the arguments to render()
, check out the example class I posted above.
这篇关于sfWidgetFormChoice呈现为无序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!