PHP code example of earc / native-php-template-engine

1. Go to this page and download the library: Download earc/native-php-template-engine 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/ */

    

earc / native-php-template-engine example snippets


use eArc\NativePHPTemplateEngine\AbstractTemplateModel;

class MyTemplate extends AbstractTemplateModel
{
    /** @var object */
    protected $object;
    /** @var int */
    public $value;
    
    public function __construct(object $object, int $value)
    {
        $this->object = $object;
        $this->value = $value;    
    }

    public function template() : void
    { 

    use eArc\NativePHPTemplateEngine\AbstractTemplateModel;
    
    class MyRootTemplate extends AbstractTemplateModel
    {
        /** @var AbstractTemplateModel */
        protected $head;
        /** @var AbstractTemplateModel */
        protected $body;
        
        public function __construct(AbstractTemplateModel $head, AbstractTemplateModel $body)
        {
            $this->head = $head;    
            $this->body = $body;
        }
    
        public function template() : void
        { 

    use eArc\NativePHPTemplateEngine\AbstractTemplateModel;
    use eArc\NativePHPTemplateEngine\IteratorTemplateModel;
    
    class MyTemplate extends AbstractTemplateModel
    {
        /** @var IteratorTemplateModel */
        protected $loop;
        
        public function __construct(iterable $iterable)
        {
            $this->loop = new IteratorTemplateModel($iterable);    
        }
    
        public function template() : void
        { 

$template = new MyTemplate($object, $value);

$renderedTemplate = (string) $template;

echo new MyTemplate($object, $value);

echo json_encode(new MyTemplate($object, $value);

$template = new MyTemplate($object, $value);

echo json_encode(['data' => $template, 'html' => (string) $template]);

(string) $entity->getTemplate();

(string) $entity->getTemplate(MyEntityTemplate::class);

(string) new MyEntityTemplate($entity, $additionalParameter);

use eArc\NativePHPTemplateEngine\helpers\TemplateModelInterface;

class SomeEntity implements TemplateModelInterface
{
    // ...
    
    public function getTemplate(?string $fQCN = null)
    {
        if (null === $fQCN) {
            return new SomeEntityDefaultTemplate($this);
        }

        return new $fQCN($this);
    }
}

use eArc\NativePHPTemplateEngine\AbstractTemplateModel;
use eArc\NativePHPTemplateEngine\IteratorTemplateModel;

class MyTableTemplate extends AbstractTemplateModel
{
    // ...
    public function __construct($collection)
    {
        $tmplCollection = [];
        foreach ($collection as $entity) {
            $tmplCollection[] = new EntityDefaultTemplate($entity);
        }
        $this->collection = new IteratorTemplateModel($tmplCollection);
    }
    // ...
}

    use eArc\NativePHPTemplateEngine\helpers\CollectionTemplateModel;
    // ...
    $this->collection = new CollectionTemplateModel($collection, EntityDefaultTemplate::class);

     use eArc\NativePHPTemplateEngine\helpers\CollectionTemplateModel;
    // ...
    $this->collection = new CollectionTemplateModel($collection);

     use eArc\NativePHPTemplateEngine\helpers\CollectionTemplateModel;
    // ...

    // calls internally: new EntityDefaultTemplate($collectionItem, $some, $arguments)
    $this->collection = new CollectionTemplateModel($collection, EntityDefaultTemplate::class, $some, $arguments);

    //...
    
shell script
$ composer