1. Go to this page and download the library: Download sy/component 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/ */
$c = new Sy\Component();
$c->setTemplateContent('<!-- BEGIN A -->{SLOT}<!-- END A -->');
$c->setVar('SLOT', 'Hello');
foreach (['Foo', 'Bar', 'Baz'] as $v) {
$c->setBlock('A', ['SLOT' => $v]);
}
$c->setBlock('A');
echo $c;
class A extends Sy\Component {
public function __construct() {
parent::__construct();
$this->setTemplateFile(__DIR__ . '/template.html');
// setBlocks will set a block for each line in the data array
$this->setBlocks('foo', [
['firstname' => 'John', 'lastname' => 'Doe', 'age' => 32],
['firstname' => 'John', 'lastname' => 'Wick', 'age' => 42],
['firstname' => 'Jane', 'lastname' => 'Doe', 'age' => 25],
['firstname' => 'Bob', 'lastname' => 'Doe'],
]);
}
}
echo new A();
$c = new Sy\Component();
// use a php template file with the second parameter
$c->setTemplateFile(__DIR__ . '/template.tpl', 'php');
$c->setVar('NAME', 'World');
echo $c;
use Sy\Component;
class Hello extends Component {
public function __construct($name = 'world') {
$this->setTemplateFile(__DIR__ . '/Hello.tpl');
$this->setVar('NAME', $name);
}
}
// echo 'Hello world!'
$hello = new Hello();
echo $hello;
// echo 'Hello toto!'
$hello = new Hello('toto');
echo $hello;
$c = new Sy\Component();
$c->setTemplateFile(__DIR__ . '/template.tpl');
$c->setVar('NAME', new Hello());
use Sy\Component;
class MyComponent extends Component {
public function __construct() {
parent::__construct();
$this->setTemplateFile(__DIR__ . '/MyComponent.tpl');
// if $_REQUEST['action'] is not set, call initAction
$this->actionDispatch('action', 'init');
}
public function initAction() {
}
public function fooAction() {
}
}
use Sy\Component;
class MyComponent extends Component {
public function __construct() {
parent::__construct();
$this->setTemplateFile(__DIR__ . '/tpl/mycomponent.tpl');
// Add a translator, it will look for translation file into specified directory
$this->addTranslator(__DIR__ . '/lang', 'php', 'fr');
// Use translation method
$this->setVar('SLOT1', $this->_('Hello world'));
$this->setVar('SLOT2', $this->_('This is %s', 'an apple'));
$this->setVar('SLOT3', $this->_('This is %s', 'an pineapple'));
$this->setVar('SLOT4', $this->_('Number of %d max', 10));
}
}
echo new MyComponent();
return array(
'Hello world' => 'Bonjour monde',
'This is %s' => 'Ceci est %s',
'an apple' => 'une pomme',
'a pineapple' => 'un ananas',
'Number of %d max' => 'Nombre de %d max',
);
use Sy\Component;
class A extends Component {
public function __construct() {
$this->mount(function () {
$this->addTranslator(__DIR__ . '/lang', 'php', 'fr');
$this->setTemplateContent('<a>{HELLO/} {"world"} {B}</a>');
$this->setVars([
'HELLO' => $this->_('hello'),
'B' => new B()
]);
});
}
}
class B extends Component {
public function __construct() {
$this->mount(function () {
$this->setTemplateContent('<b>{HELLO/} {"world"} {C}</b>');
$this->setVars([
'HELLO' => $this->_('hello'),
'C' => new C()
]);
});
}
}
class C extends Component {
public function __construct() {
$this->mount(function () {
$this->addTranslator(__DIR__ . '/lang/alt', 'php', 'fr');
$this->setTemplateContent('<c>{HELLO/} {"world"}</c>');
$this->setVars([
'HELLO' => $this->_('hello'),
]);
});
}
}
echo new A();