我正在从 zend 框架 1 迁移到 3,我有一个返回树枝模板的函数,但我不知道我应该使用什么来渲染 zf3 上的视图树枝模板
如何:
- 使用查看器类
- 设置我的模板路径
- 设置数组以在模板中呈现它
- 退货模板
法典:
protected function convertItemList($aItemList)
{
$aSet = [];
//$config['template_paths'] = [APPLICATION_PATH . '/../library/Core/Backend/SRO/Views/'];
//$oView = new Core_Twig_View($config);
if (!$aItemList) {
return [];
}
foreach ($aItemList as $iKey => $aCurItem) {
$aSpecialInfo = [];
$aInfo = $aCurItem;
$aInfo['info'] = $this->getItemInfo($aCurItem);
$aInfo['blues'] = $this->getBluesStats($aCurItem, $aSpecialInfo);
$aInfo['whitestats'] = $this->getWhiteStats($aCurItem, $aSpecialInfo);
//$oView->assign('aItem', $aInfo);
$i = isset($aCurItem['Slot']) ? $aCurItem['Slot'] : $aCurItem['ID64'];
if ($aCurItem['MaxStack'] > 1) {
$aSet[$i]['amount'] = $aCurItem['Data'];
}
$aSet[$i]['TypeID2'] = $aInfo['TypeID2'];
$aSet[$i]['OptLevel'] = $aInfo['OptLevel'];
$aSet[$i]['RefItemID'] = !isset($aCurItem['RefItemID']) ? 0 : $aCurItem['RefItemID'];
$aSet[$i]['special'] = isset($aInfo['info']['sox']) && $aInfo['info']['sox'] ? true : false;
$aSet[$i]['ItemID'] = $aCurItem['ID64'];
$aSet[$i]['ItemName'] = $aInfo['info']['WebName'];
$aSet[$i]['imgpath'] = $this->getItemIcon($aCurItem['AssocFileIcon128']);
//$aSet[$i]['data'] = $oView->render('itemData.twig');
}
return $aSet;
}
我 https://github.com/OxCom/zf3-twig 使用这个模块。您可以通过 github 说明安装它,并将此参数添加到 zf3 配置数组中:
'service_manager' => array(
'factories' => array(
...
'TwigStrategy' => ZendTwigServiceTwigStrategyFactory::class,
...
),
)
1)在此之后,您可以通过以下代码在某些控制器的某些操作中使用Twig:
function someAction(){
...
$viewModel = new ZendTwigViewTwigModel(['foo'=>'bar']);
return $viewModel;
}
2) 设置其他模板:
function someAction(){
$viewModel = new ZendTwigViewTwigModel(['foo'=>'bar']);
$viewModel->setTemplate('application/controller/name'); //set path here
return $viewModel;
}
3)您可以通过TwigModel"__construct"参数设置数组变量:
function someAction(){
$viewModel = new ZendTwigViewTwigModel($someVariablesArray);
$viewModel->setTemplate('application/controller/name'); //set path here
return $viewModel;
}
4)如果你需要返回html代码,你需要做一些事情:
- 在服务中添加另一个参数:
'service_manager' => array(
'factories' => array(
...
'TwigStrategy' => ZendTwigServiceTwigStrategyFactory::class,
'TwigRenderer' => ZendTwigServiceTwigRendererFactory::class,
...
),
)
- 在控制器工厂中添加 TwigRenderer 服务:
class YourControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new YourController($twigRenderer);
}
}
并在控制器中获取 twigRenderer:
private $twigRenderer;
public function __construct($twigRenderer)
{
$this->twigRenderer = $twigRenderer;
}
- 在此之后获取 html:
function someAction(){
$viewModel = new ZendTwigViewTwigModel(['foo'=>'bar']);
$viewModel->setTemplate('mails/order/order_in_process');
$html = $this->twigRenderer->render($viewModel);
return $html;
}
对不起我的英语!