1. Go to this page and download the library: Download snicco/templating 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/ */
use Snicco\Component\Templating\TemplateEngine;
// The TemplateEngine accepts one or more instances of ViewFactory.
// See #Using view factories for available implementations.
$view_factory = /* */
$engine = new TemplateEngine($view_factory);
// hello-world is relative to the root directory "templates"
$view = $engine->make('hello-world');
$view1 = $view->with('first_name', 'Calvin');
$output = $engine->renderView($view1);
var_dump($output); // Hello Calvin
$view2 = $view->with('first_name', 'Marlon');
$output = $engine->renderView($view2);
var_dump($output); // Hello Marlon
// Views can also be created by passing an absolute path
$view = $engine->make('/path/to/templates/hello-world.php');
use Snicco\Component\Templating\TemplateEngine;
$view_factory = /* */
$engine = new TemplateEngine($view_factory);
$output = $engine->render('hello-world', ['first_name' => 'Calvin']);
var_dump($output); // Hello Calvin
use Snicco\Component\Templating\TemplateEngine;
$view_factory = /* */
$engine = new TemplateEngine($view_factory);
$view = $engine->make('users.profile');
$output = $engine->render('users.profile', ['first_name' => 'Calvin']);
use Snicco\Component\Templating\Context\ViewContextResolver;
use Snicco\Component\Templating\Context\GlobalViewContext;
$global_context = new GlobalViewContext()
// All templates now have access to a variable called $site_name
$global_context->add('site_name', 'snicco.io');
// The value can be a closure which will be called lazily.
$global_context->add('some_var', fn() => 'some_value');
$context_resolver = new ViewContextResolver($global_context);
use Snicco\Component\Templating\Context\ViewContextResolver;
use Snicco\Component\Templating\Context\GlobalViewContext;
$global_context = new GlobalViewContext()
$global_context->add('app', [
'request' => [
'path' => '/foo',
'query_string' => 'bar=baz'
]
]);
// Inside any template
echo $app['request.path']
echo $app['request.query_string']
$context_resolver = /* */
// Using a closure
$context_resolver->addComposer('hello-world', fn(View $view) => $view->with('foo', 'bar'));
// Using a class that implements ViewComposer
$context_resolver->addComposer('hello-world', HelloWorldComposer::class);
// Adding a composer to multiple views
$context_resolver->addComposer(['hello-world', 'other-view'], fn(View $view) => $view->with('foo', 'bar'));
// Adding a composer by wildcard
// This will make the current user available to all views inside the templates/users directory.
$context_resolver->addComposer('users.*', fn(View $view) => $view->with('current_user', Auth::user()));
use Snicco\Component\Templating\TemplateEngine;
use Snicco\Component\Templating\ViewFactory\PHPViewFactory;
$context_resolver = /* */
$php_view_factory = new PHPViewFactory(
$context_resolver,
[__DIR__.'/templates']
);
$template_engine = new TemplateEngine($php_view_factory);