Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/n2ref/micro_templater

Simple and strong templater
https://github.com/n2ref/micro_templater

php template-engine

Last synced: 7 days ago
JSON representation

Simple and strong templater

Awesome Lists containing this project

README

        

Micro Templater
===============

Simple but powerful PHP template engine.

Just one file.

Have a look how it easy to use.

#### Examples

##### Variables
###### Example # 1 Using the method 'assign'

```php
$html = '
Title



  • [var2]


';

$tpl = new Mtpl();
$tpl->setTemplate($html);

$tpl->assign('var1', 'foo');
$tpl->assign('[var2]', 'bar');

echo $tpl->render();
```
Result will look like:

```html
Title



  • bar


```

##### Blocks

###### Example # 1 A clear indication of the block
```php
$html = '
Title

Exemple 1: error message!


';

$tpl = new Mtpl();
$tpl->setTemplate($html);

$tpl->touchBlock('error');

echo $tpl->render();
```
Result will look like:

```html
Title

Exemple 1: error message!

```

###### Example # 2 Implicit indication block
```php
$html = '
Title

[message]


';

$tpl = new Mtpl();
$tpl->setTemplate($html);

$tpl->error->assign('[message]', 'Exemple 2: error message!');

echo $tpl->render();
```
Result will look like:

```html
Title

Exemple 2: error message!

```

##### Loops

###### Example # 1 A list of menu
```php
$html = '
Title


';

$menu = array(
'home' => 'Home',
'gallery' => 'Gallery',
'help' => 'Help'
);

$tpl = new Mtpl();
$tpl->setTemplate($html);

foreach ($menu as $page_name => $title) {
$tpl->menu->assign('[URL]', '?view=' . $page_name);
$tpl->menu->assign('[TITLE]', $title);
$tpl->menu->reassign();
}

echo $tpl->render();
```
Result will look like:

```html
Title


```

##### Fill drop down

###### Example # 1
```php
$html = '

Title




Year


Submit

';

$tpl = new Mtpl();
$tpl->setTemplate($html);

$years = array(
2000 => '2000',
2005 => '2005',
2010 => '2010',
2015 => '2015'
);
$tpl->fillDropDown('year', $years, 2015);

echo $tpl->render();
```
Result will look like:

```html

Title




Year

2000
2005
2010
2015


Submit

```