frctl / twig

Use Twig templates with Fractal.
32 stars 34 forks source link

Allow Twig paths to be compatible with PHP Twig #29

Closed BrianWalters closed 4 years ago

BrianWalters commented 5 years ago

Here's my use case: I want a Fractal instance to be a component library for a Symfony project. I would like to use the same Twig templates in the Symfony codebase and in the Fractal codebase.

My problem is Fractal has a very opinionated way of including templates. You don't use a filesystem path, like PHP Twig does, instead you use a Fractal handle. If I had this directory structure:

└── components
    └── button
        └── button.twig

To include the button component, I would have to do:

{% include '@button' %}

but I want to configure Fractal to include it like:

{% include 'button/button.twig %}

so that I could use the same templates with an instance of PHP Twig.

I could get around this by making a custom Twig Loader for PHP Twig, like what these guys did for Drupal 8. But I'd rather not have to introduce this complexity and requirement for using this component library. And this seems like a fairly common use case for PHP devs. Is there any way around this? Could the Twig adapter be extended to handle this use case?

BrianWalters commented 5 years ago

I figured out a workaround for my issue. Looks like Fractal does have support for paths, but twigjs does compute those paths to be relative sometimes. I found this issue https://github.com/twigjs/twig.js/issues/73 that does provide a way for twigjs to not use relative paths. Anyway, gonna make a PR for this and add a config option.

Chapabu commented 5 years ago

We've had this issue on Drupal 8 projects in the past. If I remember correctly, we resolved it by using an array of templates (in the docs here, right at the bottom). I think it looked something similar to:

include ['@button', 'buttons/button.html.twig`]

The first path doesn't resolve in Drupal, so it falls through to the second which does.

I'd have to confirm this with a colleague though (@iwright?)

iwright commented 5 years ago

Hello,

As you have noticed, twig works slight different between the languages. Below are how we have worked with Drupal 8 template fallbacks and Fractal fallbacks. Please note, the Drupal 8 version is actually to make the theme child friendly too.

For Drupal, we also use the components module so you can set the correct namespace. https://www.drupal.org/project/components

Fractal: Node twig 1.11.0 and below doesn't support template array syntax.

{% if button %}
  {% set button_cta = {
    cta: button.cta,
    type: button.type,
    href: button.href,
    text: button.text,
    external: button.external,
    classes: button.classes
  } %}

  {# Fractal #}
  {% include '@buttons' ignore missing with button_cta %}

  {# Drupal #}
  {% include '@atoms/buttons/buttons.twig' ignore missing with button_cta %}
{% endif %}

Drupal 8:

{{ include([
  directory ~ '/templates/includes/footer.html.twig',
  '@base_theme/includes/footer.html.twig'
]) }}

I hope this helps,

Chapabu commented 4 years ago

I think this one was fixed by #30. If not, then please feel free to re-open :)