Download the PHP package lubosdz/yii2-template-engine without Composer

On this page you can find all versions of the php package lubosdz/yii2-template-engine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yii2-template-engine

Yii2 Template Engine

Simple, fast and flexible HTML templating engine for Yii2 PHP framework with zero configuration. Supports basic control structures (IF, FOR, SET), importing subtemplates (IMPORT), piped and dynamic directives, active record (AR) relations and deep nested arrays. It is similar to Twig or Blade, however with less overhead, no dependencies and without advanced features.

It can be used to render e.g. an invoice from HTML markup, edited in a WYSIWYG editor and turned into PDF or MS Word file - see example bellow.

Generic version without framework dependency can be found at lubosdz/html-templating-engine.

Installation

or via composer.json:

Basic usage

Initiate template engine inside Yii application:

use lubosdz\yii2\TemplateEngine;
$engine = new TemplateEngine();

or register as a component in app/config/main.php:

...
'components' => [
    'engine' => [
        'class' => 'lubosdz\yii2\TemplateEngine',
    ]
]
...

$engine = Yii::$app->template;

Use method $engine->render($html [, $values]) to generate HTML output:

Once output generated, it can be e.g. supplied to PDF or MS Word renderer to produce a PDF or MS Word file respectively.

Simple placeholders

Templating engine collects all placeholders within supplied HTML markup and attempts to replace them with matching $values or evaluate as control structure. Placeholders that have not been processed are left untouched by default. This behaviour is suitable for development. In production, setting $engine->setForceReplace(true) can be set to replace unprocessed placeholders with empty string.

$html = 'Hello <b>{{ who }}</b>!';
$values = ['who' => 'world'];
echo $engine->render($html, $values);
// output: "Hello <b>world</b>!"

Built-in directives

Template engine comes with couple of generally usable methods for basic date, time and string manipulation - directives. These can be referenced directly inside supplied HTML markup. Directives use the pipe operator | to chain operations within the placeholder.

$html = $engine->render('Generated on {{ today }} at {{ time }}.');
// output - respecting EN locale: "Generated on Dec 31, 2021 at 11:59pm."
// output - respecting SK/CS locale: "Generated on 31. 12. 2021 at 23:59."

echo $engine->render('Meet me at {{ now(7200) | time }}.');
// output example, if now is 8:30am: "Meet me at 10:30am." (shift +2 hours = 7200 secs)

echo $engine->render('My name is {{ user.name | escape }}.', ['user' => [
    'name' => '<John>',
]]);
// filtered output: "My name is &lt;John&gt;."

echo $engine->render('Hello {{ user.name | truncate(5) | e }}.', ['user' => [
    'name' => '<John>',
]]);
// truncated and filtered output: "Hello &lt;John...."

Specific for the Yii 2 framework, object properties as well as related models are automatically collected, if referenced inside placeholders.

$html = $engine->render('Hello {{ customer.name }}.', [
    'customer' => Customer::findOne($id), // find active record
]);
// output e.g.: "Hello John Doe."

$html = $engine->render('Address is {{ customer.address.city }} {{ customer.address.zip }}.', [
    'customer' => Customer::findOne($id), // Customer has defined relation to object `Address`
]);
// output e.g.: "Address is Prague 10000."

Other built-in directives:


// trim - standard PHP trim function
$html = $engine->render('Hello {{ username | trim }} ', [
    'username' => '   John Doe!   ',
]);
// output "Hello John Doe!"

$html = $engine->render('Hello {{ username | trim(" !eo") }}', [
    'username' => '   John Doe!   ',
]);
// output "Hello John D"

// replace - match simple string or regular expression (REGEX)
$html = $engine->render('HELLO {{ name | replace (BADBOY, GOODBOY) }}!', [
    'name' => 'BADBOY'
]);
// output "HELLO GOODBOY"

// nl2br - new lines to brackets
$html = $engine->render('NOTES: {{ notes_textarea | nl2br }}', [
    'notes_textarea' => "first line ...\nsecond line\n- last line -"
]);
// output:
"NOTES: first line ...
<br>second line
<br>- last line -
"

// concatenation - joining strings
$html = $engine->render('Order #{{ order_id | concat("by user"; " - ") | concat(customer.name) }}', [
    'order_id' => "123",
    'customer' => [
        'name' => 'John Doe',
    ],
]);
// output "Order #123 by customer - John Doe"

Dynamic directives

Dynamic directives allow binding custom anonymous functions to placeholders. They can be added at a runtime in 2 steps and greatly extend flexibility of the engine.

In the following example we will attach dynamic directive named coloredText and render output with custom inline CSS:

Note: The first argument passed into dynamic directive ($text in the example above) is always the value from previous piped operation.

IF .. ELSEIF .. ELSE .. ENDIF

Control structure IF .. ELSEIF .. ELSE .. ENDIF is supported:

$templateHtml = "
{{ if countOrders > 0 }}
    <h3>Thank you!</h3>
{{ else }}
    <h3>Sure you want to leave?</h3>
{{ endif }}
";

$values = [
    'countOrders' => count(Customer::findOne($id)->orders); // e.g. 3
];

