Snippets not working, undefined variable error

Hello,

I am trying to use snippets to clean up my code, however it won’t work unless I write the entire code in my template without snippets.

My code is:

<?php snippet('header') ?>
<?php
	$filterBy = get('filter');
	$projectsPage = page('projects');
	$projects = $projectsPage
		->children()
		->listed()
		->when($filterBy, function($filterBy) {
		return $this->filterBy('category', $filterBy);
		})

?>

<ul>
	<?php foreach ($projectsPage->children() as $project): ?>
	<li class="container">
		<div class="carousel">
			<ul class="ul draggable">
				<!-- Project Tags -->
				<?php snippet('project-info') ?>
				<?php snippet('project-gallery') ?>
		</li>
		<?php endforeach ?>
	</ul>
	
</div>

<?php snippet('footer') ?>

and I get the following error:

How do I fix this?

Thank you in advance :slight_smile:

You need to pass variables to the snippet:

<?php snippet('project-info', compact('project')) ?>
<?php snippet('project-gallery', compact('project')) ?>

Thank you!