Search
Other Snippets questions
Forum

foreach structure in Snippets?

Brent Critchfield's Avatar Brent Critchfield
I suspect I already know the answer to this, but I thought I'd check anyways... is there any way to implement a foreach loop in Snippets? I'm building a menu (you already helped me with a bug in Articles Anywhere for this project). I'd like to be able to write a tag like
{snippet alias="menu" tag="appetizers,salads,sandwiches,desserts"}
and have the snippet repeat for each of those categories.
Peter van Westen's Avatar Peter van Westen ADMIN
No, Snippets does not have that functionality.

You could make a loop with php through Sourcerer, which outputs the Snippet tags.
Please post a rating at the Joomla! Extensions Directory
Brent Critchfield's Avatar Brent Critchfield
Ok, I'll look into that approach. Thanks!
Peter van Westen's Avatar Peter van Westen ADMIN
You can do something like:
{source}<?php

$tags = ['appetizers', 'salads', 'sandwiches', 'desserts'];

foreach($tags as $tag)
{
   echo '{snippet alias="menu" tag="' . $tag . '"}';
}

?>{/source}

Make sure the Snippets system plugin is ordered AFTER Sourcerer.
Please post a rating at the Joomla! Extensions Directory
Brent Critchfield's Avatar Brent Critchfield
Thank you!!! Works great.
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription