PHP code example of net_bazzline / php_component_template

1. Go to this page and download the library: Download net_bazzline/php_component_template 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/ */

    

net_bazzline / php_component_template example snippets


$isFoo = ($bar === 'foo');
if ($isFoo) {
     /* ... */ 
} else { 
    /*  ... something else */ 
}

//assuming the file in the relative path 'template.phtml' has the following content
// $this->foobar('foo', 'bar');
$myViewHelper = function($foo, $bar) {
    return 'there is no ' . $foo . ' without a ' . $bar;
}
$template = new CallableComplexFileBasedTemplateManager('template.phtml');
$template->registerCallable('foobar', $myViewHelper);
echo $template->render() . PHP_EOL;
//expected result: there is no foo without a bar

use Net\Bazzline\Component\Template\RuntimeContentBasedTemplate;

//create a instance
$template = new RuntimeContentBasedTemplate();

//set content
$template->setContent('there is no {one} without a {two}');

//assign variable one by one ...
$template->assignOne('one', 'foo');
//... or by providing an array
$template->assignMany(array('one' => 'foo'));
//you can also assign a template to a template
//  this is used if a layout template is defined with a {content} key for e.g.
$template->assignOne('content', $otherTemplate);

//you can render it in different ways
//1) explicit calling the method
echo $template->render();
//2) casting it to a string
echo (string) $template;
//3) using it as a function
//  you can also provide all optional parameters like in the constructor
echo $template();

mkdir -p vendor/net_bazzline/php_component_template
cd vendor/net_bazzline/php_component_template
git clone https://github.com/bazzline/php_component_template .

composer