An open API service indexing awesome lists of open source software.

https://github.com/xtompie/tpl


https://github.com/xtompie/tpl

Last synced: 4 months ago
JSON representation

Awesome Lists containing this project

README

          

# Tpl

Tpl native PHP template system.

Simple, lightweight, no dependencies, low abstraction, low features, easy to extend.

```php
__invoke('page.tpl.php', ['title' => 'Foobar']);
```

`page.tpl.php`:

```phtml

= $this->e($title) ?>


```

- uses native PHP
- not auto escape, but `vendor/bin/xtompie-tpl-audit.sh -p src -s .tpl.php` can be used
- no sections
- no blocks
- no namespaces
- autocompletion using ``

## Requiments

PHP >= 8.0

## Installation

Using [composer](https://getcomposer.org/)

```shell
composer require xtompie/tpl
```

## Full feature example

```phtml

// Extend Tpl for customization, set templatePathPrefix, add helpers
// src/App/Shared/Tpl/Tpl.php

namespace App\Shared\Tpl\Tpl;

use Xtompie\Tpl\Tpl as BaseTpl;

class Tpl extends BaseTpl
{
protected function templatePathPrefix(): string
{
return 'src/';
}

protected function date(int $time): string
{
return $this->e(date('Y-m-d H:i:s', $time));
}
}

// src/App/Test/Ui/Controller/TestController.php

namespace App\Test\Ui\Controller;

use App\Shared\Tpl\Tpl;

class TestController
{
public function __construct(
private Tpl $tpl
) {
}

public function __invoke(): string
{
return $this->tpl->__invoke('Test/UI/Tpl/content.tpl.php', ['title' => 'foobar']);
}
}

// src/Test/UI/Tpl/content.tpl.php - first level
// keep template file names with `.tpl.php` suffix e.g. easy exclude from phpstan

push('Test/UI/Tpl/layout.tpl.php', ['title' => $title]); ?>

= $this->e($title) ?>

// src/Test/UI/Tpl/layout.tpl.php - second level

push('Test/UI/Tpl/head.tpl.php', ['title' => $title]); ?>


= $this->render('Test/UI/Tpl/navbar.tpl.php') ?>
= $this->content() ?>

// src/Test/UI/Tpl/navbar.tpl.php

Index

// src/Test/UI/Tpl/head.tpl.php - third level


= $this->e($title) ?>


= $this->content() ?>

```