Forum
Change value of variable using URL
Hi,
I'm using this setup to populate $navgroup
with a value from the URL.
$page_url = perch_page_url(['include-domain' => false,], true); // Output the full URL of the current page
$dir_name = dirname($page_url); // Returns a parent directory's path
$navgroup = str_replace('/','', $dir_name); // Replace all occurrences of the search string with the replacement string
print $navgroup;
It only half works, here are some examples:
Example 1: site.com/de/demo/
$navgroup
has a value of de
when I visit the demo
page. This is what I want to happen.
Example 2: site.com/de/
$navgroup
has a value of /
when I visit the de
page
I need it to have a value of de
Example 3: site.com/de/demo/test
$navgroup
has a value of demo
when I visit the test
page
I need it to have a value of de
Wherever the user is on the de
branch, I'd like $navgroup
to output de
What's the best way to achieve this?
I'm trying to get this to work so I can use $navgroup
to change the main navigation on a multi-language website.
Using something like this:
perch_pages_navigation(['navgroup'=> $navgroup]); // output navgroup based on value of $navgroup
So is the value you want always the first url segment?
Hi Drew,
Yes please.
Hello Stephen,
Assuming you only get the URL this way:
So the value of
$page_url
doesn't includehttps://
, you could use something like this:Great stuff, thanks Hussein.
I'm working on a multi-language website (using the branching technique), and was looking for a way to use part of the url as a variable
Putting it all together, here's what I'm using change the
navgroup
based on the current language branch. I've also usedset_var
so I can use$nav_group
in my html templates.In Runway you can also grab the segment from the route using
perch_get()
.Hi Drew,
I'm curious, how would I grab the segment from the route using
perch_get()
?It's usually
perch_get(0)
, or you can name it in your route. The routing values are always displayed in the debug for a routed page.Ah, yes, very neat. Thanks.