Jump to content

Recommended Posts

Posted

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.

Posted

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');

 

Posted

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.

Posted
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

  • Like 1
Posted
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.

Posted
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?

  • Like 1
Posted

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>

 

Posted
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

Posted

There's no include statement before $config->paths->site . 'template/include/random-trailers-5.php'

Posted
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!

Posted

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

  • Like 2
Posted

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.

10.thumb.png.418b240c318e63fb4025bc7f49f2a603.png

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:

11.thumb.png.c4fb097372538936d3c0780204b6d03b.png

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 :)

  • Like 1

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
  • Recently Browsing   0 members

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