Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:57:20+00:00 2026-06-12T02:57:20+00:00

So far, I have figured out how to return a typical JSON response in

  • 0

So far, I have figured out how to return a typical JSON response in Zend Framework 2. First, I added the ViewJsonStrategy to the strategies section of the view_manager configuration. Then, instead of returning a ViewModel instance from the controller action, I return a JsonModel instance with all my variables set.

Now that I’ve figured that piece out, I need to understand how to render a view and return it within that JSON response. In ZF1, I was able to use $this->view->render($scriptName), which returned the HTML as a string. In ZF2, the Zend\View\View::render(...) method returns void.

So… how can I render an HTML view script and return it in a JSON response in one request?

This is what I have right now:

    if ($this->getRequest()->isXmlHttpRequest()) {
        $jsonModel = new JsonModel(...);

        /* @todo Render HTML script into `$html` variable, and add to `JsonModel` */
        return $jsonModel;
    } else {
        return new ViewModel(...);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:57:22+00:00Added an answer on June 12, 2026 at 2:57 am

    OK, i think i finally understood what you’re doing. I’ve found a solution that i think matches your criteria. Though i am sure that there is room for improvement, as there’s some nasty handwork to be done…

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

    As you can see, the templateMap i create is … nasty … it’s annoying and i’m sure it can be improved by quite a bit. It’s a working solution but just not a clean one. Maybe somehow one would be able to grab the, probably already instantiated, default PhpRenderer from the ServiceLocator with it’s template- and path-mapping and then it should be cleaner.

    Thanks to the comment ot @DrBeza the work needed to be done could be reduced by a fair amount. Now, as I’d initially wanted, we will grab the viewrenderer with all the template mapping intact and simply render the ViewModel directly. The only important factor is that you need to specify the fully qualified template to render (e.g.: “$module/$controller/$action”)

    I hope this will get you started though 😉

    PS: Response looks like this:

    Object:
        html: "<h1>Hello World</h1>"
        jsonArray: Array[6]
        jsonVar1: "jsonVal2"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been putting only jQuery mask so far, but I figured out that
So far I've figured out how to pass Unicode strings, bSTRs, to and from
I am using Zend Framework 1.9.6. I think I've got it pretty much figured
I have now nearly figured out how to Filter a NSTreeController, to do this
I have a QTreeView, and I've figured out how to set its style by
I'm trying to make a custom linked list, so far I've figured out how
I'm a new user to knockout.js and so far have been very impressed with
So far I have only been updated a view from within its controller. I
So far I have the expression: http://[^\.]*\.mydomain\.com/((.*?)) Which matches... http://www.mydomain.com/Images/favicon.ico But I really dont
So far I have been creating Web Portal but recently I had a request

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.