echo $engine->render($templateHtml, $values);
// output e.g.: "<h3>Thank you!</h3>" - if some order found

FOR ... ELSEFOR .. ENDFOR

Structure FOR ... ELSEFOR .. ENDFOR will create loop:

$templateHtml = "
<table>
{{ for item in items }}
    {{ SET subtotal = item.qty * item.price * (100 + item.vat) / 100 }}
    <tr>
        <td>#{{ loop.index }}</td>
        <td>{{ item.description }}</td>
        <td>{{ item.qty }}</td>
        <td>{{ item.price | round(2) }}</td>
        <td>{{ item.vat | round(2) }}%</td>
        <td>{{ subtotal | round(2) }} &euro;</td>
    </tr>
    {{ SET total = total + subtotal }}
{{ endfor }}
</table>
<p>Amount due: <b> {{ total | round(2) }} Eur</b></p>
";

$values = [
    'items' => [
        ['description' => 'Item one', 'qty' => 1, 'price' => 1, 'vat' => 10],
        ['description' => 'Item two', 'qty' => 2, 'price' => 2, 'vat' => 20],
        ['description' => 'Item three', 'qty' => 3, 'price' => 3, 'vat' => 30],
        ['description' => 'Item four', 'qty' => 4, 'price' => 4, 'vat' => 40],
    ]
];

$html = $engine->render($templateHtml, $values);
// outputs valid HTML table with items e.g.: "<table><tr><td>#1</td><td> ..."

Following auxiliary variables are accessible inside each loop:

SET command

Allows manipulating local template variables, such as count totals:

See also example under FOR.

Note: shorthand syntax += e.g. SET total += subtotal is NOT supported.

IMPORT command

Allows importing another templates (subtemplates). Importing of subtemplates from within subtemplates is supported too. For security reasons imported subtemplate(s) must reside inside the template directory (e.g. ../templates/header.html) or subdirectory (e.g. ../templates/invoice/header.html). This allows effective structuring and maintaing template sets. Attempt to load a template from outside of the template directory will throw an error.

First, set the template directory either explicitly or via loading template by alias:

// set the template directory root explicitly
$engine->setDirTemplates('/abs/path/to/templates');

// template directory will be set implicitly as the parent of `invoice.html`
// note: engine's method "render()" supports Yii's aliases, so if supplied string
//       starts with ampersand `@` it is assuming template absolute path
$htmlInvoice = $engine->render('@templates/invoice.html');

Then in processed template add the import command:

Configuring template engine

Templating engine comes with most typical pre-configured settings. In many cases it may be useful to change default behaviour. The engine allows changing:

Setting the argument separator in directives

The engine uses by default semicolon ; which is less common and less prone to conflict with supplied texts. It can be changed to more typical comma , by setting:

Please note the the engine will ignore placeholders for which parsing fails. See also test for detailed behaviour.

Enabling / disabling errors logging

By default the engine logs errors into system logs. Typically, these may be ie. unprocessed placeholders (meaning no value supplied) or failed parsing of placeholders. It is highly recommended to enable this logging during development. However, in production it may be more desired to turn it off by setting:

Replacing of empty or unprocessed placeholders

By default the engine does not replace any unprocessed or empty placeholders. This allows quick discovering the issues in templates during development. By defining replacement as a string we can force the engine to insert such a string into the output for all empty or unprocessed placeholders. Following are typical and valid replacement alternatives:

Reading template resources

After processing whole template it may be usefull to store some processed data. Typically we may want to re-populate template with same values in the future and store current data into the database. The engine allows reading generated resources.

This will return all data necessary for reconstructing the template:

Rendering PDF or MS Word files

Rendering engine allows user to safely change output without any programming knowledge. To add an extra value, we can also turn rendered HTML output into professionally looking PDF or MS Word file:


// first process HTML template and input values
$htmlOutput = $engine->render($htmlMarkup, $values);

// then generate PDF file:
$pdf = new \TCPDF();
$pdf->writeHTML($htmlOutput);
$path = $pdf->Output('/save/path/my-invoice.pdf');

// or generate MS Word file:
$word = new \PhpOffice\PhpWord\PhpWord();
$section = $word->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $htmlOutput);
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($word, 'Word2007');
$writer->save('/save/path/my-invoice.docx');

Running tests

Tips & notes

use -d parameter to inject debugging arguments, e.g. > phpunit -d [email protected]:7869 %*

class MyRenderer extends \lubosdz\yii2\TemplateEngine
{
    // your code, custom directives, override parent methods ..
}

Changelog

1.1.0 - released 2024-12-26

1.0.9 - released 2023-12-29

1.0.8 - released 2023-12-11

1.0.7 - released 2023-11-14

1.0.6 - released 2023-10-22

1.0.5 - released 2023-09-18

1.0.4 - released 2023-09-05

1.0.3 - released 2022-08-04

1.0.2 - released 2022-02-01

1.0.1 - released 2021-12-30

1.0.0 - released 2021-12-13


All versions of yii2-template-engine with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0
yiisoft/yii2 Version ^2.0.35
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package lubosdz/yii2-template-engine contains the following files

Loading the files please wait ....