Maintain one Portfolio - extra sub pages become visible with a client login

Hi, It would be great to get some advice.
I have a portfolio. Which works like so…
One main page called Portfolio. This then has sub pages for all my different projects, Which then in turn have images as files.

My main page shows the first image of each subpage which you can then click through to get to each project which then displays the images.

I would like a may of maybe tagging each project as “Public or Client” So when a front end client is logged in the “Extra” Client projects would appear in the portfolio aswell.

I thought this would be cool as means less pages and means ill only have to manage one large portfolio, rather than having an extra “area” which the content would need managing separatley.

The only problem is I have zero idea where to start as a php / Kirby newbie. Any help here would be amazing, maybe it’s already been done and is actually quite simple to achieve?

You’d want to look into using the $kirby->user() function to determine whether a user is currently logged in or not and then only render the “protected” entries if that function returns a logged in user.

Here’s a quick example of how I’d do it (please note the comments need to be removed to actually run this code, I just typed this directly into the forum):

<?php foreach($page->children() as $entry): ?> // I'm assuming your portfolio entries are child-pages of your main page. Loop over those
   <?php if(!$entry->isProtected()->toBool() || $kirby->user()): ?> // isProctected is your toggle in the blueprint. If it's not set => Just render the page. If it *is* set => Only render the page if $kirby->user() also returns a logged in user
        <h2><?= $entry->title()?></h2> // Rendering your portfolio entries however you'd like
   <?php endif; ?>
<?php endforeach;?>

Note that you cannot use page caching with such a solution, because the protected projects would also be stored in the cache.

Hi Thanks for this, great help, how would I go about making certain entries “protected” though.
Unless i’ve missed something that would work but only if before hand there was a way of marking / tagging certain sub pages as protected?

Would this be because the cache needs to be publicly visible? If I had a separate client area handled separately would I also need to disable page caching?
Do things like kirby’s resize and crop functions work with caching disabled?

No, because the cache stores what is first shown and if that happens to be the protected stuff, it will be shown to all users.

So the isProtected() thing you see in my example would just be the value stored in a Kirby toggle field that you yourself add to the blueprint (see Toggle | Kirby CMS).

That way, you can store the information on whether a page should be protected or not and then use it like shown in my previous snippet.

If you also want to protect the page itself (i.E. don‘t render the page for people who aren’t logged in if the page is set to be protected) you can basically wrap the entire template of that page in the same if statement shown in my example. That will then prevent unauthorized people from seeing protected pages.