【问题标题】:how to render twig template and return function zend framework如何渲染树枝模板并返回函数zend框架
【发布时间】:2019-06-09 07:14:42
【问题描述】:

我正在从 zend 框架 1 迁移到 3 ,并且我有一个返回 twig 模板的函数,但我不知道应该使用什么来在 zf3 上渲染视图 twig 模板

如何:

  • 使用查看器类
  • 设置我的模板路径
  • 设置数组以在模板中呈现它
  • 返回模板

代码:

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;
}

【问题讨论】:

    标签: php zend-framework twig render viewmodel


    【解决方案1】:

    我使用这个模块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);
       } 
    }
    

    并在您的控制器中获取 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;
       }
    

    对不起我的英语!

    【讨论】:

      猜你喜欢
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多