Code Monkey home page Code Monkey logo

template's Introduction

Fast and powerful php template engine

Syntax close to php for easy learning and management.

Scrutinizer Code Quality Code Intelligence Status Build Status

Installation

You can install the package via composer:

composer require tleckie/template

Printing variable:

{{$name}}

Flexibility of spaces in variables:

<p>{{$name}}</p>

<p>{{   $user->getName()   }}</p>

<p>
    {{
    $user->getName()
    }}
</p>

Printing constant:

{{CONSTANT}}

Set variables:

{set $variable = 'test'}

{set $variable = 355}

{set $userId = 25}

{set $userModel = new \MyNamespace\User($userId)}

{set $userModel = new \MyNamespace\User(25, 'John')}

Dump:

{dump $users}

Comments:

{# {dump $users} #}

Extends template:

<html>
<head></head>
<body>

    {extends Common/Header.html}
    
    <p>List</p>
        
    {foreach $users as $user}
        <p>{{$user->getName()}}</p>
    {endforeach}

    {extends Common/Footer.html}

</body>
</html>

If you add changes to the included templates as extensions, the compiler will not show these changes. To remove the compiled files you have the "flushCompiled()" method

$tpl = new Template(__DIR__.'/tpl/', '/var/www/cache/compiled/');
$tpl->flushCompiled();

You can enable development mode so that templates are always compiled.

use Tleckie\Template\Template;

$tpl = new Template(
    __DIR__.'/tpl/', 
    '/var/www/cache/compiled/', 
    null, 
    true // development mode
);

Conditionals if, else & elseif:

{if $title === 'title'}
    <div>{{$title}}</div>
{else}
    It is not a headline!
{endif}
{if $type === 'Orange'}
    <div>{{$type}}</div>
{elseif $title !== 'Apple'}
    It is not an Apple!
{else}
    Other fruit!
{endif}
{if $age >= 18}
    <div>Yes!</div>
{else}
    Ups :)
{endif}
{if $age !== 18}
    <div>Yes!</div>
{else}
    Ups :)
{endif}

Nested loops:

Foreach and for supported.

<html>
<head></head>
<body>

    {foreach $data as $key => $persons}
        {foreach $persons as $id => $name}
            <div><string>{{$name}}:</string>{{ $key }}</div>
        {endforeach}
    {endforeach}
    
</body>
</html>

Helpers:

Create your own helpers to invoke on the template.

{{$this->arrayHelper::last($persons)}}

Create the template instance:

<?php

require_once "vendor/autoload.php";

use Tleckie\Template\Template;

$tpl = new Template(__DIR__ . '/tpl/', '/var/www/cache/compiled/');

$data = [
    'names' => ['Marcos', 'John', 'Pedro', 'Marta'],
    'title' => 'User list',
    'users' => [
        new User('Marcos'), new User('John'), new User('Pedro')
    ]
];

$tpl->render('List/Users.html', $data);

Template List/Users.html

<html>
<head></head>
<body>
    {foreach $users as $user}
        <p>{{ strtoupper($user->getName()) }}</p>
    {endforeach}
</body>
</html>

Rules:

The Tleckie\Template\Compiler\Parser\Rules class establishes the rules for handling templates. You can create new rules to enrich your templating engine through the RuleInterface interface.

use Tleckie\Template\Template;
use Tleckie\Template\Compiler\Compiler;
use Tleckie\Template\Compiler\Parser\Rules;

$rules = [
    new Rules(),
    new MyOwnRules()
];

$compiler = new Compiler($rules);

$tpl = new Template(
    __DIR__.'/tpl/', 
    '/var/www/cache/compiled/',
     $compiler
);

Helpers:

You can create your own helpers to be invoked from templates to do complex tasks, manipulate objects or strings.

The Template class has a method for adding helpers. Note that by this mechanism you can also add your dependency injector.

<?php

require_once "vendor/autoload.php";

use Tleckie\Template\Template;

$tpl = new Template(
    __DIR__ . '/tpl/', 
    '/var/www/cache/compiled/'
);

$tpl->registerHelper(
    'arrayHelper', 
    new \MyNamespace\Infrastructure\Helpers\ArrayHelper()
);

Each helper added to the template object must have an alias to be invoked through it.

{{$this->arrayHelper::last($persons)}}

Helper example:

<?php

namespace MyNamespace\Infrastructure\Helpers;

use function end;

/**
 * Class ArrayHelper
 * @package MyNamespace\Infrastructure\Helpers
 * @author Teodoro Leckie Westberg <[email protected]>
 */
class ArrayHelper
{
    public static function last(array $array)
    {
        return end($array);
    }
}

That's all! I hope this helps you ;)

template's People

Contributors

teodoroleckie avatar

Stargazers

 avatar  avatar

Watchers

 avatar

template'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.