Jump to content

How to work with include() in templates?


totoff
 Share

Recommended Posts

Hi all,

sorry, I know, this is really a beginner question, but I'm trying to wrap my head a bit more around the "Delayed output" concept as explained here.

So I have a placeholder variable $sidebar in my main template file and would like to assign an include file I already have to it (contact.inc) and render it. I managed to do so with

ob_start();
require_once('./contact.inc');
$sidebar = ob_get_clean(); 

which works fine. But I'm sure there must be an easier way via the API but can't find out.

Thanks for pointing me in the right direction. Still learning (after all this years ...)

Link to comment
Share on other sites

What is the content of contact.inc? Or why is it outputting directly?

If you want to go the delayed route, you should wrap an $out .= "..." around the content of contact.inc. At least this is how I do understand that concept. :)

Your variable in contact.inc could be $contact = " .... "; and then you add it to your sidebar:

// $sidebar is allready available in your main.inc

if ( pseudo code says I need contact information ) {
    include('./contact.inc');
    $sidebar .= $contact;   // $contact is initiated in your contact.inc
}
if ( pseudo code says I need teasers ) {
    include('./teasers.inc');
    $sidebar .= $teasers;
}
if ( pseudo code says I need teasers elsewhere) {
    include('./teasers.inc');
    $footer = $teasers . $footer;
}
...
Link to comment
Share on other sites

hi totoff,

using the TemplateFile class is perfect for this, have a look at ryans blog profile: https://github.com/ryancramerdesign/BlogProfile/blob/master/templates/archives.php#L101

simple way: file "site/templates/markup/contact-markup.php" (naming it -markup here to point out the difference to the standard template file)

John Doe<br>
Sample Street 1<br>
Sample City

template file eg "site/templates/contact.php

$contact = new TemplateFile(wire('config')->paths->templates . 'markup/contact-markup.php');
echo $contact->render(); // don't forget to echo!

more flexible: with variables! markup/contact-markup.php

<?= $name ?><br>
<?= $address ?><br>
<?= $city ?>

contact.php

$contact = new TemplateFile(wire('config')->paths->templates . 'markup/contact-markup.php');
$contact->set('name', 'john doe');
$contact->set('address', 'sample street');
$contact->set('zip', 'sample city');
echo $contact->render();

edit: in your case you can do $sidebar = $contact->render() instead of echoing it... forgot the point of delayed output  ;)

  • Like 6
Link to comment
Share on other sites

Or you can use wireRenderFile a new addition to ProcessWires functions:

https://processwire.com/blog/posts/processwire-2.5.2/#new-wirerenderfile-and-wireincludefile-functions

echo wireRenderFile('markup/contact-markup.php', array(
   'name'=>'john doe',
   'address'=>'sample street',
   'zip'=>'sample city'
));
I did not test this example, but according to the docs it should work like this. Requires ProcessWire 2.5.2
  • Like 4
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...