CLI Views

I was wondering (As its not jumping out at me from the documentation) how one would assign a view to a CLI task. This would be used to render text out to stdout, eg a phtml file that may be XML with vars assigned to it. Then rather then echoing out stuff, I could pipe it cleanly to other apps.

$app->getDI()->set('view', function() use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir($config->application->viewsDir);
    return $view;
});

And the CLI app is not failing. So far so good.. but

Within my task;

$this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);

But this results in

PHP Fatal error: Class 'Modules\Cli\Task\View' not found in

Any help/thoughts/examples welcome



3.0k

Humor me, but did you import the View namespace, as in "use Phalcon\Mvc\View"?



11.0k

@lobostome, you are correct View was not imported at the time. But soon spotted that as well with my tired eyes.

Corrected but still no cigar. Heres a better example

namespace Modules\Cli\Task;
use \Phalcon\Mvc\View as View;

// TaskBase extends \Phalcon\CLI\Task
class MainTask extends TaskBase
{
  // Simple task action to list stuff
     public function listingsAction()
    {
        echo __METHOD__ . PHP_EOL;
        $myvars = array(
            0 => 'Hello',
            1 => 'World'
        );
        $this->view->setVar('myvars', $myvars);
    }
}

The above now does not throw an exception anymore, but nor does it render a view.

./app.php listings
Modules\Cli\Task\MainTask::listingsAction

Maybe I am missing something obvious here... paths checked for views dir etc already. All seems fine.



11.0k

Okay this will work

Bootstrap.php

$app->getDI()->set('view', function() use ($config) {
    $view = new Phalcon\Mvc\View\Simple();
    $view->setViewsDir($config->application->viewsDir . 'tasks/');
    return $view;
});

MainTask.php

namespace Modules\Cli\Task;
use Modules\Legacy\Models\MessagesMerge;
class MainTask extends TaskBase
{
    public function listingsAction()
    {
        $myvars = array(
            0 => 'Hello',
            1 => 'World'
        );
        $this->view->setVar('myvars', $myvars);
        echo $this->view->render('templates/listings');

    }
}

listings.phtml

<?php
foreach($myvars as $k=>$v)
{
    echo $k . ' =>' . $v . PHP_EOL;
}

Output

ap.php listings
Modules\Cli\Task\MainTask::listingsAction
0 =>Hello
1 =>World

It would appear using the simple view works.

Have I missed anything in relation to using the main view (Mvc\View)? TIA



3.0k

In your example with Phalcon\Mvc\View, it seems like you are not setting the render level.



11.0k

@lobostme I set it in the base class. But it did not have and positive effect.



3.0k

Not knowing much about the structure or nature of your application, I would guess that the view does not get rendered.

You could force the output. The flow is:

  1. Start output buffering
  2. Execute the render process
  3. Stop output buffering

For an example, see http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View.html#class-phalcon-mvc-view

or use new Phalcon\Mvc\View\Simple - http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_View_Simple.html

<?php

 $view = new Phalcon\Mvc\View\Simple();
 echo $view->render('templates/my-view', array('content' => $html));


11.0k

Yup went for simple view in the end (See ^^). Works a charm.

Thanks