Jump to content

Help with wireRenderFile/wireIncludeFile/include


SamC
 Share

Recommended Posts

I touched on this briefly in another thread yesterday which @adrian kindly helped me out. I've been spending a lot of time working out the basics, finding best ways to use fields (repeater matrix/pagefield etc.) in order for it to be as easy as possible for the end user, this is my main aim.

I'm still trying to work out what's actually happening here though when including/rendering other files:

// config.php
$config->prependTemplateFile = '_init.php';

// _init.php
$body = $page->body;

// main.php
    <?php
        // Notice: Undefined variable: body in...
        //wireIncludeFile($config->paths->templates . "views/{$page->template->name}");

        // Notice: Undefined variable: body in...
        //echo wireRenderFile($config->paths->templates . "views/{$page->template->name}");

        // works - adrian explained why this one works in another thread
        //include($config->paths->templates . "views/{$page->template->name}" . ".php");

        // works
        //wireIncludeFile($config->paths->templates . "views/{$page->template->name}", array('body' => $page->body));
    ?>

// about.php - using main as alternate template filename
<?php namespace ProcessWire; ?>

<div class='body-row'>
    <div class="wrapper">
    <?php if ($body) {
        echo $body;
        }
    ?>
    </div>
</div>

Could anyone explain why in the 4 example lines I tried, two work but the other two don't?

I like the idea of wireRenderFile() as it has come in useful being able to send additional variables, like what I do here:

// admin - edit - about
// has a pagefield select dropdown (insertContentBlock) to choose a content block

// admin - edit - content block
// is a single repeater matrix (genericContent) with title, body, images fields

// main.php
<?php if ($page->insertContentBlock): ?>
    <div class='content-block'>
        <div class="wrapper">
            <?php
                // get Id of the referenced page
                $blockId = $page->insertContentBlock;
                wireIncludeFile("./includes/_content-block", array('blockId' => $blockId));
            ?>
        </div>
    </div>
<?php endif; ?>

// _content-block.php
<?php namespace ProcessWire;

foreach ($blockId->genericContent as $item) {

    if ($item->type == 'sectionTitle') {
        echo "<h2>{$item->title}</h2>";
    }
    else if ($item->type == 'sectionBody') {
        echo $item->body;
    }
    else if ($item->type == 'sectionImages') {
        $img = $item->images->first();
        echo "<img src='{$img->url}'>";
    }            
}

?>

But I digress. Still a bit confused though why my variables in _init.php are not available at 'mysite.com/about/' if this template loads before main.php, if that makes sense. Any help would be great. Thanks.

Edited by SamC
Typo in title
Link to comment
Share on other sites

I think it's just about PHP variable scope. When you use PHP's built in "include" it is including the contents of the file directly, but when you call a custom function to do the include (eg wireIncludeFile(), or your own custom function), then the $body variable doesn't make it through, which is why wireIncludeFile() and wireRenderFile() have the ability to pass variables in an array. 

  • Like 3
Link to comment
Share on other sites

11 hours ago, adrian said:

I think it's just about PHP variable scope. When you use PHP's built in "include" it is including the contents of the file directly, but when you call a custom function to do the include (eg wireIncludeFile(), or your own custom function), then the $body variable doesn't make it through, which is why wireIncludeFile() and wireRenderFile() have the ability to pass variables in an array. 

Hi @adrian thanks. Does that mean because the code in (for example) about.php is running inside a function, the outer variables (i.e. in _init.php) are not available. I thought the variables in _init.php were global to all templates because this template loads first. Guess I'm wrong :P

I just took the variables out of _init,php, no skin off my back just writing '$page->body'.

Link to comment
Share on other sites

4 hours ago, SamC said:

Hi @adrian thanks. Does that mean because the code in (for example) about.php is running inside a function, the outer variables (i.e. in _init.php) are not available. I thought the variables in _init.php were global to all templates because this template loads first. Guess I'm wrong :P

I just took the variables out of _init,php, no skin off my back just writing '$page->body'.

Variables declared in _init.php are available in all templates, but php scoping rules still apply when trying to access a variable inside a function. Remember, even if you tried to access $body (defined in _init.php) in a function elsewhere in _init.php, you still wouldn't be able to. 

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