2017 © Pedro Peláez
 

library template

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

image

affinity4/template

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  • Wednesday, April 25, 2018
  • by Affinity4
  • Repository
  • 1 Watchers
  • 2 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Template

Full-featured template engine with optional syntax which is easy to learn. Can use plain PHP also., (*1)

Features

  • HTML Comment syntax
  • Can use plain PHP in templates if needed
  • Add new syntax if needed.

Installation

Affinity4/Template is available via composer:, (*2)

composer require affinity4/template

or, (*3)

{
    "require": {
        "affinity4/template": "^1.1"
    }
}

Syntax

Output a variables:, (*4)

<h1><!-- :title --></h1>

Set a variable:, (*5)

<!-- :showTitle = true -->

To get an array item by key, such as $post['title']:, (*6)

<!-- :post.title -->

If statement:, (*7)


    <h1><!-- :title --></h1>



    <h1><!-- :title --></h1>

    <h1><!-- :title --></h1>
    <h2>Something</h1>

    <h1>Default Title</h1>

Foreach loop:, (*8)



<article>

</article>

NOTE: Can be @foreach also., (*9)

While loop:, (*10)



    Number: <!-- :i --><br />



For loop:, (*11)

<!-- @for :i = 1; :i <= 3; :i++ -->
    Number: <!-- :i --><br />
<!-- @/for -->

Layouts and Blocks

You can extend master layouts the same way as you would in any other template engine such as Twig or Blade., (*12)

Create a master layout with sections to be overridden in each view file:, (*13)

File: views/layout/master.php, (*14)

<!DOCKTYPE html>
<html>
<head>
    <title><!-- @block title -->This can be overridden<!-- @/block -->: Site description</title>

    <link href="/assets/css/main.css" rel="stylesheet">
    <!-- @block css -->
    <!-- Each view can add custom CSS here -->
    <!-- @/block -->

    <script src="/assets/js/jquery.js"></script>
    <!-- @block js-head -->
    <!-- Each view can add custom JS here -->
    <!-- @/block -->
</head>
<body>
    <main>    
        <h1><!-- @block page-title -->Default Page<!-- @/block --></h1>

        <!-- @block content -->
        <p>Page content goes here...</p>
        <!-- @/block -->
    </main>

    <!-- @block js-footer -->
    <!-- Each view can add custom JS here -->
    <!-- @/block -->
</body>
</html>

Then in you view:, (*15)

File: views/home.php, (*16)






<link href="/assets/css/home.css" rel="stylesheet">







This is the homepage, (*17)

Then simply render the view:, (*18)

File: index.php, (*19)

use Affinity4\Template;

$template = new Template\Engine(new Template\Syntax);

$tempalte->render('views/home.php', ['page_title' => 'Home']);

Usage

To render a template:, (*20)

use Affinity4\Template;

$template = new Template\Engine(new Template\Syntax);

$template->render('views/home.php', ['title' => 'Home Page']);

If you want to add new syntax you can use the addToken method after initializing the template engine., (*21)

use Affinity4\Template;

$template = new Template\Engine(new Template\Syntax);

$template->addRule('/\{\{ ([\w\d+]) \}\}/', '= $$1 ?>');

$template->render('views/home.php', ['title' => 'Home Page']);

You can also pass a callable as the second argument to the addToken method to use preg_replace_callback instead for the replacement., (*22)

use Affinity4\Template;

$template = new Template\Engine(new Template\Syntax);

$template->addRule('/\{\{ ([\w\d]+) \}\}/', function ($statement) {
    return ''; 
});

$template->render('views/home.php', ['title' => 'Home Page']);

Overriding Syntax

One thing which is quite original about Affinity4 Template is that it allows you to replace the Syntax class with your won simple class to create a template language of your own., (*23)

It easy to add all the features currently in Affinity4 Template by simply extending the Affinity4\Template\Tokenizer class and implementing the Affinity4\Template\SyntaxInterface. From there you need only add rules for variables, loops etc. and extend and block syntax. If you do not add extend or block rules that functionality will simply not be available., (*24)

Here is an example of creating a Blade style syntax of your own, (*25)

addRule('/\{\{ ?(\$.*) ?\}\}/', '');

        $this->addRule('/@if ?\(\(.*))/', '<?php if ($1) : ?>');
        $this->addRule('/@elseif ?\(\(.*))/', '<?php elseif ($1) : ?>');
        $this->addRule('/@else/', '<?php else : ?>');
        $this->addRule('/@endif/', '<?php endif; ?>');

        $this->addRule('/@foreach ?\(\(.*))/', '<?php foreach ($1) : ?>');
        $this->addRule('/@endforeach/', '<?php foreach ($1) : ?>');

        // For, while etc...

        $this->addExtendsRule('/@extends\((.*)\)/');
        $this->addSectionRule('/@section\('(.*)'\)/', '/@endsection/');
    }
}

You then simply use dependency injection when calling the Template Engine class, (*26)

require_once __DIR__ . '/vendor/autoload.php';

use Affinity4\Template\Engine;
use Your\Template\Syntax\Blade2;

$blade2 = new Engine(new Blade2);
$blade2->render('views/home.blade', ['title' => 'Blade 2']);

The your layout template (views/layout/master.blade) can be:, (*27)

<!doctype html>
<html>
<head>
    <title>@section('title') Default to be overriden @endsection</title>
</head>
<body>
    <h1>
    @section('title')
        Title here...
    @endsection
    </h1>
</body>
</html>

In views/home.blade..., (*28)

@extends('layouts/master.blade')

@section('title')
  {{ $title }}
@endsection

Tests

Run tests:, (*29)

vendor/bin/phpunit

Licence

(c) 2017 Luke Watts (Affinity4.ie), (*30)

This software is licensed under the MIT license. For the full copyright and license information, please view the LICENSE file that was distributed with this source code., (*31)

The Versions

25/04 2018

dev-master

9999999-dev

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts

25/04 2018

1.3.0

1.3.0.0

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts

22/05 2017

1.2.0

1.2.0.0

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts

16/05 2017

1.1.0

1.1.0.0

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts

16/05 2017

1.1.1

1.1.1.0

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts

15/05 2017

1.0.2

1.0.2.0

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts

15/05 2017

1.0.1

1.0.1.0

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=7.0
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts

15/05 2017

1.0.0

1.0.0.0

Simple template engine with optional syntax which is easy to learn. Can use plain PHP also.

  Sources   Download

MIT

The Requires

  • php >=5.6
  • mikey179/vfsstream ^1.6

 

The Development Requires

by Luke Watts