Reputation: 824
Hey I wrote a very simple template system for our designers to use. Its uses str_replace
under the hood to do its thing.
Its works great! The issue now is i'd like do some looping (foreach
) on some data passed.
Here's and example of the template code
$var_c = [
[
"head" => "a",
"body" => "b",
"foot" => "c"
],
[
"head" => "x",
"body" => "y",
"foot" => "z"
]
];
$tpl_vars = [
"__{var_a}__",
"__{var_b}__",
"__{var_c}__"
];
$real_vars = [
$var_a,
$var_b,
$var_c
];
str_replace($tpl_vars, $real_vars, $content_body);
Note $var_c
contains an array and i'd like to loop through this array. How do i achieve that.
For structure i was thinking
__startloop__
loop var_c as c
c[head]
c[body]
c[foot]
__endloop__
I cant seem to get my head around how to code this. :)
UPDATE: Twig, smarty and the likes are too big and cumbersome for the work. Its also introduces the learning curve for the designers to adopt a templating language.
Upvotes: 0
Views: 190
Reputation: 144
See my Text-Template class. It supports conditions (if), loops (for) and filters: https://github.com/dermatthes/text-template
Example template (String in a variable):
Hello {= name},
Your list of Items:
{for curItem in items}
{=@index1}: {= curItem.name}
{/for}
PHP-Code:
<?php
$data = [
"name" => "Some Username",
"items" => [
["name" => "First Item"],
["name" => "Second Item"]
]
];
$tt = new TextTemplate ($templateString);
echo $tt->apply ($data);
This should do the job.
Upvotes: 1