可以在变量中获取Phalcon \ Mvc \ View渲染输出吗?

时间:2012-12-16 13:36:22

标签: php view phalcon

我需要回放json对象,它具有带有渲染动作的属性'html'。 是否有可能与Phalcon本地人一起做?

示例:

$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。关于phalcon php框架的问题: http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View.html

5 个答案:

答案 0 :(得分:6)

首先需要启动输出缓冲:

$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',
    ....
];

答案 1 :(得分:1)

试试这个

$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',
    ....
];

答案 2 :(得分:0)

$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',
    ....
];

别忘了使用

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

否则您可能会返回一个空字符串。

答案 3 :(得分:0)

有一个简单的解决方案,我使用它(在我在模型中使用的应用程序的任何部分): 1.从DI加载视图对象 2.使用带参数的getRender

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

答案 4 :(得分:0)

这是一个基于View的类,呈现为HTML或JSON(Api调用)。

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

更改config / service.php以默认创建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;
});

在控制器中,您可以决定所需的输出类型

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