这是控制器的一些示例代码,它显示一个允许从“打印”通道中选择所有现有类别的表单。
public function indexAction()
{
$channels = $this->channelRepository->getFullChannels();
$selected_channel = null;
/*
* default channels are: 'print', 'mobile' 'ecommerce'
*/
foreach($channels as $channel) {
if('print' == $channel->getCode() ) {
$selected_channel = $channel;
break;
}
}
$categories = [];
/*
* fill-in the array with the values we're interested in
*/
if($selected_channel) {
$category = $selected_channel->getCategory();
$categories_ids = array_merge([$category->getId()], $this->categoryRepository->getAllChildrenIds($category));
foreach($categories_ids as $category_id) {
$category = $this->categoryRepository->find($category_id);
$categories[] = array('id' =>$category->getId(), 'label' => $category->getLabel());
}
}
return $this->templating->renderResponse('CfXmlBundle:Form:index.html.twig', array('categories' => $categories, 'locale' => 'en_US', 'scope' => null));
}
以及相关的 Twig 模板:
<form>
<div style="clear: both; width: 100%;">
<label>Choose a catalog:</label>
<select name="category_id" style="width: 100%;">
{% for category in categories %}
<option value="{{ category.id }}">{{ category.label }}</option>
{% endfor %}
</select>
</div>
<div style="clear: both; width: 100%">
<label>Catalog title</label>
<input type="text" name="title" value="" style="width: 100%;" placeholder="default is choosen catalog name" />
</div>
<div style="clear: both; width: 100%;">
<label>Catalog description</label>
<textarea name="description" style="width: 100%;"></textarea>
</div>
<div style="clear: both;">
<input style="float: left;" type="checkbox" name="prices" value="0" />
<label style="float: left;"> Show prices ?</label>
</div>
<div style="clear: both; text-align:right;">
<input type="submit" value="Generate" />
</div>
</form>