Download the PHP package codewithkyrian/jinja-php without Composer
On this page you can find all versions of the php package codewithkyrian/jinja-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download codewithkyrian/jinja-php
More information about codewithkyrian/jinja-php
Files in codewithkyrian/jinja-php
Package jinja-php
Short Description A minimalistic PHP implementation of the Jinja templating engine, specifically designed for parsing and rendering ML chat templates.
License MIT
Informations about the package jinja-php
Jinja PHP
A zero-dependency PHP implementation of the Jinja templating engine, specifically designed for parsing and rendering machine learning (ML) chat templates. This project is heavily inspired by HuggingFace's Jinja template engine in JavaScript, intended primarily for ML chat templates, but is versatile enough to be used for general purposes of parsing most Jinja templates.
Installation
Install Jinja PHP through Composer:
Quick Start
Here's how you can use Jinja PHP to render a template:
Features
✅ Supported Features
Control Structures
- Conditional Statements:
{% if %}
,{% elif %}
,{% else %}
,{% endif %}
- For Loops:
{% for %}
,{% else %}
,{% endfor %}
with loop variables (loop.index
,loop.index0
,loop.first
,loop.last
,loop.length
,loop.previtem
,loop.nextitem
) - Break/Continue:
{% break %}
,{% continue %}
for loop control - Ternary Expressions:
{{ value if condition else other_value }}
Variables & Data
- Variable Output:
{{ variable }}
- Negative Array Indexing:
messages[-1]
,array[-2]
(Python-style) - Object/Array Access:
user.name
,messages[0]['content']
- Null Literals:
none
,None
- Boolean Literals:
true
,false
,True
,False
- String Concatenation:
{{ "Hello" ~ " " ~ "World" }}
Macros & Functions
- Macros:
{% macro name(arg1, arg2) %}...{% endmacro %}
with{{ name() }}
calls - Function Calls:
{{ function(arg1, arg2) }}
- Keyword Arguments:
{{ function(param1=value1, param2=value2) }}
- Spread Arguments:
{{ function(*args) }}
Filters
- String Filters:
lower
,upper
,title
,capitalize
,strip
,lstrip
,rstrip
- String Manipulation:
replace
,split
,startswith
,endswith
,indent
- Array Filters:
length
,join
,map
,reverse
,sort
- Data Filters:
tojson
,default
- Custom Filter Blocks:
{% filter filter_name %}...{% endfilter %}
Advanced Features
- Comments:
{# This is a comment #}
- Set Statements:
{% set variable = value %}
and block sets{% set variable %}{% endset %}
- Call Blocks:
{% call macro_name() %}...{% endcall %}
- Exception Handling:
{{ raise_exception('Error message') }}
- String Concatenation: Multiple string literals and
~
operator
Built-in Functions
range()
: Generate number sequencesraise_exception()
: Throw runtime exceptionslen()
: Get length of arrays/strings
🔄 Partially Supported Features
- Complex Expressions: Most Jinja expressions work, but some edge cases may differ
- Template Inheritance: Basic support (limited)
- Custom Filters: Can be added via the Environment class
- Whitespace Control: Basic support with
{%-
and-%}
❌ Not Yet Supported
- Template Inheritance:
{% extends %}
,{% block %}
,{% include %}
- Custom Functions: User-defined functions
- Advanced Filters: Some complex filters like
groupby
,batch
Usage Examples
Conditional Statements
For Loops with Loop Variables
Macros
String Manipulation
Array Operations
Set Statements
Filter Blocks
Comments
Advanced Usage
Error Handling
Complex Expressions
Testing
Jinja PHP comes with a comprehensive test suite to ensure functionality remains consistent and reliable. To run the tests:
The test suite includes:
- Unit tests for all language features
- End-to-end template processing tests
- Error handling and edge case tests
Performance
Jinja PHP is designed for performance with:
- Zero external dependencies
- Efficient tokenization and parsing
- Optimized runtime execution
- Memory-conscious design
Contributing
Jinja PHP is designed to be robust and feature-rich, offering support for a wide range of functionalities. If there's a feature you need that isn't currently implemented, we encourage you to request it. Additionally, if you're proficient with PHP and understand the internals of templating engines, consider contributing to the project by submitting a pull request with your proposed feature.
Contributors
- Kyrian Obikwelu
- Other contributors are welcome.
Acknowledgements
- Hugging Face for their work on ML chat templates.
- The Jinja template engine for Python, which served as the primary inspiration for this project.
Support
If you find any issues or have a question, feel free to open an issue in the repo.
License
This project is licensed under the MIT License. See the LICENSE file for more information.