Skip to content

elvispdosreis/Smarty-View

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Slim Framework 3 Smarty View

Build Status Latest Stable Version Total Downloads License

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

Install

Via Composer

$ composer require mathmarques/smarty-view

Requires Slim Framework 3 and PHP 5.5.0 or newer.

Usage

// Create Slim app
$app = new \Slim\App();

// Fetch DI Container
$container = $app->getContainer();

// Register Smarty View helper
$container['view'] = function ($c) {
    $view = new \Slim\Views\Smarty('path/to/templates', [
        'cacheDir' => 'path/to/cache',
        'compileDir' => 'path/to/compile',
        'pluginsDir' => ['path/to/plugins', 'another/path/to/plugins']
    ]);
    
    // Add Slim specific plugins
    $smartyPlugins = new \Slim\Views\SmartyPlugins($c['router'], $c['request']->getUri());
    $view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']);
    $view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']);

    return $view;
};

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->view->render($response, 'profile.tpl', [
        'name' => $args['name']
    ]);
})->setName('profile');

// Run app
$app->run();

Custom template functions

This component exposes a custom path_for() and base_url() functions to your Smarty templates.

You can use path_for() function to generate complete URLs to any Slim application named route. This is an example Smarty template:

{extends 'layout.tpl'}

{block name=body}
<h1>User List</h1>
<ul>
    <li><a href="{path_for name="profile" data=["name" => "Matheus"]}">Matheus</a></li>
</ul>
{/block}

Testing

phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

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

About

Slim Framework 3 view layer built on top of Smarty

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.3%
  • Smarty 0.7%