/slim-smarty-view

Slim Framework 4 view layer built on top of Smarty

Primary LanguagePHPMIT LicenseMIT

Slim Framework 4 Smarty View

Latest Stable Version Total Downloads Latest Unstable Version License

This is a Slim Framework 4 view helper built on top of the Smarty templating component. You can use this component to create and render templates in your Slim Framework 4 application.

How to install

Via Composer

$ composer require scorninpc/slim-smarty-view "2.*"

Example

<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/vendor/autoload.php';


// Create Container
$container = new \DI\Container();
AppFactory::setContainer($container);

// Create the app
$app = AppFactory::create();

// Set view in Container
$container->set("view", function($container) {

	// Create smarty view
	$view = new \Slim\Views\Smarty(
		[
			'template_dir' => [__DIR__ . "/templates"],		// Where to put .tpl files
			'compile_dir' =>  __DIR__ . "/templates_c",		// Where to save compiled

			'cache_dir' =>  __DIR__ . "/templates_c",		// Where to cache
			'caching' => FALSE,								// Enable usa of cache
			'cache_lifetime' => 4600,						// Time for cache

			'force_compile' => TRUE,						// Force to compile .tpl all the time (compile .tpl every time . this is slow for production)
			'debugging' => FALSE,							// Enable debug console
			'compile_check' => TRUE,						// Enable check if need compile (this will check timestamp of file and compile again. set to false for performance)
		]
	);

	return $view;
});

// Route
$app->get('/', function (Request $request, Response $response, $args) {

	return $this->get('view')->render($response, 'index.tpl', [
		'variable' => "Hello!",
	]);
	
});

// Run
$app->run();

Credits

This project is only a fork to add examples and the package on packagist to work with composer, all credits of this nice rework are from Matheus Marques

License

The MIT License (MIT). Please see License File for more information.