Jump to content

Recommended Posts

Posted

I just started trying to clean up an old site, and possible utilize some things I have learned over the past year (especially since my knowledge of php has gotten much better). I was trying to utilize a function to switch out the included header based on the url, but I can't seem to get it right. I started off with:

// Include New Header
function headerSwap(){
	$homepage = $config->httpHost;
	$header_one = "/page-link-one/";
	$header_two = "/page-link-two/";
		switch($homepage){
			case "/page-link-one/";
				include('headerone.inc');
					break;

			case "/page-link-two/";
				include('headertwo.inc');
					break;

			default:
				include('default.inc');
		}

}

However, it seems that it is always using the default case. I tried echoing $homepage, but that doesnt seem to be returning anything. Any help on this would be amazing.

Posted
$config->httpHost;

This does not return a URL. It returns the Current HTTP host name. E.g. processwire.com, hence doesn't match your cases.

Edit:

In addition, since you are in a function, your headerSwap() function does not know what $config is. You will need wire('config').

  • Like 1
Posted (edited)
35 minutes ago, Klenkes said:

Seems to me that you need something like a PHP var:

I doubt that's necessary in this case. He wants the current URL. @louisstephens, if all you want is the URL, then this should do it..

$homepage = wire('page')->url;

Edit...but maybe easier just to pass the URL to the headerSwap() as a parameter like this:

// Include New Header
function headerSwap($url){	
		switch($url){
			case "/page-link-one/";
				include('headerone.inc');
					break;
			case "/page-link-two/";
				include('headertwo.inc');
					break;
			default:
				include('default.inc');
		}
}

You would then call the function like this:

headerSwap($page->url);

 

Edited by kongondo
Example code
  • Like 1
Posted

@kongondo, thanks for your help! However, this has caused a new issue. In my headers, I had a few stylesheets that I was linking via 

<?php echo $config->urls->templates?>/main.css ?>

It now appears that processwire "cant" access the urls to the stylesheets anymore, and are just coming up as:

 

Screen Shot 2017-01-31 at 4.13.50 PM.png

Posted
2 hours ago, louisstephens said:

I just started trying to clean up an old site, and possible utilize some things I have learned over the past year (especially since my knowledge of php has gotten much better).

Brilliant!

1 hour ago, louisstephens said:

thanks for your help! However, this has caused a new issue. In my headers, I had a few stylesheets that I was linking via 


<?php echo $config->urls->templates?>/main.css ?>

It now appears that processwire "cant" access the urls to the stylesheets anymore, and are just coming up as:

Keeping up with the theme of learning....I can't stress enough the importance of debugging and testing your code. We have 2 great utilities in ProcessWire (besides PHP itself) to help us with these, namely $config->debug = true and Tracy Debugger. If you were testing with Tracy, she would have shown you where the error was, to be precise, these 2 PHP notices. 

  1. Undefined variable config
  2. Trying to get property of non-object in...

Why is this happening? Remember, you are including your *.inc files within the context of a function headerSwap(). Regarding the first notice, this takes us back to what I stated earlier; headerSwap() does not know what $config is. That variable is out of scope. In respect of the second notice, you are then saying give me the properties 'urls' and 'templates'  of this variable $config. That will obviously fail, since notice 1# has already told us headerSwap() does not know what $config is. The solution, is as before. In fact, the hint was in your statement:

1 hour ago, louisstephens said:

It now appears that processwire "cant" access the urls to the stylesheets anymore

ProcessWire 'cant' access. So, we give it access. As simple as this in your *.inc files:

 

<?php echo wire('config')->urls->templates?>/main.css ?>

Now you can call your function (see the suggested code in my edited post above):

 

headerSwap($page->url);

:)

  • Like 3
Posted

I can't believe I had never installed TracyDebugger, it is amazing. I did notice via the module, that all the variables I had declared in the _init.php file are now undefined (if I have my function called on the home template (I had $title = $page->get('headline|title'); and a few others). Removing the call removes the error, but at least I have a starting point and start trying to whittle down to the core issue.

  • Like 1
Posted (edited)
11 hours ago, louisstephens said:

the variables I had declared in the _init.php file are now undefined

If the only place you use headerSwap() is in your home template file, then you could just use your code directly. No real need to use the function. There are other ways to go about this, btw.

Edited by kongondo
missing text
  • Like 1
Posted
14 hours ago, kongondo said:

If the only place you use headerSwap() is in your home template file, then you could just use your code directly. No real need to use the function. There are other ways to go about this, btw.

Yeah, that is what I ended up doing after digging around and looking at everything I could find in the forums. I mean, why make things harder on myself? I appreciate all the help and guidance kongondo!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...