Template changes depending on a filter


I have the following structure in the content directory :

  -----projects A
  -----projects B
  -----projects C

The template-projectX is the following :

<div class="grid-4">
        <?php $articles = $site->index()->visible()->filterBy('tags', 'projectX', ',')->paginate(8) ?>
        <?php foreach($articles as $article): ?>
            <div class="tile txtleft">
                <a href="<?php echo $article->url() ?>">
                <?php if($image = $article->images()->sortBy('sort', 'asc')->first()): ?>
                <img class="fl" src="<?php echo thumb($image, array('width' => 100, 'height' => 100))->url() ?>" alt="<?php echo $article->title()->html() ?>">
                <?php endif ?>
                   <h3><?php echo $article->title()->html() ?></h3>
                <p><?php echo $article->text()->excerpt(100) ?></p></a>
        <?php endforeach ?>

The only difference between the templates for projectA, ProjectB or ProjectC is the filterBy(‘tags’, ‘ProjecX’).
I started to duplicate the template with this only change (1 word) in order to get the relevant display for the respective template.
Is there a simpliest way to do so, meaning having only one single template with the line and the filter tag referring to the directory which it belongs to ?

I don’t know if it is clear enough, but it would help me a lot.
Thank you for your support


You could use a variable, either the uid of the page, or a special field, which you call in your template.

Thank you for the suggestion.
I tried to change the tags filterBy(‘tags’, ‘ProjecX’) in the above code by the variable
filterBy(‘tags’, ‘<?php echo $page->dirname() ?>’)
but it doesn’t work.
Any other suggestion ?


Try uid instead of dirname(), dirname returns the sorting number of the folder, you only want the name of the folder without the number. Also, you might have to convert the output to string, but I’m not sure.

filterBy('tags', (string)$page->dirname())

I tried the following code

<?php $articles = $site->index()->visible()->filterBy('tags', '<?php echo(string)$page->uid() ?>', ',')->paginate(8) ?>

But it still doesn’t work.
I am currently totally lost !

There shouldn’t be php tags within php or quotes around the variable, try:

<?php $articles = $site->index()->visible()->filterBy('tags', $page->uid(), ',')->paginate(8) ?>