Git Product home page Git Product logo

tooolooop's Introduction

Tooolooop


Build status Code coverages Code climate Latest Stable Version Total Downloads

PHP7 lightweight native templates.

Installation

via Composer:

composer require romanzaycev/tooolooop

Usage

<?php declare(strict_types = 1);

require "vendor/autoload.php";

use Romanzaycev\Tooolooop\Engine;

$engine = new Engine(__DIR__ . '/views');

$template = $engine->make('page');
$template->assign(['text' => 'Lorem ipsum']);

echo $template->render();

views/page.php:

<?php $this->extend('layout') ?>

<section>
  <?=$this->e($text)?>
</section>

<?php $this->start('footer') ?>
<footer>
  Some footer content.
</footer>
<?php $this->end() ?>

views/layout.php:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>
<body>
  <main>
    <h1>Example</h1>
    <?=$this->block('content')?>
  </main>

  <?=$this->block('footer')?>
</body>
</html>

Need more examples?

Requires

PHP >= 7.2.0

Extending library

PSR-11 container support

You can use a PSR-11 compatible container and inject dependencies into objects that are generated inside the library (Scope):

<?php

use Romanzaycev\Tooolooop\Engine;
use Psr\Container\ContainerInterface;

/** @var ContainerInterface $container */
$container = ...; // Initialize PSR-11 container
                  // and define implementation of Romanzaycev\Tooolooop\Scope\ScopeInterface
$engine = new Engine(__DIR__ . '/views');
$engine->setContainer($container);
$template = $engine->make('page'); // <-- Scope in this template will be obtained from container

You can define the implementation of Romanzaycev\Tooolooop\Scope\ScopeInterface in the container configuration and engine instances Scope through it.

โš ๏ธ ATTENTION! Make sure that the container returns a new instance of Scope all time! Cached Scope instance does not work.

User scope

Otherwise, you can specify a custom implementation of the class via $engine->setScopeClass():

<?php

use Romanzaycev\Tooolooop\Engine;
use Romanzaycev\Tooolooop\Scope\Scope;
use Romanzaycev\Tooolooop\Scope\ScopeInterface;

class UserSpecificScope extends Scope implements ScopeInterface {
    // Implement your additions, ex. widget system :-)
}

$engine = new Engine(__DIR__ . '/views');
$engine->setScopeClass(UserSpecificScope::class);

Testing

composer run test

tooolooop's People

Contributors

romanzaycev avatar

Stargazers

 avatar  avatar

Watchers

 avatar

tooolooop's Issues

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.