Installation:
composer require ajgl/twig-extensions
(Note: Despite being deprecated, this package may still be used in legacy projects or as a reference for the newer AjglBreakpointTwigExtension.)
Register the Extension:
In your Laravel service provider (e.g., AppServiceProvider), add:
$this->app['twig']->addExtension(new \Ajgl\TwigExtensions\AjglTwigExtensions());
First Use Case: Test a basic Twig filter (if available) in a Blade template:
{{ 'hello'|uppercase }} {# Example if 'uppercase' filter exists #}
AjglBreakpointTwigExtension.Legacy Template Integration:
{{ breakpoint() }} with the newer package’s equivalent.Custom Extensions:
AjglTwigExtensions) to add your own filters/functions:
class CustomTwigExtensions extends \Ajgl\TwigExtensions\AjglTwigExtensions {
public function getFunctions() {
return array_merge(parent::getFunctions(), [
new \Twig_SimpleFunction('custom_function', [$this, 'customFunction']),
]);
}
public function customFunction($input) {
return strtoupper($input);
}
}
Conditional Logic:
{% if breakpoint('sm') %}
<div class="sm-only">...</div>
{% endif %}
@twig directives to embed Twig logic:
@twig
{{ 'content'|some_filter }}
@endtwig
$twig = new \Twig_Environment($loader);
$twig->addExtension(new \Ajgl\TwigExtensions\AjglTwigExtensions());
$template = $twig->createTemplate('{{ some_function() }}');
$template->render();
Deprecation Warning:
AjglBreakpointTwigExtension for new projects.Undefined Extensions:
// Example: Check for 'truncate' filter in AjglTwigExtensions.php
public function getFilters() { ... }
Namespace Conflicts:
App\Extensions\Twig\...).Enable Twig Debugging:
In config/twig.php, set:
'debug' => env('APP_DEBUG', false),
To display errors like:
Twig_Error_Runtime: Filter 'unknown_filter' does not exist.
Log Extension Calls: Override methods to log usage (e.g., for breakpoint checks):
public function breakpoint($size) {
\Log::debug("Breakpoint called for: $size");
return parent::breakpoint($size);
}
Override Existing Extensions:
getFilters(), getFunctions()).Add Global Variables:
addGlobal:
$this->app['twig']->addGlobal('app_name', config('app.name'));
Custom Breakpoint Logic:
breakpoint() function, replace it with a Laravel helper:
// app/Helpers.php
if (!function_exists('breakpoint')) {
function breakpoint($size) {
return request()->wantsJson() || request()->isMobile();
}
}
How can I help you explore Laravel packages today?