当从模板传递到Craft 3中的自定义模板标记时,如何在标记字段上循环?

当从模板传递到Craft 3中的自定义模板标记时,如何在标记字段上循环?

问题描述:

Supposing I have a Twig template like this:

{{ craft.myPlugin.bar(entry.specialTags) }}

How can I efficiently do the following within my custom template tag?

public function bar($tags)
{
    if ($tags->contains('blah')) { // pseudo-code!
        // return something...
    }
}

It seems that you can loop through each of the tags of a tag field like this:

for ($tagField->all() as $tag) {
    if ($tag->title == "blah") {
        // return something...
    }
}

Or to use something like the following to convert into a list of just tag titles:

$tagTitles = array_map(
    function($tag) { return $tag->title; },
    $tagField->all()
);