Forum

Thread tagged as: Question, Runway

remove slash from current route for use with css

I have setup a template that enables me to set variables from current route not page, so that I can dynmically change content etc all from the same template based on the route. I'm trying to use it also to add a class to the body tag to allow unique style changes based on route but it adds a slash at the start.

In the template I can remove the slash like so:

<perch:content id="current_route" replace="/|">

But how do I remove the slash for this code?

// Set system variable based on current route, this can then be accessed by perch templates
// perch_template.html = <perch:content id="current_route">
PerchSystem::set_var('current_route', perch_get(0));

// Set variable based on current route.
$currentroute = perch_get(0);

    // Loads the global header template /admin/templates/layouts
    // Dynamically Loads page title from the Page Details in the CMS
    // Applies 'page' class to body tag in header from current URL
    perch_layout('global.header', array(
            'title' => perch_page_title(true),
            'page'  => $currentroute,
        ));

Current output is:

<body class="/name-of-current-route-in-url">

I need without slash:

<body class="name-of-current-route-in-url">

Thanks

Mark Watts

Mark Watts 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Mark,

You can use PHP's ltrim():

$currentroute = ltrim('/', perch_get(0));

Thanks Hussein, but this now doesn't output anything?

// Set system variable based on current route, this can then be accessed by perch templates
// perch_template.html = <perch:content id="current_route">
PerchSystem::set_var('current_route', perch_get(0));

// Set variable based on current route.
// $currentroute = perch_get(0);
 $currentroute = ltrim('/', perch_get(0)); // removes slash for use in the body tag as a class name 

    // Loads the global header template /admin/templates/layouts
    // Dynamically Loads page title from the Page Details in the CMS
    // Applies 'page' class to body tag in header from current URL
    perch_layout('global.header', array(
            'title' => perch_page_title(true),
            'page'  => $currentroute,
        ));

Outputs:

<body class="">

Any other ideas or have I implemented it incorrectly?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Sorry it should be the other way around:

ltrim(perch_get(0), '/')

That did the trick ! Thank you Hussein

ali ali

ali ali 0 points