patternlab-node icon indicating copy to clipboard operation
patternlab-node copied to clipboard

Adding custom TwingExtension functions and filters in engine-twig.

Open JEltgroth opened this issue 5 years ago • 7 comments

I am using Pattern Lab Node v5.11.1 on Linux, with Node v14.6.1, using a Twig Edition.

Installed Dependencies: "@pattern-lab/cli": "^5.11.1", "@pattern-lab/core": "^5.11.1", "@pattern-lab/engine-twig": "^5.10.2", "@pattern-lab/uikit-workshop": "^5.11.1"

I currently am using the old Pattern Lab PHP with Twig engine at work for our design library, and am exploring what it would take to update pattern lab to the new versions in node.

My comment specifically addresses Twig Extensions for functions and filters.

In PHP PL with Twig, creating twig extensions is dirt simple, and we have many that we use in the course of our work. When I initially tried out Node PL, I installed it with the included twig engine which was engine-twig-php. I noticed there was a file in the root PL directory which was to be used to twig extensions.

I then read that, unless I misunderstood, the @patternlab/engine-twig should be the de facto twig engine in the node environment because of it's full-featured compatibility with the Twig Specs via twing, I reinstalled my PL Node environment to use that instead.

My issue is that after reading the files and documentation for engine-twig and twing in general, I can't seem to get any custom TwingExtension classes to work with pattern lab as the engine builds the template, leaving me with build errors stating custom filters or functions are unrecognized.

I don't know if I'm either missing something critical or if this isn't currently allowed in the engine-twig dependency as of yet.

If it is possible and someone could point me in the right direction, that would be appreciated.

If it is not currently a feature, I would like to request that the ability to pass in custom extensions to the renderer as it executes via a customExtension.ts/js file in the PL directory or something similar be added to Pattern Labs Twing Engine.

JEltgroth avatar Jul 26 '20 07:07 JEltgroth

It's hard to keep track of everything. This issue has been automatically marked as stale because it has not had recent activity, neither from the team nor the community. It will be closed if no further activity occurs. Please consider adding additional info, volunteering to contribute a fix for this issue, or making a further case that this is important to you, the team, and the project as a whole. Thanks!

stale[bot] avatar Oct 04 '20 04:10 stale[bot]

I cannot even get engine-twig to compile twig files. I get registerPartial errors.

[11:19:10] Starting 'buildPatternlab'... registerPartial(07-pages-page - pages-page - undefined - 07-pages/page.twig) registerPartial(06-templates-page - templates-page - undefined - 06-templates/page.twig) registerPartial(02-global-colors-colors - global-colors - undefined - 02-global/colors/colors.twig) registerPartial(02-global-fonts-fonts - global-fonts - undefined - 02-global/fonts/fonts.twig)

chazchumley avatar Oct 23 '20 15:10 chazchumley

It's hard to keep track of everything. This issue has been automatically marked as stale because it has not had recent activity, neither from the team nor the community. It will be closed if no further activity occurs. Please consider adding additional info, volunteering to contribute a fix for this issue, or making a further case that this is important to you, the team, and the project as a whole. Thanks!

stale[bot] avatar Dec 25 '20 12:12 stale[bot]

I’d love to see some docs on adding custom twing filters and functions in pattern lab as well. I’m in the same boat as @JEltgroth

patrickcate avatar Dec 30 '20 02:12 patrickcate

I'm also interested in using custom twig filters and functions in PL Node. Has anyone been able to do this successfully?

jesconstantine avatar Apr 07 '21 11:04 jesconstantine

It's hard to keep track of everything. This issue has been automatically marked as stale because it has not had recent activity, neither from the team nor the community. It will be closed if no further activity occurs. Please consider adding additional info, volunteering to contribute a fix for this issue, or making a further case that this is important to you, the team, and the project as a whole. Thanks!

stale[bot] avatar Jan 09 '22 00:01 stale[bot]

I were able to add FakerPHP to PTL Node using alter-twig.php in root folder. This is the contents of

alter-twig.php
<?php
require './vendor/autoload.php';

function addCustomExtension(\Twig_Environment &$env, $config)
{
    $env->addFunction(new \Twig_SimpleFunction('faker', function ($string, $options="") {
        $faker = \Faker\Factory::create();
        if ($options != "") {
            if (is_array($options)) {
                return $faker->$string(...$options);
            } else {
                return $faker->$string($options);
            }
        } else {
            return $faker->$string;
        }
    }));
}

Contents of

composer.json
{
    "require": {
        "fakerphp/faker": "*"
    }
}

And I'm able to utilize Faker functions inside my twig partials, like {{ faker('realText') }}, {{ faker('numberBetween', [0, 999]) }} or {{ faker('date', 'd.m.Y') }}

DarkPreacher avatar Jan 10 '22 09:01 DarkPreacher