Load snippet based on certain page grouping

Is there a way to load specific snippets in a template based on which folder a page is located in?

I’m trying to generalize a set of files for an ecommerce type system. Instead of each product type having their own template, I’d rather create a “shop > product category > individual product” system where the templates would be something similar to “shop.php, product-category.php, product.php”.

But products from different categories have different details which are set out in a table. Depending on the category, the tables with have different number of columns and different column headers. I want to be able to extract these tables as snippets and then load the appropriate one based on its product category.

Does this make sense?

Yes, that is possible. One way of doing it would be to give the snippets the name of the category, then you could call the snippet via its parent page:

<?php snippet($page->parent()->uid()); ?>

Parent folder (category) name: shoes => snippet name: shoes.php

Thanks @texnixe

The folder setup I currently have looks like this:

  • Shop
    • Rods
      • Alpha
      • Beta
    • Reels
      • Alpha
      • Beta

With your example of using the parent folder category, I would call the snippet for “Rods - i.e. product category” within my “product.php” template which is for the “Alpha - i.e. individual product” level.

If so, how can I turn it into an “if/elseif” statement so my “product.php” template can call the correct snippet if the parent category corresponds.

So, the rods snippet if the parent is Rods or the reels snippet if the parent is Reels and so on.

If you name the snippets like I suggested, you don’t need an if statement (that’s the whole idea of naming the snippet like the the parent category), you just call the snippet by the product’s parent folder name, i.e. if the product’s parent folder is reels, the reels.php snippet is called, if the product’s parent folder is rods, the rods.php snippets is called, etc.

Please don’t use capital letters in folder and filenames.

Of course, you can use a whole lot of if-statements instead, but that would only be necessary if you call the snippets differently. Also, you are more flexible with this solution, because if you add new categories, you only have to add a new snippet, but you wouldn’t have to change the product.php template.

Yeah none of my folders of files have caps, that was just my fingers going faster than my brain.

I understand now, had a major brain fart first time I read through what you were suggesting. The second time I slowed down and read it, I definitely had my Homer Simpson moment “DOUH”.

ok I ran into an issue. The snippet seems to only look up to one parent level. I have multiple levels of folders a primary category and I need the snippet to look through the entire level of folders to find the correct folder with corresponding snippet file.

The you could use

<?php snippet($page->parents()->last()->uid()); ?>

Thanks @texnixe

That still didn’t work, here is a snapshot of the folder structure. You can see the actual product is several levels down from the “reels” category folder. All reels need to use the snippet I created to generate the table for each product details which is identical for them all. Not all folders have the same depth to them but they all need to use the same snippet.

Well, what exactly does not work? Is the snippet not called at all (test with just putting an echo statement (echo 'hello';) at the top of the snippet) or does it not render any data or even throw an error?

No data and no error. I put <?php echo 'hello' ?> at the top of the snippet but still nothing. When I use the snippets for the rods folder it works and generates this.

But the folder structure for them isn’t as deep.

That’s strange. If you want, zip and upload your project somewhere and PM me a download link…

Ah, I completely overlooked the fact that the last parent is not reels or rod, but the shop. Try this:

<?php snippet($page->parents()->flip()->nth(1)->uid()) ?>

So instead of getting the last parent, we flip the order of the parents and then get the 2nd element.

Thanks @texnixe

That worked perfect