SamC Posted March 28, 2017 Share Posted March 28, 2017 Having a weird issue, I'm using; <?php include($config->paths->templates . "views/{$page->template->name}"); ?> ...in main.php. The home page is set to use main as alternate template filename. So when on homepage, tries to include file '/site/templates/views/home'. I get an error though. I have a similar setup on another local site and have never had to do this before: // works <?php include($config->paths->templates . "views/{$page->template->name}" . ".php"); ?> I thought the file was presumed .php unless otherwise stated? Maybe I'm missing a setting somewhere but can't figure it out. Any help would be great, thanks. Link to comment Share on other sites More sharing options...
adrian Posted March 28, 2017 Share Posted March 28, 2017 "include" is pure PHP - it knows nothing about ProcessWire templates and presumed extensions, so you must point it to a valid filename with its extension. Maybe you are thinking of "wireIncludeFile" - https://processwire.com/blog/posts/processwire-2.5.2/#new-wirerenderfile-and-wireincludefile-functions Link to comment Share on other sites More sharing options...
SamC Posted March 28, 2017 Author Share Posted March 28, 2017 @adrian you got here before I had a chance to repost. I removed: <?php namespace ProcessWire; ?> ...from the top of main.php, now the includes work without having to specify .php extension i.e. // main.php <!DOCTYPE html> <html lang="en"> <head> <title><?php echo $page->title; ?></title> </head> <body> <ul> <li><a href="/">Home</a></li> <li><a href="/buildings/">Buildings</a></li> </ul> <?php include("./includes/_search-form"); ?> <?php include($config->paths->templates . "views/{$page->template->name}"); ?> </body> </html> I thought you had to have the namespace line at the top of each template? A bit confused with this, some examples have it, some don't. What are the rules here? Link to comment Share on other sites More sharing options...
adrian Posted March 28, 2017 Share Posted March 28, 2017 So what is happening there is that the file compiler is kicking in when you haven't manually declared the namespace. The file compiler turns: include($config->paths->templates . "views/{$page->template->name}"); into: include(\ProcessWire\wire('files')->compile($config->paths->templates . "views/{$page->template->name}",array('includes'=>true,'namespace'=>true,'modules'=>true,'skipIfNamespace'=>true))); The PW compile method is converting that path and adding the .php extension. You can test it yourself: Anyway, I don't think you should rely on the file compiler to do that - either add the extension, or use wireIncludeFile() 3 Link to comment Share on other sites More sharing options...
SamC Posted March 28, 2017 Author Share Posted March 28, 2017 15 minutes ago, adrian said: So what is happening there is that the file compiler is kicking in when you haven't manually declared the namespace. The file compiler turns: include($config->paths->templates . "views/{$page->template->name}"); into: include(\ProcessWire\wire('files')->compile($config->paths->templates . "views/{$page->template->name}",array('includes'=>true,'namespace'=>true,'modules'=>true,'skipIfNamespace'=>true))); The PW compile method is converting that path and adding the .php extension. You can test it yourself: Anyway, I don't think you should rely on the file compiler to do that - either add the extension, or use wireIncludeFile() Ah I see, thanks. So is it a good (for want of a better word) idea to include the namespace at the top of each file too? Like: // main.php <?php namespace ProcessWire; ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title><?php echo $page->title; ?></title> </head> <body> <ul> <li><a href="/">Home</a></li> <li><a href="/buildings/">Buildings</a></li> <li><a href="/about/">About</a></li> </ul> <?php wireIncludeFile($config->paths->templates . "includes/_search-form"); ?> <?php wireIncludeFile($config->paths->templates . "views/{$page->template->name}"); ?> </body> </html> // about.php <?php namespace ProcessWire; ?> <h1><?php echo $page->title; ?></h1> About stuff here... Link to comment Share on other sites More sharing options...
adrian Posted March 28, 2017 Share Posted March 28, 2017 Yes, adding the namespace is the "correct" way to do things with PW 3 when starting new projects. The file compiler was built primarily as a way to facilitate easy upgrades from PW 2.x without needing to change any files, and also to support non-namespaced modules in PW 3. 2 Link to comment Share on other sites More sharing options...
SamC Posted March 28, 2017 Author Share Posted March 28, 2017 3 minutes ago, adrian said: Yes, adding the namespace is the "correct" way to do things with PW 3 when starting new projects. The file compiler was built primarily as a way to facilitate easy upgrades from PW 2.x without needing to change any files, and also to support non-namespaced modules in PW 3. Awesome, thanks! Must admit I get a bit downhearted sometimes about my progress. What I do is read over my old posts, then I realise that I've actually learned hell of a lot over the last 6 months and am already making things more complex than I did previously (and actually writing php and javascript). Although my age doesn't help, that over the hill feeling creeps up on me quite a lot when I see people half my age programming all sorts of things I can't even understand a few lines of. Not stopping though, enjoy it too much. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now