PHP code example of sy / template

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

    

sy / template example snippets




use Sy\Template\Template;

// Create a template with variable slot
$template = new Template();
$template->setFile('mytemplate.tpl');

// Fill the variable slot
$template->setVar('NAME', 'World');

// Output render
echo $template->getRender();



use Sy\Template\Template;

// Create a template with a block
$template = new Template();
$template->setFile('mytemplate.tpl');

// This variable will be overrided below
$template->setVar('NAME', 'Hello world');

// Fill the variable slot and repeat the block
foreach (['foo', 'bar', 'baz'] as $name) {
	$template->setVar('NAME', $name);
	$template->setBlock('MY_BLOCK');
}

// Output render
echo $template->getRender();



use Sy\Template\Template;

// Create a template with a block
$template = new Template();
$template->setFile('mytemplate.tpl');

// This variable will not be overrided below because the block use isolated variables
$template->setVar('NAME', 'Hello world');

// Fill the variable slot and repeat the block
foreach (['foo', 'bar', 'baz'] as $name) {
	// Use isolated variables for this block
	$template->setBlock('MY_BLOCK', ['NAME' => $name]);
}

// Output render
echo $template->getRender();



use Sy\Template\Template;

// Create a template with a block
$template = new Template();
$template->setFile('mytemplate.tpl');

// No setBlock here

// Output render
echo $template->getRender();

{NAME}
<!-- BEGIN MY_BLOCK -->
Hello {NAME}
<!-- END MY_BLOCK -->

{NAME}
<!-- BEGIN MY_BLOCK -->
Hello {NAME}
<!-- END MY_BLOCK -->