1. Go to this page and download the library: Download kuofp/yatp library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kuofp / yatp example snippets
#valid
<!-- @block_name -->
...
<!-- @block_name -->
#valid (with zero or many spaces)
<!--@block_name-->
...
<!-- @block_name -->
#invalid (must use pair blocks)
<!-- @block_name -->
{mark_name}
// Require it with the correct path
// Initialize
$tpl = new Yatp('<strong>{str}</strong>');
// You can use a file (if exists).
$tpl = new Yatp('view.tpl');
// 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(' echo "Hello World!"
// Support dot operation
$tpl = new Yatp('
<!-- @a -->
<!-- @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 -->a
<!-- @b -->b
<!-- @c -->c
<!-- @d -->d
<!-- @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('
<!-- @a -->1<!-- @a -->
<!-- @a -->2<!-- @a -->
<!-- @a -->3<!-- @a -->
');
$tpl->block('a')->render();
// Output:
// 1
// 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><!-- @str --><!-- @str --></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('<!-- @block -->Hello World!<!-- @block -->');
$tpl->assign([
'mark' => $msg->block('block')
])->render();
// Output:
// <strong>Hello World!</strong>
// Support dot operation, too
$tpl = new Yatp('
<!-- @a -->
<!-- @c -->a.c<!-- @c -->
<!-- @a -->
<!-- @b -->
<!-- @c -->b.c<!-- @c -->
<!-- @b -->
');
$tpl->assign([
'a.c' => 'replaced'
])->render();
// Output:
// replaced b.c
$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
// )