Jump to content

PHP error on homepage


SamC
 Share

Recommended Posts

Been at this for awhile but can't work out why this issue is happening. I use main.php as my 'alternate template name' on both home and basic-page templates. The function is here (from _func.php):

/**
* Render navigation from array of pages
*
* @param PageArray $items
* @param $menuClassName
* @return string
*
*/
function renderMenu(PageArray $items, $menuClassName) {

    $str = '';

    foreach ($items as $item) {

        $menuText = $item->get('menuLinkTitle|title');

        if ($item->id == wire('page')->id) {
            $str .= '<li class="current">';
        }
        else {
            $str .= '<li>';
        }
        $str .= "<a href=\"$item->url\">$menuText</a></li>";
    }

    // if output was generated above, wrap it in a <ul>
    if ($str) {
        $str = "<ul class=\"$menuClassName\">$str</ul>";
    }

    return $str;
}

...and I call it in main.php thus:

<!-- main navigation -->
<nav>
    <?php
        echo renderMenu($pages->get('/')->and($pages->get('/')->children), "main-menu");
    ?>
</nav>

So, when on a page created with a basic-page template, for example, 'About', at mysite.com/about/ the menu works fine. Shows 'Home | About | Contact'.

However, when I now go to the homepage, it's error time!

Fatal error: Uncaught TypeError: Argument 1 passed to renderMenu() must be an instance of PageArray,
instance of ProcessWire\PageArray given, called in
/Users/Sam/dev/ecoop.dev/site/assets/cache/FileCompiler/site/templates/main.php on
line 28 and defined in /Users/Sam/dev/ecoop.dev/site/templates/_func.php:10 Stack trace: #0

(this is just the first couple of lines).

Ok, so I echo out a bunch of stuff, like:

echo $pages->get('/')->and($pages->get('/')->children);
// returns 1|1015|1018|1019|1022 on EVERY page including HOME

All I wanted to do was to pass this array into the function, and print the menu on every page. My PHP 'skills' are limited you will no doubt see.

Basically, I'm lost! Any help is much appreciated. My main confusion is why doesn't this code work on the homepage, but works on every other page? Thanks.

 

 

 

 

Link to comment
Share on other sites

12 minutes ago, adrian said:

Looks like you have a namespace issue. Have you tried clearing the FileCompiler cache?

I don't think it should matter, but do you have the ProcessWire namespace added to your home template, but not others?

Thanks for the info. The only file with something similar is admin.php which has '<?php namespace ProcessWire;' at the top.

I deleted everything out of '/sites/assets/cache/FileCompiler/' (if this is the correct way??). Problem still persists though.

Been searching through the API and the code seems correct, but obviously something is wrong.

Link to comment
Share on other sites

If I put this in main.php:

    <nav>
        <?php

        function renderMenu(PageArray $items, $menuClassName) {

    $str = '';

    foreach ($items as $item) {

        $menuText = $item->get('menuLinkTitle|title');

        if ($item->id == wire('page')->id) {
            $str .= '<li class="current">';
        }
        else {
            $str .= '<li>';
        }
        $str .= "<a href=\"$item->url\">$menuText</a></li>";
    }

    // if output was generated above, wrap it in a <ul>
    if ($str) {
        $str = "<ul class=\"$menuClassName\">$str</ul>";
    }

    return $str;
}


            $arr = $pages->get('/')->and($pages->get('/')->children);
            echo renderMenu($arr, 'main-menu');
        ?>
    </nav>

Ignore the terrible formatting, just copied and pasted it in there! So, with this, it works on all pages.

Link to comment
Share on other sites

1 minute ago, adrian said:

Maybe your file compiler settings are not compiling included files:

Screen Shot 2016-09-24 at 7.28.43 AM.png

Ah, welcome back sanity! Spot on :) thanks Adrian. Mine was set to 'Yes (template file only)'. Had no idea what this section actually did, now I have a better idea.

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