Jump to content

Failed to open stream, include without .php in template


SamC
 Share

Recommended Posts

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

@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

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:

58da9b344a83a_ScreenShot2017-03-28at10_19_29AM.png.c28b1efc217f243fa1cc8affe20b9dc0.png

 

Anyway, I don't think you should rely on the file compiler to do that - either add the extension, or use wireIncludeFile()

  • Like 3
Link to comment
Share on other sites

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:

58da9b344a83a_ScreenShot2017-03-28at10_19_29AM.png.c28b1efc217f243fa1cc8affe20b9dc0.png

 

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

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.

  • Like 2
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...