PHP code example of ingenerator / kohana-view

1. Go to this page and download the library: Download ingenerator/kohana-view library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ingenerator / kohana-view example snippets


Kohana::modules([
  'existing'    => 'existing/modules/call/here',
  'kohana-view' => __DIR__.'/../vendor/ingenerator/kohana-view'
]);


//application/classes/View/Hello/WorldView.php
namespace View\Hello;

/**
 * @property-read string  $name       automatically returned from the $variables array
 * @property-read boolean $is_morning automatically returned from the var_is_morning method
 */
class WorldView extends \Ingenerator\KohanaView\ViewModel\AbstractViewModel
{
    protected $variables = [
        'name' => NULL,
    ];

    protected function var_is_morning()
    {
        $date = new \DateTime;
        return ($date->format('H') < 12);
    }

}


//application/views/hello/hello_world.php
/**
 * @var \View\Hello\WorldView                         $view
 * @var \Ingenerator\KohanaView\Renderer\HTMLRenderer $renderer
 */


//application/classes/Controller/Welcome.php
class Controller_Welcome extends Controller
{
  public function action_index()
  {
    $view = new View\Hello\WorldView;
    $view->display(['name' => $this->request->query('name')]);
    $renderer = $this->dependencies->get('kohanaview.renderer.html');
    /** @var \Ingenerator\KohanaView\Renderer\HTMLRenderer */
    $this->response->body($renderer->render($view));
  }
}


namespace View\Layout;

/**
 * @property-read string $body_html
 * @propery-read  string $title
 */
class SitePageTemplateView extends Ingenerator\KohanaView\ViewModel\PageLayout\AbstractPageLayoutView
{
}


namespace View\Layout;

/**
 * @property-read ViewModel $sidebar  
 */
class ContentWithSidebarLayoutView extends Ingenerator\KohanaView\ViewModel\PageLayout\AbstractIntermediateLayoutView
{
    public function __construct(SitePageTemplateView $page, ViewModel $sidebar) 
    {
        parent::__construct($page);
        $this->sidebar = $sidebar;
    }
    
    protected function var_sidebar()
    {
        return $this->sidebar;
    }
}


namespace View\Layout;

class SidebarView extends AbstractViewModel
{
    // Whatever you want it to show
}


namespace View\Pages;

/**
 * @property-read View\Layout\SitePageTemplateView $page
 * @property-read string                           $name
 */
class HelloWorldView extends Ingenerator\KohanaView\ViewModel\PageLayout\AbstractNestedChildView
{
  protected $variables = [
    'name' => NULL
  ];
  
  protected function var_page()
  {
      // If you want to make this available to set things from the view : it's not 


//application/views/site_page_template.php
/**
 * @var \View\Layout\SitePageTemplateView $view
 * @var \Ingenerator\KohanaView\Renderer\HTMLRenderer $renderer
 */


//application/views/content_with_sidebar_layout.php
/**
 * @var \View\Layout\ContentWithSidebarLayout $view
 * @var \Ingenerator\KohanaView\Renderer\HTMLRenderer $renderer
 */


//application/views/pages/hello_world.php
/**
 * @var \View\Pages\HelloWorldView                    $view
 * @var \Ingenerator\KohanaView\Renderer\HTMLRenderer $renderer
 */
// You can do this here if you want to keep templatey-type stuff together
// Or in your view model at display time if it's a bit more involved
$view->page->setTitle('Hello World');


class Controller_Welcome extends Controller // Look, extend any controller! No more Controller_Template!
{
  public function action_index()
  {
    // You probably want to put your views into the dependency container too
    $content = new HelloWorldView(
        new ContentWithSidebarLayoutView(
            new SitePageTemplateView(),
            new SidebarView()
        )
    );
    $content->display(['name' => $this->request->query('name')]);
    $renderer = $this->dependencies->get('kohanaview.renderer.page_layout');
    /** @var \Ingenerator\KohanaView\Renderer\PageLayoutRenderer $renderer */

    $this->response->body($renderer->render($content));
  }
}

class View_Something extends AbstractViewModel {
  protected $default_variables = [
    'title' => 'My page title',
  ];

  protected $variables = [
    'caption' => NULL
  ];

}

print $view->title;    // 'My page title'
print $view->caption;  // ''

$view->display(['caption' => 'Something', 'title' => 'A title']);
print $view->title;    // 'A title'
print $view->caption;  // 'Something'

$view->display(['caption' => 'Something else']);
print $view->title;    // 'My page title'
print $view->caption;  // 'Something else'



class View_That_Does_Work {
  protected $variables = [
    'user_email' => ''
  ];

  protected function var_user_activity()
  {
    $activity = [];
    foreach ($this->database->loadActivityForUser($this->user_email) as $activity) {
      $activity[] = (string) $activity;
    }
    $this->variables['user_activity'] = $activity;
    // Future usage of $view->user_activity will now get the value cached in the variables array without calling
    // this method again.
    return $activity;
  }
}


class View_Container {
  protected $variables = [
    'users' => [],
  ];

  public function __construct(View_User_FaceWidget $face_widget)
  {
    $this->face_widget = $face_widget;
  }

  protected function var_face_widget()
  {
    return $this->face_widget;
  }
}


//application/views/container.php
/**
 * @var \View_Container                               $view
 * @var \Ingenerator\KohanaView\Renderer\HTMLRenderer $renderer
 */