Git Product home page Git Product logo

borsch-smarty's Introduction

Borsch Smarty Template Renderer

Build Status Latest Stable Version License

An implementation of Smarty for the Borsch's TemplateRendererInterface.

This package is part of the Borsch Framework.

Installation

Via composer :

composer require borschphp/smarty

Integration in Borsch Framework

Open the file config/container.php then add your TemplateRendererInterface definition.

use Borsch\Smarty\Smarty;
use Borsch\Template\TemplateRendererInterface;

/*
 * Template renderer definitions
 * -----------------------------
 *
 * Defining the TemplateRendererInterface here so our HomeHandler handler (see upper) can use it when instantiated.
 * The default Borsch Smarty renderer is used.
 * We cache it so that it is not re-created when fetched a second time.
 */
$container->set(TemplateRendererInterface::class, function () {
    $smarty = new Smarty();
    $smarty->setTemplateDir(__DIR__.'/../resources/templates');
    $smarty->setCompileDir(__DIR__.'/../storage/smarty/templates_c');
    $smarty->setCacheDir(__DIR__.'/../storage/smarty/cache');

    return $smarty;
})->cache(true);

To load it in your handler, add it as a parameter of the constructor :

class HomeHandler implements RequestHandlerInterface
{

    /** @var TemplateRendererInterface */
    protected $renderer;

    /**
     * HomeHandler constructor.
     * @param TemplateRendererInterface $renderer
     */
    public function __construct(TemplateRendererInterface $renderer)
    {
        $this->renderer = $renderer;
    }

    /**
     * @inheritDoc
     */
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return new HtmlResponse($this->renderer->render('home'));
    }
}

Usage

$smarty = new \Borsch\Smarty\Smarty();

// Adding some directories where templates are located
$smarty->addPath(__DIR__.'/templates');
$smarty->addPath(__DIR__.'/templates/error', 'error');
$smarty->addPath(__DIR__.'/templates/success', 'success');

// Assign some variables
$smarty->assign([
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3'
]);

// Display the template (with or without .tpl extension)
echo $smarty->render('template_file_name');

License

The package is licensed under the MIT license. See License File for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.