Skip to content

add template function #3

@lloydzhou

Description

@lloydzhou

i have one template function only 132 chars:

function t($t,$d='data'){return "extract(\$$d);".str_replace(['{{','}}','{%','%}'],['");$c(',');$c("','");','$c("'],'%}'.$t.'{%');}

there's some example code here:

  • define template file "test.tpl"
      <h1>{{$title}}</h1>
      {% foreach($messages as $message){ %}
          <p>{{$message}}</p>
      {% } %}
  • define the data will render to template
    $data = [
        'title' => 'test title',
        'messages' => ['message1', 'message2', 'message3']
    ];
    $c='print_r';
  • compile template and save into file
file_put_contents('test.php', '<?php '. t(file_get_contents('test.tpl')));
include ('test.php');
  • compile the template into function, can call it by using render data
$f = create_function('$d,$c', $t = t(file_get_contents('test.tpl'), 'd'));
$f($data, 'print_r');

you can see it in gist: https://gist.github.com/lloydzhou/9e81afb1f946a1123fb7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions