vietartisans / templater
VA Team's templater object
Requires
- php: ^5.4 || ^7.0 || ^7.1
- philo/laravel-blade: 3.*
- twig/twig: 1.*
- vietartisans/light-gulp: dev-master
This package is not auto-updated.
Last update: 2018-05-13 22:50:54 UTC
README
Name | Description |
---|---|
Project | VA Core |
Module | Templater |
Author | @sondoha, @buiquangduc, @duyngha |
Templater is a PHP library that handle template files when we develop PHP application, plugin or website.
Features
- Use Blade and Twig as template engines
- Support manifest.json asset URL when we working with gulp
Requirements
Make sure all dependencies have been installed before moving on:
Installation
Install templater library using Composer from the directory that you are working on.
$ composer require vietartisans/templater
Usage
We use 2 popular template engines in our library: Blade and Twig, so here are 2 ways to use it.
Blade
Blade template engine was developed by Laravel, a popular PHP framework. Its structure looks similar to Twig but it SUPPORTED raw PHP.
See more how to use it here Blade Templates
your-file.php
// Import composer autoload first require_once __DIR__ . '/vendor/autoload.php'; $templater = new VA\Templater(__DIR__.'/templates/'); echo $templater->render('/path/to/template-file', [ // Your templates/path/to/template-file.blade.php should be rendered 'param_1' => 'value_1', ... 'param_n' => 'value_n' ]);
/path/to/template-file.blade.php
/** * @var $param_1 string * ... * @var $param_n array */ <p>Hello, this is my template. Variable $param_1: {{ $param_1 }}.</p>
Folder structure
your-application/ ├── templates/ │ ├── path/ │ ├── to/ | ├── template-file.blade.php │ ├── another-template.blade.php ├── your-file.php # your main php file └── vendor/ # → Composer packages (never edit)
Twig
We used Twig template engine for the example above. This engine is powerful and flexible, but it does not support raw PHP.
See more about Twig format here: Twig Template Engine
your-file.php
// Import composer autoload first require_once __DIR__ . '/vendor/autoload.php'; $templater = new VA\Templater(__DIR__.'/templates/', 'twig'); // Parameter number 2 is 'twig', it means we will use Twig as default engine. echo $templater->render('/path/to/template-file.php', [ // Your templates/path/to/template-file.php should be rendered 'param_1' => 'value_1', ... 'param_n' => 'value_n' ]);
/path/to/template-file.php
/** * @var $param_1 string * ... * @var $param_n array */ <p>Hello, this is my template. Variable $param_1: {{ param_1 }}.</p>
Folder structure
your-application/ ├── templates/ │ ├── path/ │ ├── to/ | ├── template-file.php │ ├── another-template.php ├── your-file.php # your main php file └── vendor/ # → Composer packages (never edit)
$param_1
to $param_n
are variables that you will use in your template file, feel free to name it your way.
Wordpress Theme Override
If you run this library in a WordPress plugin, you should use theme file override features.
$templater->setWordPressThemeSupport('your-app-name');
Then you can write template file in theme folder like this wp-content/themes/{theme-name}/templates/{your-app-name}/your-template.php
Custom Function
This feature is working with Twig only.
index.php
<?php $templater->addFunction('test', function($a, $b) { return $a + $b; })->render('test.php');
templates/test.php
{{ test(1, 2) }} // return 3
Changelog
1.1.2
- Add custom function for Twig
1.1.1
- Improve code quality
1.1
- Update source folder structure
- Integrate Blade
1.0
- Integrate twig
- Setup composer/packagist
- PSR-4 code standard
- Fall back folder
- Travis CI