好吧,我想我终于明白了你在做什么。我找到了符合您条件的解决方案。尽管我确信仍有改进的空间,但仍有一些麻烦的工作要做…
public function indexAction(){ if (!$this->getRequest()->isXmlHttpRequest()) { return array(); } $htmlViewPart = new ViewModel(); $htmlViewPart->setTerminal(true) ->setTemplate('module/controller/action') ->setVariables(array( 'key' => 'value' )); $htmlOutput = $this->getServiceLocator() ->get('viewrenderer') ->render($htmlViewPart); $jsonModel = new JsonModel(); $jsonModel->setVariables(array( 'html' => $htmlOutput, 'jsonVar1' => 'jsonVal2', 'jsonArray' => array(1,2,3,4,5,6) )); return $jsonModel;}如您所见,我创建的templateMap令人讨厌……令人讨厌,我敢肯定它可以进行很多改进。
这是一个可行的解决方案,但不是一个干净的解决方案。也许可以以某种方式能够从ServiceLocator中获取其可能已经实例化的默认PhpRenderer及其模板和路径映射,然后它应该更干净。
由于@DrBeza的评论,需要做的工作可以减少很多。现在,正如我最初想要的那样,我们将在所有模板映射完好无损的情况下抓取viewrenderer,并直接渲染ViewModel。唯一重要的因素是您需要指定要渲染的完全合格的模板(例如:“
$ module / $ controller / $ action”)
我希望这会让您入门;)
PS:响应如下所示:
Object: html: "<h1>Hello World</h1>" jsonArray: Array[6] jsonVar1: "jsonVal2"



