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

Twig Components Laravel Package

ibexa/twig-components

Ibexa DXP Twig Components provides reusable Twig components and helpers used across Ibexa DXP. Install and use it as part of a full Ibexa DXP setup rather than standalone. Licensed under Ibexa Business Use (BUL) or Trial/Test (TTL).

View on GitHub
Deep Wiki
Context7
## Getting Started

### Minimal Setup
1. **Installation**:
   ```bash
   composer require ibexa/twig-components

Requires Ibexa DXP (or a compatible Twig environment). For Laravel, pair with spatie/laravel-twig.

  1. First Use Case: Render a basic table with CMS content (or Eloquent data):

    {% use 'ibexa:table.html.twig' %}
    {{ ibexa.table({
        'data': contentList,
        'columns': [
            { 'label': 'Title', 'property': 'title' },
            { 'label': 'Date', 'property': 'publish_date' }
        ]
    }) }}
    
  2. Where to Look First:


Implementation Patterns

Core Workflows

  1. Data Binding:

    • CMS Content: Pass Ibexa Content objects or Location collections directly.
      {{ ibexa.table({ data: locations, columns: [...] }) }}
      
    • Eloquent Models: Wrap in a Twig extension to normalize data:
      // app/Extensions/TableDataExtension.php
      public function getTableData($data) {
          return array_map(fn($item) => [
              'title' => $item->name,
              'date' => $item->created_at->format('Y-m-d')
          ], $data);
      }
      
      {% set tableData = this.getTableData(posts) %}
      {{ ibexa.table({ data: tableData, columns: [...] }) }}
      
  2. Column Customization:

    • Dynamic Columns: Use Twig loops to define columns:
      {% set columns = [] %}
      {% for field in content.fields %}
          {% set columns = columns|merge([{
              'label': field.name,
              'property': 'value.' ~ field.identifier
          }]) %}
      {% endfor %}
      {{ ibexa.table({ data: contentList, columns: columns }) }}
      
    • Custom Renderers: Extend via Twig filters:
      {{ column.value|ibexa_table_cell_renderer }}
      
  3. Pagination/Sorting:

    • Integrate with Laravel’s pagination:
      {{ ibexa.table({
          data: posts->paginate(10),
          columns: [...],
          pagination: true
      }) }}
      
    • Override sorting logic via Twig extensions.
  4. Styling:

    • SCSS Overrides: Extend the base stylesheet:
      // resources/scss/ibexa/_table.scss
      .ibexa-table {
        --table-border-color: #333;
      }
      
    • Tailwind: Use arbitrary variants:
      {{ ibexa.table({ data: [...], class: 'table-auto border-collapse' }) }}
      

Integration Tips

  • Laravel Service Provider: Register Twig extensions in AppServiceProvider:
    public function boot() {
        $this->loadViewsFrom(__DIR__.'/../vendor/ibexa/twig-components/src/Resources/views', 'ibexa');
        $this->app['twig']->addExtension(new IbexaTableExtension());
    }
    
  • Blade-Twig Interop: Render Twig components in Blade:
    // app/Helpers/TwigHelper.php
    public static function renderTwigComponent($view, $data) {
        return \Twig\Environment::create()->render($view, $data);
    }
    
    @php echo \App\Helpers\TwigHelper::renderTwigComponent('ibexa:table.html.twig', $tableData) @endphp
    
  • API Data: Normalize API responses before passing to the table:
    $tableData = array_map(function($item) {
        return [
            'id' => $item['id'],
            'name' => $item['attributes']['name'],
        ];
    }, $apiResponse);
    

Gotchas and Tips

Pitfalls

  1. Twig Dependency:

    • Issue: The package assumes Twig is configured. Laravel’s default Blade templating won’t work without a bridge.
    • Fix: Use spatie/laravel-twig or render Twig components via PHP helpers (as shown above).
  2. CMS-Specific Assumptions:

    • Issue: Components like ibexa.Table expect Ibexa Content objects or Location collections. Passing raw arrays may break.
    • Fix: Normalize data in a Twig extension or Laravel service:
      public function normalizeTableData($data) {
          return array_map(function($item) {
              return [
                  'title' => $item->getField('title')->value,
                  'date' => $item->getField('publish_date')->value,
              ];
          }, $data);
      }
      
  3. Styling Conflicts:

    • Issue: Default styles may clash with Tailwind/Bootstrap.
    • Fix: Override variables in your SCSS:
      .ibexa-table {
        --table-bg-color: theme('colors.white');
        --table-header-bg: theme('colors.gray.100');
      }
      
  4. Pagination Quirks:

    • Issue: The pagination option may not work out-of-the-box with Laravel’s paginator.
    • Fix: Extend the component’s Twig template to handle Illuminate\Pagination\LengthAwarePaginator:
      {% if data instanceof Illuminate\Pagination\LengthAwarePaginator %}
          {% set pagination = {
              'total': data->total(),
              'perPage': data->perPage(),
              'currentPage': data->currentPage(),
              'lastPage': data->lastPage()
          } %}
      {% endif %}
      
  5. Missing Bundles Exception Handling:

    • Issue: In v5.0.9, the package now throws exceptions if required Ibexa bundles are missing, which may cause unexpected errors during development.
    • Fix: Ensure all required Ibexa bundles are installed and properly configured. Check for clear error messages indicating which bundle is missing and install it via Composer:
      composer require ibexa/core-bundle
      
    • Debugging: If you encounter exceptions during development, verify your composer.json includes all necessary Ibexa dependencies and run composer install or composer update.

Debugging Tips

  1. Template Overrides:

    • Override the base template in resources/views/components/ibexa/table.html.twig to debug rendering issues.
  2. Data Validation:

    • Use dump() in Twig to inspect passed data:
      {% dump(data) %}
      {{ ibexa.table({ data: data, columns: columns }) }}
      
  3. Extension Debugging:

    • Check if extensions are registered:
      $this->app['twig']->getExtension('ibexa_table')->getFunctions();
      

Extension Points

  1. Custom Components:

    • Extend the package by creating new Twig components in app/Resources/views/components/ibexa/.
    • Example: Add a Card component by copying the table structure.
  2. Twig Extensions:

    • Add custom logic via extensions:
      // app/Extensions/CustomTableExtension.php
      class CustomTableExtension extends \Twig\Extension\AbstractExtension {
          public function getFunctions() {
              return [
                  new \Twig\TwigFunction('custom_table_cell', [$this, 'renderCell']),
              ];
          }
      
          public function renderCell($value, $type) {
              // Custom rendering logic
              return "<span class='{$type}'>{$value}</span>";
          }
      }
      
      {{ column.value|custom_table_cell('highlight') }}
      
  3. Configuration:

    • Override defaults via config/ibexa.php (if Ibexa DXP is installed) or environment variables:
      'table' => [
          'default_columns' => [
              ['label' => 'ID', 'property' => 'id'],
          ],
      ],
      
  4. JavaScript Integration:

    • Attach events via Twig’s attr filter:
      <button {{ attr({'class': 'ibexa-table-sort', 'data-sort': column.property}) }}>
          {{ column.label }}
      </button>
      
    • Listen in your JS:
      document.querySelectorAll('.ibexa-table-sort').forEach
      
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor