1

When I include specific Twig template component multiple times, how can I give it a unique ID (or class)?

Components are not inside for loop, so I cannot use loop.index. I also don't wont to pass unique variable myself when I include it. And using random Twig function doesn't ensure that the result is unique.

Let's also assume that each component doesn't have unique Craft element assigned to it, like entry - in such case, I could just use this entry ID.

  • Could you be a bit more specific with what sort of data you are working with? Personally this is not something I have ever needed to do whilst working with Craft and Twig. That isn't to say that your use case isn't valid, but I can't quite picture what you need this for. – Jamie Wade Feb 25 '19 at 8:58
  • @JamieWadeI can't give you anything specific, because there isn't any use case I'm thinking of now. When I create "page builder" using matrix field, templates of specific blocks of matrix can have JS included in them. And I want this JS to work only on specific instance of block, no other blocks. So naturally, I use matrix element block ID to give unique HTML attribute to div and then I run javascript using that unique attribute. I was just wondering how to do such a thing where there is no unique ID available that comes from Craft elements. – piotrpog Feb 25 '19 at 19:12
  • Interesting, thanks for explaining! Looks like Robin has solved this for you anyway :-) – Jamie Wade Feb 26 '19 at 9:11
3

You can create a unique ID function in PHP and pass it to Twig

{% set uniqueId = uniqueId() %}

And your PHP part

class Extension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
    public $uniqueIds;

    public function getFunctions(): array
    {
        return [
             new Twig_SimpleFunction('uniqueId', [$this, 'getUniqueId'])
        ];
    }

    /**
     * getUniqueId
     *
     * @return string
     */
    public function getUniqueId(): string 
    {
        // generate a random string
        $id = StringHelper::randomString('12');

        // check if it's already set
        while (\in_array($id, $this->uniqueIds, true)){
            // if so, use another one
            $id = StringHelper::randomString('12');
        }
        // set it as "used"
        $this->uniqueIds[] = $id;

        return $id;
    }
}
| improve this answer | |
  • I was thinking of some Twig only Solution, but this is also useful - thanks! – piotrpog Feb 25 '19 at 23:32
  • where would this php file sit in Craft's folder structure? – erwinheiser Jun 4 '19 at 14:12
  • Where ever you like. Craft doesn't make any assumptions about your custom folder structure. You need to include your files to your composer and register your Twig extension via module/plugin but you can place them where you like. There is no strict path as in Craft 2. – Robin Schambach Jun 4 '19 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.