PHP code example of peak / view

1. Go to this page and download the library: Download peak/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/ */

    

peak / view example snippets


$presentation = new SingleLayoutPresentation(
    '/path/to/layout1.php', 
    '/path/to/view1.php'
);
$data = [
    'title' => 'FooTheBar',
    'name' => 'JohnDoe'
];
$view = new View($data, $presentation);

$output = $view->render();

$presentation = new Presentation([
    '/layout1.php' => [
        '/view1.php',
        '/layout2,php' => [
            '/view2.php',
            '/view3.php',
        ]
    ]
]);

$view->addMacro('formatedName', function() {
    return ucfirst($this->name);
});

...
<h1>Hello <?= $this->formatedName() 

class EscapeTextHelper
{
    public function __invoke($text)
    {
        return filter_var($text, FILTER_SANITIZE_SPECIAL_CHARS);
    }
}

$view->setHelpers([
    'escape' => new EscapeTextHelper(),
    // ...
]);

...
<h1>Hello <?= $this->escape($this->name) 

$view->setDirectives([
    // give you ability to print variable with syntax {{ $myVar }}
    new EchoDirective(), 
    // give you ability to call native php functions, 
    // macros or helpers with syntax @function(...args)
    new FnDirective(),  
]);

$view->addVars([
    'name' => 'bob'
    'messages' => [
        // ...
    ],
    'items' => [
        'property' => 'value'
    ]
]);
$this->layoutContent
html
<div class="container">
    hello <?= $this->name