Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom column twig template problem #72

Closed
kris-sum opened this issue Mar 22, 2024 · 15 comments · Fixed by #73
Closed

Custom column twig template problem #72

kris-sum opened this issue Mar 22, 2024 · 15 comments · Fixed by #73
Assignees
Labels
bug Something isn't working

Comments

@kris-sum
Copy link

Hi,

Great package, i'm new to symfony (but not to php) and have been looking for a decent data table component to use and this is by far the best structured one! (thank you!)

I'm having a problem creating a new column type - following the guide , everything is fine except I can't get my twig template working.

I get the following exception:

Block "column_link_with_row_data_value" on template "@KreyuDataTable/themes/base.html.twig" does not exist.

I have

{# templates/datatable/theme.html.twig #}

{% block column_link_with_row_data_value %}
    <a {% with { attr: { href, target }|merge(attr) } %}{{- block('attributes') -}}{% endwith %}>
        {{- block('column_text_value') -}}
    </a>
{% endblock %}

and

kreyu_data_table:
  defaults:
    themes:
      - '@KreyuDataTable/themes/bootstrap_5.html.twig'
      - 'datatable/theme.html.twig'

As a workaround, if I remove the bootstrap5 theme from krey_data_table.yaml and extend it in my twig template, it works fine.

@Kreyu
Copy link
Owner

Kreyu commented Mar 22, 2024

Hey, thank you for the kind words! :) I can confirm this is a bug, and I will try to fix this as soon as possible.

@Kreyu Kreyu self-assigned this Mar 22, 2024
@Kreyu Kreyu added the bug Something isn't working label Mar 22, 2024
@Kreyu Kreyu closed this as completed in #73 Mar 22, 2024
Kreyu added a commit that referenced this issue Mar 22, 2024
@Kreyu
Copy link
Owner

Kreyu commented Mar 22, 2024

Well, that was a quick fix :) Version 0.17.7 should be working properly in your case

@kris-sum
Copy link
Author

Whoa, that was super fast! Thank you so much! ❤

@kris-sum
Copy link
Author

Hmm, now I get

Variable "block_theme" does not exist. in in ....\vendor\kreyu\data-table-bundle\src\Resources\views\themes\base.html.twig (line 112)
image

@Kreyu
Copy link
Owner

Kreyu commented Mar 22, 2024

@kris-sum have you tried clearing the cache? Twig sometimes can be quite quirky 😅 This variable was added in the new version and is automatically added to the Twig context

@Kreyu Kreyu reopened this Mar 22, 2024
@kris-sum
Copy link
Author

kris-sum commented Mar 22, 2024

Yeah, I killed ./var/cache . I'll try again...

@Kreyu
Copy link
Owner

Kreyu commented Mar 22, 2024

Yeah that should do the trick. Maybe I was too hasty, I'm looking into it

@kris-sum
Copy link
Author

Still no joy.

This code path doesn't seem to be executed on my install:

image

@Kreyu
Copy link
Owner

Kreyu commented Mar 22, 2024

I was able to reproduce this issue by:

  1. creating a custom column type named LinkWithRowDataFormColumnType, which means its block prefix will equal link_with_row_data
  2. not adding a column_link_with_row_data_value block in any of the themes

Previously I only tested a scenario where the block was properly defined in the template. In this scenario it should render the column as the text instead of throwing an error - working on a fix already.

In the meantime, can you provide the contents of your column type class? And how exactly is it named?

@kris-sum
Copy link
Author

LinkWithRowDataColumnType.php is a really basic copy of LinkColumnType, with an additional parent property passed to the callable:

<?php

declare(strict_types=1);

namespace App\Datatable\Column\Type;

