PHPackages                             tleckie/template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. tleckie/template

ActiveLibrary[Templating &amp; Views](/categories/templating)

tleckie/template
================

Fast and powerful php template engine. Syntax close to php for easy learning and management.

1.0.2(5y ago)217[1 issues](https://github.com/teodoroleckie/template/issues)MITPHPPHP ^8

Since May 10Pushed 5y ago1 watchersCompare

[ Source](https://github.com/teodoroleckie/template)[ Packagist](https://packagist.org/packages/tleckie/template)[ Docs](https://github.com/teodoroleckie/template)[ Fund](https://www.paypal.com/donate?business=ZHYA2MTGA4884&currency_code=USD)[ GitHub Sponsors](https://github.com/teodoroleckie)[ RSS](/packages/tleckie-template/feed)WikiDiscussions main Synced 6d ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

Fast and powerful php template engine
=====================================

[](#fast-and-powerful-php-template-engine)

Syntax close to php for easy learning and management.

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/55163a7a1c2d942d9b7cdaeb1fbab9a814b3cba69553152431eaa67122619f73/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f74656f646f726f6c65636b69652f74656d706c6174652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/teodoroleckie/template/?branch=main)[![Code Intelligence Status](https://camo.githubusercontent.com/763ab946b92da0e8a4c46eec166025fa3227eb6fc2a84f392b38a4bb3f2183ff/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f74656f646f726f6c65636b69652f74656d706c6174652f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d61696e)](https://scrutinizer-ci.com/code-intelligence)[![Build Status](https://camo.githubusercontent.com/7294fc8bb324fe302a5950670f2e5e91f13bacf7d0fc5a9a0a3bab9bc81ab270/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f74656f646f726f6c65636b69652f74656d706c6174652f6261646765732f6275696c642e706e673f623d6d61696e)](https://scrutinizer-ci.com/g/teodoroleckie/template/build-status/main)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require tleckie/template
```

### Printing variable:

[](#printing-variable)

```
{{$name}}
```

Flexibility of spaces in variables:

```
{{$name}}

{{   $user->getName()   }}

    {{
    $user->getName()
    }}

```

### Printing constant:

[](#printing-constant)

```
{{CONSTANT}}
```

### Set variables:

[](#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)

```
{dump $users}
```

### Comments:

[](#comments)

```
{# {dump $users} #}
```

### Extends template:

[](#extends-template)

```

    {extends Common/Header.html}

    List

    {foreach $users as $user}
        {{$user->getName()}}
    {endforeach}

    {extends Common/Footer.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 &amp; elseif:

[](#conditionals-if-else--elseif)

```
{if $title === 'title'}
    {{$title}}
{else}
    It is not a headline!
{endif}
```

```
{if $type === 'Orange'}
    {{$type}}
{elseif $title !== 'Apple'}
    It is not an Apple!
{else}
    Other fruit!
{endif}
```

```
{if $age >= 18}
    Yes!
{else}
    Ups :)
{endif}
```

```
{if $age !== 18}
    Yes!
{else}
    Ups :)
{endif}
```

### Nested loops:

[](#nested-loops)

Foreach and for supported.

```

    {foreach $data as $key => $persons}
        {foreach $persons as $id => $name}
            {{$name}}:{{ $key }}
        {endforeach}
    {endforeach}

```

### Helpers:

[](#helpers)

Create your own helpers to invoke on the template.

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

### Create the template instance:

[](#create-the-template-instance)

```
