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.

```php $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;

```php $this->view->setRenderLevel(View::LEVELACTIONVIEW); ```

But this results in

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

Any help/thoughts/examples welcome


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


@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

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


Okay this will work

Bootstrap.php ```php $app->getDI()->set('view', function() use ($config) { $view = new Phalcon\Mvc\View\Simple(); $view->setViewsDir($config->application->viewsDir . 'tasks/'); return $view; }); ``` MainTask.php ```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 <?php foreach($myvars as $k=>$v) { echo $k . ' =>' . $v . PHP_EOL; } ```

Output ```php 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


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


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


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

or use new Phalcon\Mvc\View\Simple -


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


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