Jump to content

Include a php file in PW template...


creativeguy
 Share

Recommended Posts

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

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

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

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

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

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
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...