It is possible to get Phalcon\Mvc\View rendered output in variable?

烈酒焚心 提交于 2019-12-01 19:14:03

问题


I need to give back json object, that has property 'html' with rendered action. Is it possible to do natively with Phalcon vew?

Example:

$posts = NewsPost::find(['limit' => 10]);
$view = new Phalcon\Mvc\View();
$view->setVar('posts', $posts);
$view->setMainView('news/posts'); // not sure if this is correct

// retrieve some data ...
$response = [
    'html' => $view->render(),
    'somedata' => 'somevalues',
    ....
];

P.S. Question regarding phalcon php framework: http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View.html


回答1:


The output buffering needs to be started first:

$view = new Phalcon\Mvc\View();

$view->setVar('posts', $posts);

$view->start();
$view->render(); //Pass a controller/action as parameters if required
$view->finish();

// retrieve some data ...
$response = [
    'html' => $view->getContent(),
    'somedata' => 'somevalues',
    ....
];



回答2:


Try this

$posts = NewsPost::find(['limit' => 10]);
$view = new \Phalcon\Mvc\View();
$view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT);
$view->setVar('posts', $posts);
$viewData = $view->render('news', 'posts');

// retrieve some data ...
$response = [
    'html' => $viewData,
    'somedata' => 'somevalues',
    ....
];



回答3:


$view = new Phalcon\Mvc\View();

$view->setVar('posts', $posts);

$view->start();
$view->render(); //Pass a controller/action as parameters if required
$view->finish();

// retrieve some data ...
$response = [
    'html' => $view->getContent(),
    'somedata' => 'somevalues',
    ....
];

Don't forget to use

$view->setViewsDir(APP_PATH . '/app/views/');

Otherwise you might get a empty string returned.




回答4:


There is a simple solution that I'm using it (in any part of the app I used in a model) : 1. Load the view object from DI 2. Use getRender with parameters

        // Get the view from DI
        $theView = $this->getDi()->getShared('view');
        // Load the text into variable
        $emailText = $theView->getRender('emails', $emailTemplate, $emailData, function($theView) {
            $theView->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
        });



回答5:


Here is a class, based on View, rendered into HTML or JSON (Api call).

use \Phalcon\Mvc\View;

class ApiView extends View
{
    const OUTPUT_DEFAULT = 0;
    const OUTPUT_JSON = 1;
    public $output_type = self::OUTPUT_DEFAULT;

    public function setOutputJson()
    {
        $this->output_type = ApiView::OUTPUT_JSON;
    }

    public function setOutputDefault() {
        $this->output_type = ApiView::OUTPUT_DEFAULT;
    }

    public function render($controllerName, $actionName, $params = null)
    {
        if ($this->output_type === ApiView::OUTPUT_JSON)
        {
            echo json_encode($this->getParamsToView());
            $this->disable();
        }
        parent::render($controllerName, $actionName, $params);
        if ($this->output_type === GollgiView::OUTPUT_JSON) {
            header("Content-type: application/json, 'UTF-8')");
        }
    }

    public function getOutputType() {
        return $this->output_type;
    }
}

Change config/service.php to create by default ApiView

/**
 * Setting up the view component
 */
$di->setShared('view', function () use ($config) {
     $view = new ApiView();
     $view->setViewsDir($config->application->viewsDir);
     $view->registerEngines(['.phtml' => 'Phalcon\Mvc\View\Engine\Php']);
     return $view;
});

In the controller you can decide what type of output you wish for

if ($this->request->has('api')) {
    $this->view->setOutputJson();
}


来源:https://stackoverflow.com/questions/13901805/it-is-possible-to-get-phalcon-mvc-view-rendered-output-in-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!