PHP code example of devanych / view-renderer

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

    

devanych / view-renderer example snippets


use Devanych\View\Renderer;

$renderer = new Renderer('/path/to/root/directory/of/views');

$content = $renderer->render('path/to/view/file', [
    'variableName' => 'the value of a variable of any type',
]);

echo $content;

$renderer->render('post/show', [
    'post' => $posts->get($id),
]);

<h1><?=$post->getName();

$renderer = new Renderer('/path/to/root/directory/of/views', 'tpl');

<title><?=$this->renderBlock('title');

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?=$this->renderBlock('title');



declare(strict_types=1);

/** @var Devanych\View\Renderer $this */

$this->layout('layouts/main');
$this->block('title', 'Page Title');

<!-- Allowed -->
 $this->beginBlock('parent');

<!-- Output the block content, if it exists  -->
 if ($name = $this->renderBlock('name')): 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?=$this->renderBlock('title');



declare(strict_types=1);

/** @var Devanych\View\Renderer $this */

$this->layout('layouts/main');



declare(strict_types=1);

/** @var Devanych\View\Renderer $this */

$this->layout('layouts/columns');
$this->block('title', 'Page Title');

$renderer->render('site/page', [/* params */]);

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><?=$this->renderBlock('title');

interface ExtensionInterface
{
    /**
     * Returns an array of functions as `function name` => `function callback`.
     *
     * @return array<string, callable>
     */
    public function getFunctions(): array;
}

$extension = new AssetExtension('/path/to/assets', 'https://examle.com/assets', true);
$renderer->addExtension($extension);

// Result: 'https://examle.com/assets/css/style.css?v=1601742615'
$renderer->asset('css/style.css');

// The `$variableName` variable will now be available inside files.
$renderer->addGlobal( 'variableName', 'the value of a variable of any type');

// For all views and layouts, the value is `$author` will equal `Boby`
$renderer->addGlobal( 'author', 'Boby');

// Only for the `blog/page` view, the value is `$author` will be equal to `John`
$renderer->render('blog/page', [
    'author' => 'John',
]);
// or assign a value in the view
$author = 'John';

// Result: '&lt;script&gt;alert(123);&lt;/script&gt;'
$renderer->esc('<script>alert(123);</script>');
// Equivalently to: `htmlspecialchars($content, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', true)`