byjg / jinja-php
Jinja for PHP
Fund package maintenance!
byjg
Installs: 33 000
Dependents: 3
Suggesters: 0
Security: 0
Stars: 16
Watchers: 1
Forks: 4
Open Issues: 1
pkg:composer/byjg/jinja-php
Requires
- php: >=8.1 <8.4
Requires (Dev)
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^5.9
README
Jinja for PHP
Jinja for PHP is a lightweight PHP implementation of the Jinja2 template engine originally developed for Python.
Overview
This library allows you to seamlessly process Jinja templates in PHP applications. It provides a familiar syntax for those coming from Python while offering a native PHP implementation.
Key Features
- Python Compatibility: Process the same Jinja templates in both Python and PHP
- Variable Support: Full support for variables, properties, and nested structures
- Conditional Logic:
if/elsestatements for conditional rendering - Loops: Iterate over arrays and objects with
forloops - Filters: Transform output with built-in filters like
upper,lower,default, etc. - Flexible Loaders: Load templates from strings or the filesystem
- Undefined Variable Handling: Different strategies for handling undefined variables
- Expressions: Support for mathematical operations, comparisons, and concatenation
Usage
use ByJG\JinjaPhp\Template; use ByJG\JinjaPhp\Undefined\DebugUndefined; $templateString = <<<EOT Hello {{ name }} EOT; $template = new Template($templateString); $template->withUndefined(new DebugUndefined()); // Default is StrictUndefined $variables = [ 'name' => 'World' ]; echo $template->render($variables);
Documentation
The detailed documentation is organized as follows:
- Basic Usage
- Template Syntax
- Loaders
- Undefined Variables
- Filters
- Control Structures
- Advanced Topics
- API Reference
- PHP Jinja vs Python Jinja2 Comparison
Installation
composer require byjg/jinja-php
Dependencies
flowchart TD
byjg/jinja-php
Loading