Set snippet path in template

Not sure if I’m getting the terminology right.

I’m setting up a portfolio website. I use ‘portfolio-item.php’ as a template file. I have several portfolio-items, but I don’t want to set-up multiple template files. For that, I want to use snippets.

I don’t want to put all portfolio snippets in the general ‘snippets’ directory, but in a subdirectory (‘snippets/portfolio’).

In my template file I can call the snippet for the portfolio item with the following code:

<?php snippet($page -> slug()) ?>

So if the slug of my portfolio item is ‘portfolio-item-01’ this line of code tries to find the snippet ‘portfolio-item-01.php’ in the ‘snippets’ directory.

How do I need to edit this code so that it looks for the snippet ‘portfolio-item-01.php’ in the ‘snippets/portfolio’ subdirectory?


Just include the path:

<?php snippet('portfolio/' . $page -> slug()) ?>
1 Like

Thanks! I didn’t know (and couldn’t find) the proper syntax.

Just as a thought for you, i’ve built a few portfolio sites (like this one for a client) and i find the Builder plugin great for this. Are you using snippets because you have different kinds of content? Like some are a set of one or more images and others are video etc? Basically with the builder plugin you can set up a set of blocks containing code for each content type and then add one or more of them to a page, rather then using snippets.

I set one up for video, another for slideshows, one for embedding 3D models in an interactive widget via SketchFab. This means the client can add multiple types of content to each page and set the order they appear in the because the modules are drag and drop enabled. You can also preview the content in the panel with a little more work.

I also put this on the body tag <body id="<?= $page->uid() ?>"> which gives you a hook if you want to art direct a post and add stuff via CSS withoug effecting other pages.

Just some thoughts for you. Have fun building your portfolio.

Thanks for the tip. I’m not using the panel for this site but this might be very useful for other projects.