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

Symfony Hybrid Views Bundle Laravel Package

cortezvini97/symfony-hybrid-views-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer in your Laravel project (Symfony-based, so use a bridge like fruitcake/laravel-symfony if needed):

    composer require cortezvini97/symfony-hybrid-views-bundle
    

    Register the bundle in config/app.php (Symfony) or bridge it via Laravel’s service provider.

  2. First Use Case Render a hybrid view (e.g., mix PHP + Twig) in a Symfony controller:

    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Response;
    
    class HomeController extends AbstractController
    {
        public function index(): Response
        {
            return $this->render('home.html.twig', [
                'title' => 'Hybrid View Demo',
                'data' => ['key' => 'value']
            ]);
        }
    }
    
    • Place the template in templates/home.html.twig (or .php for PHP views).
    • Access it via php bin/console server:run (Symfony) or Laravel’s routing.

Implementation Patterns

Workflows

  1. Hybrid Templating

    • Use Twig for logic-heavy sections (e.g., loops, filters) and PHP for legacy code.
    • Example: home.html.twig with embedded PHP:
      <h1>{{ title }}</h1>
      {% for item in data %}
          {{ include('partials/_item.php', {'item': item}) }}
      {% endfor %}
      
  2. Laravel Integration

    • Service Provider Bridge: Extend HybridViewsBundle via Laravel’s AppServiceProvider:
      public function register()
      {
          $this->app->singleton('twig', function ($app) {
              return HybridViewsBundle::getTwigEnvironment();
          });
      }
      
    • Route Binding: Use Symfony’s AbstractController in Laravel routes:
      Route::get('/', [HomeController::class, 'index']);
      
  3. Partial Reuse

    • Share partials between Twig/PHP:
      • templates/partials/_header.twig (Twig)
      • resources/views/partials/header.php (PHP)
    • Load via {% include %} or {{ include }} in templates.

Tips

  • Environment Awareness: Use {{ app.environment }} in Twig to conditionally load assets.
  • Laravel Blade + Twig: Combine with twig/extra-bundle for Blade-like syntax in Twig:
    {{ include('layouts.app', {'content': include('home.twig')}) }}
    

Gotchas and Tips

Pitfalls

  1. Namespace Collisions

    • Twig/PHP templates may conflict if using identical partial names. Prefix with twig_ or php_:
      {% include('twig_header.twig') %}
      
  2. Caching Quirks

    • Twig cache (var/cache/dev) may not auto-update in dev. Clear it after template changes:
      php bin/console cache:clear
      
    • Laravel’s cache may interfere; use php artisan cache:clear if needed.
  3. Dependency Conflicts

    • Ensure symfony/twig-bundle and twig/twig versions align with Laravel’s Symfony bridge.

Debugging

  • Template Errors: Twig errors show line numbers in .twig files; PHP errors may be ambiguous. Use:

    $this->get('twig')->addRuntimeLoader(new \Twig\RuntimeLoader\RuntimeLoader());
    

    For better error context.

  • Variable Dumping: Use Twig’s dump() or Laravel’s dd() in PHP templates:

    {{ dump(data) }}
    

Extension Points

  1. Custom Filters Add Twig extensions in Symfony’s config/packages/twig.yaml:

    twig:
        extensions:
            - App\Twig\AppExtension
    

    Or in Laravel’s AppServiceProvider:

    $twig->addExtension(new \App\Twig\AppExtension());
    
  2. PHP Template Overrides Override Twig’s loader to prioritize PHP templates:

    $loader = new \Twig\Loader\FilesystemLoader([__DIR__.'/templates']);
    $twig->setLoader($loader);
    
  3. Hybrid Forms Use Symfony’s FormComponent in Twig:

    {{ form_start(form) }}
        {{ form_widget(form.name) }}
    {{ form_end(form) }}
    

    Render PHP forms via {{ include('form.php', {'form': form}) }}.

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