PHP code example of geggleto / slim-renderer

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

    

geggleto / slim-renderer example snippets


//Construct the View
$renderer = new PhpRenderer('path/to/templates');

$viewData = [
    'key1' => 'value1',
    'key2' => 'value2',
];

// Render a template
$response = $renderer->render(new Response(), 'hello.php', $viewData);

use Slim\AppFactory;
use Slim\Views\PhpRenderer;

, function ($request, $response) {
    $renderer = new PhpRenderer('path/to/templates');
    
    $viewData = [
        'name' => 'John',
    ];
    
    return $renderer->render($response, 'hello.php', $viewData);
});

$app->run();



use Psr\Container\ContainerInterface;
use Slim\Views\PhpRenderer;
// ...

return [
    PhpRenderer::class => function (ContainerInterface $container) {
        $renderer = new PhpRenderer('path/to/templates');

        return $renderer;
    },
];


// Via the constructor
$globalViewData = [
    'title' => 'Title'
];

$renderer = new PhpRenderer('path/to/templates', $globalViewData);

// or setter
$viewData = [
    'key1' => 'value1',
    'key2' => 'value2',
];
$renderer->setAttributes($viewData);

// or individually
$renderer->addAttribute($key, $value);

$viewData = [
    'title' => 'Title'
];
$renderer = new PhpRenderer('path/to/templates', $viewData);

//...

$response = $renderer->render($response, $template, [
    'title' => 'My Title'
]);

// In the view above, the $title will be "My Title" and not "Title"

<?=$this->fetch('./path/to/partial.phtml', ['name' => 'John'])

<html><head><title><?=$title

Hello <?=$name

$renderer = new PhpRenderer('path/to/templates', ['title' => 'My App']);
$renderer->setLayout('layout.php');

$viewData = [
    'title' => 'Hello - My App',
    'name' => 'John',
];

//...

$response = $renderer->render($response, 'hello.php', $viewData);

function html(?string $text = null): string
{
    return htmlspecialchars($text ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

Hello <?= html($name) 

composer