creativeguy Posted October 1, 2017 Share Posted October 1, 2017 I found a few threads about this, and the answer is exactly what I expected - if you want to include a file, just go ahead and include a file. The problem is, I obviously go one more problem backward than everyone else who's asked this, because I can't make it work. I'm a VERY new developer (long time graphic designer). I can write an includes statement, but no version of config urls or templates roots or whatever gets me there. How Do I included a PHP file that's in my templates folder, inside another folder called "include", but get to that by using a config method so it doesn't lose the path when I deploy it? It's too simple a question for anyone else to have asked - yeah, I'm that new to all this. Help is appreciated. Link to comment Share on other sites More sharing options...
abdus Posted October 1, 2017 Share Posted October 1, 2017 Assuming your templates folder is set up like this: /site/templates home.php my-template.php includes/ helpers.php Then inside a template file you can do // my-template.php include 'includes/helpers.php'; // OR include __DIR__ . '/includes/helpers.php'; __DIR__ magic constant resolves to the directory of the script and by concatenating it you can build paths for other files. What happens when the file is in the parent directory, say /site/? Then: include '../helpers.php'; // OR $config->paths->site . 'helpers.php'; // OR include realpath('../helpers.php'); Link to comment Share on other sites More sharing options...
creativeguy Posted October 1, 2017 Author Share Posted October 1, 2017 Hello again, sir. Amazing... NONE of those worked. I've been staring at my screen, reading each letter to make sure I didn't typo, and it's not getting there. I don't get an error, I just get nothing. The includes file is HTML with embedded PHP - do I have to echo something? The include returns NULL. I feel like I'm missing something really basic, but I can't figure it out. Link to comment Share on other sites More sharing options...
szabesz Posted October 1, 2017 Share Posted October 1, 2017 Hi Abdus, 10 minutes ago, abdus said: $config=>paths->site . 'helpers.php'; I'm wondering what this is. 1 Link to comment Share on other sites More sharing options...
abdus Posted October 1, 2017 Share Posted October 1, 2017 2 minutes ago, szabesz said: $config=>paths That fat arrow is a typo, it was meant to be a skinny arrow $config->paths. Thanks for the heads up 1 Link to comment Share on other sites More sharing options...
creativeguy Posted October 1, 2017 Author Share Posted October 1, 2017 Just now, abdus said: That fat arrow is a type, it was meant to be a skinny arrow $config->paths. Thanks for the heads up Yes, I caught that and and corrected it when I tried it, but it didn't work in any case. Link to comment Share on other sites More sharing options...
abdus Posted October 1, 2017 Share Posted October 1, 2017 1 minute ago, creativeguy said: didn't work Can you share your code? Do you have namespace ProcessWire; declared at the top of your file? 1 Link to comment Share on other sites More sharing options...
creativeguy Posted October 1, 2017 Author Share Posted October 1, 2017 Here's one of the includes code. This is the whole code inside the includes file. <section> <hr/> <h4 class='caps' style='font-style:italic;'>Featured Collections</h4> <div class="home-features"> <?php $allFeatures = $pages->find('template=feature-page,sort=random'); $counter=0; ?> <?php foreach($allFeatures as $feature): ?> <?php $counter++; ?> <div class="home-feature"> <a href="<?= $feature->url ?>"> <img src="<?= $feature->images->first()->url ?>" /> </a> <p><?= $feature->title ?></p> </div> <?php if ($counter >= 5) break; ?> <?php endforeach ?> </div> <a class="seeall" href="<?= $config->urls->root ?>features">See All the Features ></a> </section> Link to comment Share on other sites More sharing options...
abdus Posted October 1, 2017 Share Posted October 1, 2017 2 minutes ago, creativeguy said: <a class="seeall" href="<?= $config->urls->root ?>features">See All the Features ></a> Even when you include a file, you still need to declare namespace at the top. 2 minutes ago, creativeguy said: <section> Change this to <?php namespace ProcessWire; ?> <section> Also, if /features pages exists, you can just use $pages->get('/features/')->url or $pages->get(<id>)->url instead Link to comment Share on other sites More sharing options...
creativeguy Posted October 1, 2017 Author Share Posted October 1, 2017 Done. No difference. Returns NULL. 1 Link to comment Share on other sites More sharing options...
abdus Posted October 1, 2017 Share Posted October 1, 2017 There's no include statement before $config->paths->site . 'template/include/random-trailers-5.php' Link to comment Share on other sites More sharing options...
creativeguy Posted October 1, 2017 Author Share Posted October 1, 2017 1 minute ago, abdus said: There's no include statement before $config->paths->site . 'template/include/random-trailers-5.php' DAMN - copy, paste,typing error. That got it. Thanks again! Link to comment Share on other sites More sharing options...
abdus Posted October 1, 2017 Share Posted October 1, 2017 I almost always regret it when I copy paste some code. It takes many times more to debug than just write the code again / refactor it 2 Link to comment Share on other sites More sharing options...
creativeguy Posted October 1, 2017 Author Share Posted October 1, 2017 Solid advice - will do from now on. Link to comment Share on other sites More sharing options...
clsource Posted October 1, 2017 Share Posted October 1, 2017 As a side note you could also use the PW functions wireRenderFile() or wireIncludeFile() or if you prefer objects $files->render() and $files->include() https://processwire.com/api/ref/files/render/ https://processwire.com/api/ref/files/include/ 3 Link to comment Share on other sites More sharing options...
SamC Posted October 2, 2017 Share Posted October 2, 2017 I do this in my 'main.php' file to include a bunch of other stuff in order to keep it small. Works fine, never had a problem with the paths. Excuse the poor annotations. My templates also all have... <?php namespace ProcessWire; ?> ...at the top too so I don't lose the scope of functions in '_func.php' (which is prepended to 'main.php'). One of the functions for example is called in '_top-bar.php' file and without the namespace you get a undefined function warning which confused the hell out of me at first. As mentioned, you can also use wireIncludeFile(), but with this approach, if you can also pass additional information into the template which is great within loops! Stuff like: I pass the current loops page object ID into the '_card.php' file which can include stuff like this: // _card.php <?php namespace ProcessWire; // $pageId in first loop os 1101, $pageId in second is 1103 $entry = $pages->get($pageId); ?> <div class="card <?= $entry->name; ?>"> <p>The title of the page which has been passed to me is <?= $entry->title; ?></p> </div> Would probably make more sense if you did some experimenting but give it a shot. Good luck 1 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