larowlan / vite-plugin-twig-drupal

Provides a vite plugin for rendering Drupal flavoured twig files with Storybook
MIT License
15 stars 4 forks source link

Add Support for Twig functions #10

Closed psebborn closed 6 months ago

psebborn commented 6 months ago

This fixes #9 , adding support for custom functions. Usage gudelines:

Add a functions object to the Twig constructor, and pass the custom functions you need into there. Use as normal within Twig templates!

import bemTwigExtension from "bem-twig-extension";
plugins: [
    // Other vite plugins.
    twig({
      namespaces: {
        templates: join(__dirname, "components/templates"),
      },
      functions: {
        bem: bemTwigExtension,
      },
      activeTheme: "zoocha",

    })

(NB. I also added the ability for passing in the 'active theme' as sometimes I've used that in projects, and it can be helpful)

larowlan commented 6 months ago

This looks good to me

There's some test twig files and it would be good to add one for this functionality

larowlan commented 6 months ago

I wonder if we should change it from 'activeTheme' to a more generic 'globalContexts' that allows passing other values too?

larowlan commented 6 months ago

The final code uses globalContext: {} instead of special casing active_theme - example here

larowlan commented 6 months ago

https://github.com/larowlan/vite-plugin-twig-drupal/releases/tag/v1.1.0

psebborn commented 6 months ago

Nice one, thank you for adding the tests, I hadn't got round to looking at them yet. I wasn't aware of globalContext - nice learning for me :)

larowlan commented 6 months ago

I added global context here too 👍

On Tue, 9 Jan 2024, 6:20 pm Paul Sebborn, @.***> wrote:

Nice one, thank you for adding the tests, I hadn't got round to looking at them yet. I wasn't aware of globalContext - nice learning for me :)

— Reply to this email directly, view it on GitHub https://github.com/larowlan/vite-plugin-twig-drupal/pull/10#issuecomment-1882596776, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEHR5WM2P7PQTZKAKL5ISTYNT4TLAVCNFSM6AAAAABBOESOKKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBSGU4TMNZXGY . You are receiving this because you modified the open/close state.Message ID: @.***>