Template Rendering

Gobline provides by default its own template renderer for rendering html templates. It features including multiple layouts, powerful view helpers and sub requests (hmvc).

HTML rendering

In order to render an HTML template, the special route parameter _view must contain an array containing the path to the view template.

<?php
$app
->get('/')
    ->
view(['text/html' => 'view/foo.html.php']);

The model defined in the route will be passed to the view.

<?php
$app
->get('/')
    ->
action(ActionModel\FooActionModel::class)
    ->
view(['text/html' => 'view/foo.html.php']);

The view template will have access to the model simply through the $model variable.

$model->bar

Layouts

Master layout

Html content of your view and layout templates are always wrapped by the Master Layout's html. The Master Layout outputs basic html and dispatch event views like “head element is opened”, “head title element is opened”, etc. These event views can be captured by View Helpers.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
</body>
</html>

Layouts

In order to render layouts, the special route parameter _layouts must contain an array with the list of layouts to render.

<?php
$app
->get('/')
    ->
action(ActionModel\FooActionModel::class)
    ->
view(['text/html' => 'view/foo.html.php'])
    ->
layouts(['view/layout.html.php''view/sub-layout.html.php']);

View Helpers

View Helpers subscribe to view events (fired by the Master Layout) in order to insert html inside specific elements. For example, Gobline\View\Helper\Title\Title View Helper outputs the title inside the title tag. View Helpers are also used to offload logic out of the view templates.

Title

<?php $title('Some Title'); ?>

Css

<?php $css('css/main.css'); ?>

Js

<?php $js('js/script.js'); ?>

Description

<?php $description('Content for the description meta tag'); ?>

Responsive

<?php $responsive(); ?>

NoIndex

<?php $noIndex(); ?>

Meta

<?php $meta([
    
'http-equiv' => 'refresh'
    
'content' => '5; url=http://example.com'
]); ?>

Hreflang

<?php $hreflang(); ?>

Uri

Generate Uri

<?= $route('routeName')->params('key1/value1/key2/value2')->uri(); ?>

Generate absolute Url

<?= $route('routeName')->params('key1/value1/key2/value2')->url(); ?>

Redirect

<?php $route('routeName')->params('key1/value1/key2/value2')->redirect(); ?>

Execute sub request (hmvc)

<?= $route('routeName')->params('key1/value1/key2/value2')->request(); ?>

Check if route is current matched route

<?= $route('routeName')->params('key1/value1/key2/value2')->active(); ?>

BasePath

<?= $basePath?>

Lang

<?= $lang?>

Translate

<?= $translate('hello'); ?>

Escape

<?= $escape($userInput); ?>
<?= $escape->url($userInput); ?>
<?= $escape->css($userInput); ?>
<?= $escape->js($userInput); ?>
<?= $escape->htmlAttr($userInput); ?>

Flash

<?= $flash->get('key'); ?>
<?= $flash->has('key'); ?>
<?php $flash->next('key''value'); ?>

Form

<?php 
$form
->setElementAttributes('class="form-control"')
     ->
setLabelAttributes('class="control-label"')
     ->
setErrorAttributes('class="help-block"')
     ->
setRowWrapper('div''class="form-group"''has-error');
?>

<?= $form->open($model->form'id="userForm"'?>

    <?php $form->openFieldset('user'?>

        <?= $form->row('name'?>

        <?= $form->openFieldset('address'?>

            <?= $form->row('street'?>

            <?= $form->row('city'?>

        <?= $form->closeFieldset() ?>

    <?php $form->closeFieldset() ?>

    <?= $form->element('submit''value="Add" class="btn btn-default"'?>

<?= $form->close() ?> 
?>

Identity

<?= $identity->getLogin(); ?>

Placeholder

<?= $placeholder->get('key'); ?>
<?= $placeholder->set('key''value'); ?>

Custom View Helpers

You can add your own custom View Helpers. In the example below, a View Helper called “Resize” resizes an image and stores it with its new width and height.

<?php
namespace App\ViewHelper\Resize;

use 
Gobline\View\Helper\ViewHelperInterface;
use 
Imagine\Gd\Imagine;
use 
Imagine\Image\ImageInterface;
use 
Imagine\Image\Box;

class 
Resize implements ViewHelperInterface
{
    public function 
__invoke($path$width$height)
    {
        
$posExtension strrpos($path'.');

        
$newPath substr($path0$posExtension)
            .
'_'.$width.'_'.$height.substr($path$posExtension);

        if (
file_exists(getcwd().'/public'.$newPath)) {
            return 
$newPath;
        }

        
$imagine = new Imagine();
        
$image $imagine->open(getcwd().'/public'.$path);

        
$image
            
->thumbnail(new Box($width$height), 
                
ImageInterface::THUMBNAIL_OUTBOUND)
            ->
save(getcwd().'/public'.$newPath);

        return 
$newPath;
    }

    public static function 
getName()
    {
        return 
'resize';
    }
}

Register our new View Helper:

<?php
$app
->getContainer()
    ->
share(Resize::class);

$app->getViewHelperRegistry()
    ->
add(Resize::class);

JSON rendering

In order to render an JSON template, the special route parameter _view must contain an array containing the path to the view template.

<?php
$app
->get('/')
    ->
view([
        
'text/html' => 'view/foo.html.php',
        
'application/json' => 'view/foo.json.php',
    ]);

view/foo.json.php

<?php
return [
    
'foo' => 'bar',
];