2017 © Pedro Peláez
 

library microtpl

small template attribute language implemention for PHP (using xml_parse)

image

bephp/microtpl

small template attribute language implemention for PHP (using xml_parse)

  • Tuesday, November 24, 2015
  • by lloydzhou
  • Repository
  • 1 Watchers
  • 6 Stars
  • 763 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

MicroTpl

MicroTpl is small templating system for PHP., (*1)

Example

index.tpl.php

<!DOCTYPE html>
<html>
  <head>
    <title>{title}</title>
  </head>
  <body>
    <h1>{title}</h1>
    {@messages as key => message}
    <p>{message}</p>
    {/messages}
  </body>
</html>

index.php

<?php
function render($tpl, $data = array(), $return = false) {
    if ($return) ob_start();
    extract($data);
    eval('?>' .preg_replace_callback('_{([\@\/\-\?\!\&]?)([^}]+)}_', function ($m) {
    $args = preg_split('/([\s]+)/', trim(str_replace(array('as', '=>'), '', $m[2])));
        switch($m[1]) {// {@messages as index => message}  list array. 
            case '@':
            if(count($args) == 3)
                    $r = "if(isset(\${$args[0]})) foreach(\${$args[0]} as \${$args[1]} => \${$args[2]}) {";
                else {
                    $args[1] = isset($args[1])?$args[1]:'value';
                    $r = "if(isset(\${$args[0]})) foreach(\${$args[0]} as \${$args[1]}) {";
                }break;
            case '?':$r = "if(isset(\${$args[0]})&&!!\${$args[0]}){";break; // {?var} show on true
            case '!':$r = "if(!isset(\${$args[0]})||!\${$args[0]}){";break; // {!var} show on false
            case '/':$r = '}';break; // end mark
            case '&':$r = "echo isset(\${$args[0]})?\${$args[0]}:null";break; // {&var} echo 
            case '-':$r = implode(' ', $args);break; // php code
            default: $r = "echo isset(\${$args[0]})?htmlspecialchars(\${$args[0]},ENT_QUOTES):null"; // {var} echo 
        }
        return "<?php $r?>";    
    }, $tpl));
    if ($return) return ob_get_clean();
}
render(file_get_contents('index.tpl.php'), array(
    'title' => 'Hello world.', 
    'messages' => array('Hello, Earth', 'We confiscates this planet.')
));
?>

Syntax

{var}         echo escaped variable
{&var}        echo unescaped variable
{@list}       list array
{?bool}       show block on true
{!bool}       show block on false
{/list}       end of array or block
{php code}    process php code
{var='value'} assign value to variable

The Versions

24/11 2015

dev-xml

dev-xml https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

micro template xml library tal

24/11 2015

v1.0.6

1.0.6.0 https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

micro template xml library tal

23/10 2015

v1.0.5

1.0.5.0 https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

micro template xml library tal

22/10 2015

v1.0.4

1.0.4.0 https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

micro template xml library tal

26/09 2015

v1.0.3

1.0.3.0 https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

micro template xml library tal

25/09 2015

v1.0.2

1.0.2.0 https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm activerecord

23/09 2015

v1.0.1

1.0.1.0 https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm activerecord

15/09 2015

v1.0.0

1.0.0.0 https://lloydzhou.github.io/microtpl/

small template attribute language implemention for PHP (using xml_parse)

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

orm activerecord