Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Template Bundle Laravel Package

brix/template-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require alexandrekilian/template-bundle
    

    Register the bundle in config/app.php under providers:

    AlexandreKilian\TemplateBundle\TemplateBundle::class,
    
  2. Publish Assets Publish the bundle’s assets (if needed) and configuration:

    php artisan vendor:publish --provider="AlexandreKilian\TemplateBundle\TemplateBundle" --tag="config"
    
  3. First Use Case: Rendering Templates Use the Template facade to render a template:

    use AlexandreKilian\TemplateBundle\Facades\Template;
    
    $content = Template::render('path/to/template.twig');
    

    Ensure your Twig environment is configured (if not, extend the bundle’s setup).


Implementation Patterns

Core Workflows

  1. Dynamic Template Rendering Pass variables to templates dynamically:

    $data = ['title' => 'Welcome', 'items' => [1, 2, 3]];
    $rendered = Template::render('template', $data);
    
  2. Template Inheritance Leverage Twig’s extends and blocks for reusable layouts:

    {# base.twig #}
    <html>
        <body>
            {% block content %}{% endblock %}
        </body>
    </html>
    
    {# page.twig #}
    {% extends 'base.twig' %}
    {% block content %}Hello!{% endblock %}
    
  3. Integration with Brix CMS Use the bundle’s CMS-specific helpers (if documented) to fetch templates by route or entity:

    $template = Template::getByRoute('homepage');
    

Integration Tips

  • Custom Directories: Override template paths via config:
    'template_paths' => [
        base_path('resources/views/custom'),
    ],
    
  • Caching: Enable Twig cache for performance:
    'twig' => [
        'cache' => true,
    ],
    
  • Events: Listen for template-related events (if the bundle emits them) to modify rendering logic.

Gotchas and Tips

Pitfalls

  1. Missing Twig Configuration If Twig isn’t auto-configured, manually bind it in AppServiceProvider:

    $this->app->singleton('twig', function ($app) {
        return new \Twig\Environment(
            new \Twig\Loader\FilesystemLoader($app['config']['template_paths']),
            ['cache' => $app['config']['twig.cache']]
        );
    });
    
  2. Template Not Found Ensure paths in template_paths are correct and accessible. Use absolute paths for clarity.

  3. Circular Dependencies Avoid circular template inheritance (e.g., A.twig extends B.twig, which extends A.twig).

Debugging

  • Enable Twig Debug Mode:

    'twig' => [
        'debug' => env('APP_DEBUG', false),
    ],
    

    This shows template errors with line numbers.

  • Log Template Paths: Temporarily log resolved paths to verify they’re correct:

    Template::setPathResolver(function ($name) {
        \Log::debug("Resolving template: $name");
        return base_path("resources/views/$name");
    });
    

Extension Points

  1. Custom Template Resolvers Override the template resolver to fetch templates from databases or APIs:

    Template::setResolver(function ($name) {
        return \DB::table('templates')->where('name', $name)->first()->content;
    });
    
  2. Twig Extensions Register custom Twig functions/filters in config/template.php:

    'twig' => [
        'extensions' => [
            \App\Twig\CustomExtension::class,
        ],
    ],
    
  3. Event Listeners If the bundle dispatches events (e.g., TemplateRendering), hook into them for pre/post-processing:

    Template::addListener('TemplateRendering', function ($event) {
        $event->setData(array_merge($event->getData(), ['extra' => 'value']));
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware