Find the HTML ID in the URL

Hi! I looked everywhere and i can’t figure this out. Can any of you PHP wizards help me out?

I just want to determine if the current page has an ID in the URL. Like this kind of ID: “http://example.com/#idName”. If it has an ID, then i will return the HTML class i want. It should be super simple, but i can’t find it on Kirby docs, and online PHP references are too different for me to understand… Here’s my initial code:

<?php if($page->url() == $page->url() . '/#'): ?>
    class-one
<?php else: ?>
    class-one class-two
<?php endif ?>

Thank you!

I thought there was a helper for this, but couldn’t find it. I read quickly through some code and (without testing) I think this could work:

$url = $page->url();
$id = Url::toObject($url)->fragment()->toString();

In addition, you could use a custom field method or a page model to move the logic away from the template.

i think the fragment() return value will be either a string or the boolean false.

Thanks! Unfortunately i can’t make it work… I don’t have a clue of what i’m doing in this bit of code, plus it returns an error:

    <?php
        $url = $page->url();
        $id = Url::toObject($url)->fragment('#samples')->toString();
    ?>
    <?php if($url == $id): ?>
        class="layout divided"
    <?php else: ?>
        class="layout divided folded"
    <?php endif ?>

I’ve read about each of the terms in the ref docs but i still don’t understand them. Also i can’t find any info on fragment(). Plus, i don’t know how to combine $url and $id in the IF. Isn’t there any operator that says “HAS THIS” or something similar?

The fragment is not part of the request that is sent to the server. So this won’t work like this.

You could use JavaScript to get the hash value and then change your class.

I think you are overcomplicating it. If I understood your case correctly, this should be enough:

<?php if ($kirby->request()->url()->fragment()) ?>
    class-one
<?php else: ?>
    class-one class-two
<?php endif ?>

This would check if there’s any type of fragment in the current URL. If you want to check for a specific fragment you could do this instead:

<?php if ($kirby->request()->url()->fragment() === 'myfragment') ?>
    class-one
<?php else: ?>
    class-one class-two
<?php endif ?>

Thanks! I tried this code bit and i explored some other combinations, even in the starterkit, but it doesn’t work:

<div
    <?php if($kirby->request()->url()->fragment() === 'samples'): ?>
        class="intro"
    <?php else: ?>
        class="intro success"
    <?php endif ?>
></div>

I thought that this would be the best approach, but i’m intrigued with the JS approach, @texnixe. I might start to explore that solution and see if it works well.

To anyone interested, i ended up using Jquery. So if said ID is in the URL, i work on the HTML:

if(window.location.hash === "#samples"){
    $("#samples").removeClass("folded").siblings().addClass("folded");
}

In the end, it works and might probably be the best approach. Thanks for all the help!