2017 © Pedro Peláez
 

library latte-view

Latte Slim wrapper

image

ujpef/latte-view

Latte Slim wrapper

  • Monday, February 19, 2018
  • by odinuv
  • Repository
  • 2 Watchers
  • 3 Stars
  • 199 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 40 % Grown

The README.md

latte-view

Latte templating engine wrapper for Slim microframework., (*1)

You can use this small library to integrate Latte templates into a project based on Slim framework., (*2)

This project was created for course APV on Mendel University in Brno., (*3)

Installation

You can download this library using Composer:, (*4)

composer require ujpef/latte-view

Methods

__construct(Latte\Engine $latte, $pathToTemplates)

Create an instance of Latte wrapper. Pass instance of configured Latte engine. You should configure Latte engine before you pass it to the wrapper: set up templates path and set up cache folder for templates., (*5)

$engine = new \Latte\Engine\Engine();
$engine->setLoader(new \Latte\Loaders\FileLoader(__DIR__ . '/../templates/'));
$engine->setTempDirectory(__DIR__ . '/../cache');

$latteView = new \Ujpef\LatteView\LatteView($engine);

addParam($name, $param)

Make template variable called $name with $param value., (*6)

addParams(array $params)

Pass multiple values into a template. The $params array must be associative., (*7)

render(Response $response, $name, array $params = [])

Render a template given by $name with set of template variables given by $params associative array and create new Response object., (*8)

Returns new instance of Response object which can be returned from route or middleware., (*9)

addFilter($title, callable $callback)

Add a custom Latte filter - {$variable|customFilter}., (*10)

addMacro($name, callable $callback)

Add a custom Latte macro - {customMacro param}., (*11)

Integration with Slim framework

Define a dependency for Slim framework (change templates source folder and cache directory location if needed):, (*12)

use Latte\Engine;
use Latte\Loaders\FileLoader;
use Ujpef\LatteView;

$container['view'] = function ($container) use ($settings) {
    $engine = new Engine();
    $engine->setLoader(new FileLoader(__DIR__ . '/../templates/'));
    $engine->setTempDirectory(__DIR__ . '/../cache');

    $latteView = new LatteView($engine);
    return $latteView;
};

To return result of Latte template rendering call the render() method., (*13)

$app->get('/[{name}]', function (Request $request, Response $response, $args) {
    $tplVars = [
        'variable' => 123
    ];
    return $this->view->render($response, 'index.latte', $tplVars);
})->setName('index');

In template use:, (*14)

<!DOCTYPE html>
<html>
    <head>
        <title>test template</title>
    </head>
    <body>
        contents of variable: {$variable}
    </body>
</html>

Using named routes

To use Slim's build in router in a Latte macro like this {link routeName} add following lines into dependency definition:, (*15)

use Latte\Engine;
use Latte\MacroNode;
use Latte\PhpWriter;
use Latte\Loaders\FileLoader;
use Ujpef\LatteView;

$container['view'] = function ($container) use ($settings) {
    $engine = new Engine();
    $engine->setLoader(new FileLoader(__DIR__ . '/../templates/'));
    $engine->setTempDirectory(__DIR__ . '/../cache');

    $latteView = new LatteView($engine);
    $latteView->addParam('router', $container->router);
    $latteView->addMacro('link', function (MacroNode $node, PhpWriter $writer) {
        if (strpos($node->args, ' ') !== false) {
            return $writer->write("echo \$router->pathFor(%node.word, %node.args);");
        } else {
            return $writer->write("echo \$router->pathFor(%node.word);");
        }
    });
    return $latteView;
};

Remember to set names for your routes:, (*16)

$app->get('/test', function (Request $request, Response $response, $args) {
    //route implementation
})->setName('routeName');

This also works with route placeholders:, (*17)

$app->get('/test/{something}', function (Request $request, Response $response, $args) {
    //route implementation
})->setName('routeName');

In template use:, (*18)

<a href="{link routeName ['something' => 123]}">link</a>

The Versions

19/02 2018

dev-master

9999999-dev

Latte Slim wrapper

  Sources   Download

MIT

The Requires

 

20/08 2017

0.0.2

0.0.2.0

Latte slim wrapper

  Sources   Download

MIT

The Requires

 

20/08 2017

0.0.1

0.0.1.0

Latte slim wrapper

  Sources   Download

MIT

The Requires