繁体   English   中英

如何渲染树枝模板并返回函数zend框架

[英]how to render twig template and return function zend framework

我正在从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' => \ZendTwig\Service\TwigStrategyFactory::class,
         ...
       ),
    )

1)之后,您可以通过以下代码在某些控制器的某些操作中使用Twig:

   function someAction(){
      ...

      $viewModel = new ZendTwig\View\TwigModel(['foo'=>'bar']);
      return $viewModel;
   }

2)设置其他模板:

   function someAction(){
      $viewModel = new ZendTwig\View\TwigModel(['foo'=>'bar']);
      $viewModel->setTemplate('application/controller/name'); //set path here
      return $viewModel;
   }

3)您可以通过TwigModel“ __construct”参数设置数组变量:

   function someAction(){
      $viewModel = new ZendTwig\View\TwigModel($someVariablesArray);
      $viewModel->setTemplate('application/controller/name'); //set path here
      return $viewModel;
   }

4)如果您需要返回html代码,则需要执行以下操作:

  • 在服务配置中再添加一个参数:
   'service_manager' => array(
      'factories' => array(
         ...
         'TwigStrategy' => \ZendTwig\Service\TwigStrategyFactory::class,
         'TwigRenderer'  => \ZendTwig\Service\TwigRendererFactory::class,
         ...
       ),
    )
  • 在您的控制器工厂中添加TwigRenderer服务:
class YourControllerFactory implements FactoryInterface
{
   public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
   {
      return new YourController($twigRenderer);
   } 
}

并在您的Controller中获取twigRenderer:

   private $twigRenderer;

   public function __construct($twigRenderer)
   {
      $this->twigRenderer = $twigRenderer;
   }
  • 之后获取html:
   function someAction(){

      $viewModel = new ZendTwig\View\TwigModel(['foo'=>'bar']);
      $viewModel->setTemplate('mails/order/order_in_process');
      $html = $this->twigRenderer->render($viewModel);
      return $html;
   }

对不起我的英语不好!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM