https://github.com/byjg/php-jinja
Jinja for PHP is a PHP implementation of the [Jinja2](http://jinja.pocoo.org/) template engine.
https://github.com/byjg/php-jinja
Last synced: 3 months ago
JSON representation
Jinja for PHP is a PHP implementation of the [Jinja2](http://jinja.pocoo.org/) template engine.
- Host: GitHub
- URL: https://github.com/byjg/php-jinja
- Owner: byjg
- Created: 2023-03-12T03:55:44.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-03-31T19:43:34.000Z (6 months ago)
- Last Synced: 2025-04-15T14:58:27.671Z (6 months ago)
- Language: PHP
- Size: 111 KB
- Stars: 16
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Jinja for PHP
[](https://github.com/byjg/php-jinja/actions/workflows/phpunit.yml)
[](http://opensource.byjg.com)
[](https://github.com/byjg/php-jinja/)
[](https://opensource.byjg.com/opensource/licensing.html)
[](https://github.com/byjg/php-jinja/releases/)Jinja for PHP is a PHP implementation of the [Jinja2](http://jinja.pocoo.org/) template engine.
## Introduction
This library is a port of the Jinja2 template engine to PHP. It is a partial implementation of the Jinja2 template engine.
The main goal of this library is allow process templates both in Jinja Python and PHP, however he API is not the same as the original in Python.
## Implemented Features
Currently, the following features are implemented:
### Literals
Most of the literals are supported. e.g.
```jinja
{{ 1 }}
{{ 1.2 }}
{{ "Hello World" }}
{{ true }}
{{ false }}
{{ none }}
{{ [1, 2, 3] }}
{{ ['a': 1, 'b': 2] }} // It is different from Python
```@todo: use the python notation for dictionaries.
### Variables
Most of the variables are supported. e.g.
```jinja
{{ myvar }}
{{ myvar.myproperty }}
{{ myvar.myproperty.1 }}
{{ myvar.myproperty.a }}
{{ myvar.myproperty.a.myproperty }}
{{ myvar.myproperty.a.myproperty.1 }}
{{ myvar.myproperty.a.myproperty.1.myproperty }}
```@todo: The notation with brackets is not yet supported.
### Filters
Some filters are implemented:
```jinja
{{ var | upper }}
{{ var | lower }}
{{ var | default }}
{{ var | default('-') }}
{{ var | replace('a', 'b') }}
{{ var | join }}
{{ var | join(',') }}
{{ var | split }}
{{ var | split(',') }}
{{ var | capitalize }}
{{ var | trim }}
{{ var | trim('-') }}
{{ var | length }}
```### Math Operations
```jinja
{{ 1 + 2 }}
{{ 1 - 2 }}
{{ 1 * 2 }}
{{ 1 / 2 }}
{{ 1 % 2 }}
{{ 1 ** 2 }}
```### Concatenation
```jinja
{{ "Hello" ~ "World" }}
{{ var1 ~ var2 }}
```### Comparison
```jinja
{{ 1 == 2 }}
{{ 1 != 2 }}
{{ 1 < 2 }}
{{ 1 <= 2 }}
{{ 1 > 2 }}
{{ 1 >= 2 }}
```### Logic Operations
There are some differences between the Python and PHP implementation.
TODO: use `and` and `or` instead of `&&` and `||````jinja
{{ 1 && 2 }}
{{ 1 || 2 }}
{{ ! 1 }}
```### If
@todo: `elif` is not implemented yet.
```jinja
{% if var1 == var2 %}
{{ var1 }} is equal to {{ var2 }}
{% else %}
1 is not equal to 2 or 3
{% endif %}
``````jinja
{% if 1 == 2 %}
1 is equal to 2
{% else %}
1 is not equal to 2 or 3
{% endif %}
```### For
@todo: `else` is not implemented yet.
```jinja
{% for item in items %}
{{ item }}
{% endfor %}
``````jinja
{% for key, value in items %}
{{ key }}: {{ value }}
{% endfor %}
```Loop control variable:
- loop.index
- loop.index0
- loop.revindex
- loop.revindex0
- loop.first
- loop.last
- loop.length```jinja
{% for item in items %}
{{ loop.index }}: {{ item }}
{% endfor %}
```## Usage
```php
use ByJG\JinjaPhp\Template;$templateString = <<withUndefined(new DebugUndefined()); // Default is StrictUndefined
$variables = [
'name' => 'World'
];
echo $template->render($variables);
```## Installation
```bash
composer require byjg/jinja-php
```## Dependencies
```mermaid
flowchart TD
byjg/jinja-php
```----
[Open source ByJG](http://opensource.byjg.com)