PHP code example of dalpras / smart-template

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

    

dalpras / smart-template example snippets



/** ./mywebpage.php */
$templateEngine = new TemplateEngine(__DIR__ . '/templates');

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{cols}' => $render['row'](['{text}' => 'hello datum!'])
        ]);
    });

// or you can begin to nest everything you need

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{rows}' => function($render) {
                return $render['row'](['{text}' => 'hello nested!']);
            },
        ]);
    });


// or just use another file for rendering

echo $templateEngine->render('toolbar.php', function ($render) {
        return $render['header']([
            '{text}' => 'hello toolbar!'
        ]);
    });

// also you can use many files as you want

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{rows}' => function($render, TemplateEngine $template) {
                return $template->render('toolbar.php', fn($render) => $render['header']([
                    '{text}' => 'hello different template toolbar'
                ]));
            },
        ]);
    });

// or custom specific functions for rendering

echo $templateEngine->render('table.php', function ($render) {
        return $render['table']([
            '{class}' => 'text-right',
            '{rows}' => $render['myfunc']('hello func!'),
        ]);
    });


// GREAT POWER INVOLVES GREAT RESPONSIBILITY!

/** ./templates/table.php */
return [
    'table' => <<<html
        <div class="table-responsive">
            <table class="table table-sm {class}">
                <tbody>
                    {rows}
                </tbody>
            </table>
        </div>
        html,

    'row' => <<<html
        <tr><td>{text}</tr></td>
        html,
];

/** ./templates/nesteddir/toolbar.php */
return [
    'header' => <<<html
        <div class="toolbar">{text}</div>
        html,
        
    'myfunc' => fn($text) => 'This is your ' . $text
];