Yatp
Yet another TemplatePower, (*1)
A logic-less PHP template engine, (*2)
中文說明, (*3)
Blocks, (*4)
#valid
...
#valid (with zero or many spaces)
...
#invalid (must use pair blocks)
Marks, (*5)
{mark_name}
-
A valid block_name/mark_name: [A-Za-z0-9_-]+
-
A mark_name should contain at least one alphabet (update since V1.1)
Get Started
Install
$ composer require kuofp/yatp
// Require it with the correct path
require_once 'yatp.php';
Methods
__construct(string $html_or_filepath)
// Initialize
$tpl = new Yatp('<strong>{str}</strong>');
// You can use a file (if exists).
$tpl = new Yatp('view.tpl');
render([ bool $print = true ])
// Print to screen by default
$tpl = new Yatp('<strong>Hello World!</strong>');
$tpl->render();
// Output:
// <strong>Hello World!</strong>
// Get result without a print
$html = $tpl->render(false);
echo $html;
// Output:
// <strong>Hello World!</strong>
// PHP code is allowed
$tpl = new Yatp('');
$tpl->render();
// Output:
// Hello World!
block(string $target)
// Support dot operation
$tpl = new Yatp('
<!-- @c -->a.c<!-- @c -->
<!-- @a -->
<!-- @b -->
<!-- @c -->b.c<!-- @c -->
<!-- @b -->
');
$tpl->block('a.c')->render();
// Output:
// a.c
// Search the most likely one
$tpl = new Yatp('
a
b
c
d
<!-- @c -->
<!-- @b -->
<!-- @a -->
');
// Equivalent
// $tpl->block('a.b.c.d')->render();
// $tpl->block('c.d')->render();
$tpl->block('a.d')->render();
// Output:
// d
// First is adopted when block is redefined
$tpl = new Yatp('
1
<!-- @a -->2<!-- @a -->
<!-- @a -->3<!-- @a -->
');
$tpl->block('a')->render();
// Output:
// 1
assign(array $params)
// Assign to a mark
$tpl = new Yatp('<strong>{str}</strong>');
$tpl->assign([
'str' => 'Hello World!'
])->render();
// Output:
// <strong>Hello World!</strong>
// Assign several times
$tpl = new Yatp('<strong>{str}</strong>');
// Equivalent
// $tpl->assign([
// 'str' => 'Hi!'
// ])->assign([
// 'str' => 'Hi!'
// ])->render();
$tpl->assign([
'str' => ['Hi!', 'Hi!']
])->render();
// Output:
// <strong>Hi!Hi!</strong>
// Assign a variable to block in the same way
$tpl = new Yatp('<strong></strong>');
$tpl->assign([
'str' => 'Hello World!'
])->render();
// Output:
// <strong>Hello World!</strong>
// You can assign with another block
$tpl = new Yatp('<strong>{mark}</strong>');
$msg = new Yatp('Hello World!');
$tpl->assign([
'mark' => $msg->block('block')
])->render();
// Output:
// <strong>Hello World!</strong>
// Support dot operation, too
$tpl = new Yatp('
<!-- @c -->a.c<!-- @c -->
<!-- @a -->
<!-- @b -->
<!-- @c -->b.c<!-- @c -->
<!-- @b -->
');
$tpl->assign([
'a.c' => 'replaced'
])->render();
// Output:
// replaced b.c
nest(array $params)
$tpl = new Yatp('
');
$data = [
['title' => 'Lesson1'],
['title' => 'Lesson2'],
];
// Equivalent
// $tpl->assign([
// 'li' => [
// $tpl->block('li')->assign($data[0]),
// $tpl->block('li')->assign($data[1]),
// ]
// ])->render();
$tpl->assign([
'li' => $tpl->block('li')->nest($data)
])->render();
// Output:
//
// - Lesson1: Hello World!
// - Lesson2: Functions
//
debug( void )
$tpl = new Yatp();
$tpl->block('a_missing_block')->assign([
'a_missing_mark' => '',
'#wrong style' => ''
])->debug();
// Output:
// Array
// (
// [0] => Debug info:
// [1] => block "a_missing_block" is not found
// [2] => block or mark "a_missing_mark" is not found
// [3] => block or mark "#wrong style" is invalid
// )
Example
view.html, (*6)
<h1>{title}<h1>
<ul>
<!-- @li -->
<li>{title}: {text}</li>
<!-- @li -->
</ul>
php code, (*7)
$tpl = new Yatp('view.html');
$data = [
[
'title' => 'Lesson1',
'text' => 'Hello World!'
],
[
'title' => 'Lesson2',
'text' => 'Functions'
],
];
$tpl->assign([
'title' => 'Syllabus',
'li' => $tpl->block('li')->nest($data)
])->render();
Output:, (*8)
<h1>Syllabus<h1>
<ul>
<li>Lesson1: Hello World!</li>
<li>Lesson2: Functions</li>
</ul>