Skip to main content

PHP-Jinja API Reference

This guide covers the PHP-Jinja API in detail.

Core Classes

Template Class

The Template class is the central component of PHP-Jinja.

<?php
use ByJG\JinjaPhp\Template;

// Constructor
$template = new Template(string $template);

// Methods
$template->withUndefined(UndefinedInterface $undefined): static; // Set undefined variable handler
$template->render(array $variables = []): string|array|null; // Render the template

Example

$template = new Template('Hello {{ name }}!');
$result = $template->render(['name' => 'World']);
echo $result; // Outputs: Hello World!

Loader Classes

PHP-Jinja provides classes for loading templates from different sources.

FileSystemLoader

<?php
use ByJG\JinjaPhp\Loader\FileSystemLoader;

// Constructor
$loader = new FileSystemLoader(string $basePath);

// Methods
$loader->getSource(string $name): string; // Load a template file

Example

$loader = new FileSystemLoader('/path/to/templates');
$templateString = $loader->getSource('my-template.html');
$template = new Template($templateString);

Undefined Variable Handlers

PHP-Jinja offers three implementations of UndefinedInterface:

  1. StrictUndefined (default): Throws an exception for undefined variables

    $template->withUndefined(new StrictUndefined());
  2. DebugUndefined: Shows a placeholder for undefined variables

    $template->withUndefined(new DebugUndefined());
    // Output example: {{ NOT_FOUND: variable_name }}
  3. DefaultUndefined: Uses a default value for undefined variables

    $template->withUndefined(new DefaultUndefined('default value'));

Interface Reference

LoaderInterface

<?php
namespace ByJG\JinjaPhp\Loader;

interface LoaderInterface
{
public function getSource(string $name): string;
}

UndefinedInterface

<?php
namespace ByJG\JinjaPhp\Undefined;

interface UndefinedInterface
{
public function render(string $varName): string;
}

Exception Classes

PHP-Jinja may throw the following exceptions:

  • TemplateParseException: When a template cannot be parsed or a variable is not found (with StrictUndefined)
  • LoaderException: When a template file cannot be loaded

Feature Reference

Template Syntax Elements

FeatureDescriptionExample
VariablesAccess variables in templates{{ variable }}, {{ object.property }}
ExpressionsMathematical, logical operations{{ 1 + 2 }}, {{ true && false }}
ConditionalsIf statements{% if condition %}...{% else %}...{% endif %}
LoopsFor loops{% for item in items %}...{% endfor %}
FiltersTransform variables{{ variable | filter }}
CommentsNon-rendered notes{# Comment #}
Whitespace ControlControl whitespace in output{%- if condition -%}...{%- endif -%}

Available Filters

FilterDescriptionExample
upperConvert to uppercase{{ "hello" | upper }}
lowerConvert to lowercase{{ "HELLO" | lower }}
capitalizeCapitalize first letter of each word{{ "hello world" | capitalize }}
trimRemove whitespace{{ " hello " | trim }}
replaceReplace substring{{ "hello" | replace("h", "j") }}
lengthGet length{{ "hello" | length }}
defaultSet default value{{ undefined | default("Guest") }}
joinJoin array with delimiter{{ ["a", "b"] | join("-") }}
splitSplit string to array{{ "a-b" | split("-") }}