use Kreyu\Bundle\DataTableBundle\Column\Type\AbstractColumnType;
use Kreyu\Bundle\DataTableBundle\Column\Type\TextColumnType;
use Kreyu\Bundle\DataTableBundle\Column\ColumnInterface;
use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class LinkWithRowDataColumnType extends AbstractColumnType
{
    public function buildValueView(ColumnValueView $view, ColumnInterface $column, array $options): void
    {
        if (is_callable($href = $options['href'])) {
            $href = $href($view->vars['data'], $view->parent);
        }

        if (is_callable($target = $options['target'])) {
            $target = $target($view->vars['data'], $view->parent);
        }

        $view->vars = array_replace($view->vars, [
            'href' => $href,
            'target' => $target,
        ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver
            ->setDefaults([
                'href' => '#',
                'target' => '_self',
            ])
            ->setAllowedTypes('href', ['string', 'callable'])
            ->setAllowedTypes('target', ['string', 'callable'])
        ;
    }

    public function getParent(): ?string
    {
        return TextColumnType::class;
    }
}

@Kreyu
Copy link
Owner

Kreyu commented Mar 22, 2024

Okay, so this was caused by another bug, and I somehow never noticed that.

Each column type has its own map of block prefixes. For example, your LinkWithRowDataColumnType has three block prefixes:

  • link_with_row_data
  • text
  • column

When rendering, the bundle is trying to render header and value blocks using those block prefixes. Because you haven't specified the column_link_with_row_data_header block for the header, the fallback should happen (this is ok). Unfortunately, the fallback was broken, because it was trying to render the column_column_header (notice the double "column"...) block instead of the proper column_header block.

This is fixed in the version 0.17.8. Sorry for the inconvenience. Can you please update and verify whether it's working in your application? 😄

@kris-sum
Copy link
Author

Definitely getting closer! But my block dies with this error:

Twig\Error\RuntimeError {#1973 ▼
  -lineno: 4
  -name: "datatable/theme.html.twig"
  -rawMessage: "Block "attributes" on template "datatable/theme.html.twig" does not exist."
  -sourcePath: "[redacted]\templates\datatable\theme.html.twig"
  -sourceCode: """
    {# templates/datatable/theme.html.twig #}


    {% block column_link_with_row_data_value %}

        <a {% with { attr: { href, target }|merge(attr) } %}{{- block('attributes') -}}{% endwith %}>
            {{- block('column_text_value') -}}
        </a>

    {% endblock %}
    """
}

@kris-sum
Copy link
Author

kris-sum commented Mar 22, 2024

Doh, it works if I include (i.e. copy) all the referenced blocks to be in the custom datatable/theme.html.twig file,

{# templates/datatable/theme.html.twig #}

{% block column_link_with_row_data_value %}
    <a {% with { attr: { href, target }|merge(attr) } %}{{- block('attributes') -}}{% endwith %}>
        {{- block('column_text_value') -}}
    </a>
{% endblock %}

{% block attributes %}
    {% for key, value in attr|default({}) %}{{ key }}="{{ value }}"{% endfor %}
{% endblock %}

{% block column_value %}
    <span {{- block('attributes') -}}>
        {%- if translation_domain is not same as false -%}
            {{- value|trans({}, translation_domain) -}}
        {%- else -%}
            {{- value -}}
        {%- endif -%}
    </span>
{% endblock %}

{% block column_text_value %}
    {{- block('column_value') -}}
{% endblock %}

@Kreyu
Copy link
Owner

Kreyu commented Mar 22, 2024

@kris-sum Yeah, if you want to rely on the base theme blocks, you have to extend the template, or provide path to the template that contains the block (second argument of the block function), e.g.:

{% block column_link_with_row_data_value %}
    <a {% with { attr: { href, target }|merge(attr) } %}{{- block('attributes', '@KreyuDataTable/themes/base.html.twig') -}}{% endwith %}>
        {{- block('column_text_value', '@KreyuDataTable/themes/base.html.twig') -}}
    </a>
{% endblock %}

See: https://twig.symfony.com/doc/3.x/functions/block.html

Personally, in this case, I wouldn't bother and I would just extend the Bootstrap theme and use it alone, as you previously suggested as a workaround 😄

Thanks for help, two nasty bugs got fixed!

@kris-sum
Copy link
Author

Thanks for your super speedy response to this!

Forgot you could provide the path to the block, that's a slightly cleaner solution for me, will implement it in my code.

You should definitely add BuyMeACoffee to your repo , I owe you one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants