Snippets in a one-page


#1

I’m using the one-pager technique but one of my snippets has got a little out of hand, ideally, i’d like to use snippets within the snippet. However this means having logic within the inner snippet. How would be best to go about this?

I would like to put the following code in a snippet called textinfo.php

<div class="project-title">
   <a href="<?= $project->url() ?>">
      <?= $project->title()->html() ?>
   </a>

</div>

... for brevity


<div class="project-text">
   <?= $project->text()->kirbytext() ?>
</div>

Taking out on the for loop within the section snippet

<?php foreach($data->children()->visible() as $project): ?>
    <div data-title="<?= $project->uid() ?>">
        
        <?php snippet('textinfo')?>

        ...

    </div>
<?php endforeach ?>

#2

You have to pass your variable to the snippet:

<?php foreach($data->children()->visible() as $project): ?>
    <div data-title="<?= $project->uid() ?>">
        
        <?php snippet('textinfo', ['project' => $project])?>

        ...

    </div>
<?php endforeach ?>