Jump to content

Can't access $homepage while in user.php?


VirtuallyCreative
 Share

Recommended Posts

Hello PW Community,

I'm currently creating a custom profile page that overrides the default template (using user.php file).

I noticed however that when I tried to load user.php any calls I have to $homepage broke (my main navigation area). 

Here is the snippet that doesn't work on user.php, but works everywhere else: 

        <!-- BEGIN SIDEBAR MENU ITEMS-->
        <ul class="menu-items">
        <?php 
        // top navigation consists of homepage and its visible children
            foreach($homepage->and($homepage->children) as $child) {
              echo "<li class=''><a href='". ($child->hasChildren() ? "javascript:;" : $child->url) ."'><span class='title'>$child->title</span>";
              if($child->hasChildren()) {
                echo "<span class='arrow'></span></a>";
                
                //loop through the top navigation child's children pages and output them as nested menu item
                foreach($child->children as $childitem) {
                  echo "<ul class='sub-menu'>";
                  echo "<li class=''><a href='$childitem->url'>$childitem->title</a>";
                  echo "</a><span class='icon-thumbnail'>";
                  echo substr($childitem->title, 0, 2);
                  echo "</span></li></ul>";
                }
              }
          //step back out and continue listing Homepage Children pages
					if($child->id == $page->rootParent->id) {
						echo "</a><span class='bg-success icon-thumbnail'>";
						echo substr($child->title, 0, 2);
						echo "</span></li>";
					} else {
						echo "</a><span class='icon-thumbnail'>";
						echo substr($child->title, 0, 2);
						echo "</span></li>";
					}
				}
		    ?> 
        </ul>
        <div class="clearfix"></div>
      </div>
      <!-- END SIDEBAR MENU -->

it outputs a menu and works on all template pages except for user.php.
*note that $homepage is defined in _init.php and is loaded before user.php as I left the append to default. Do I have to specifically tell _init.php to append to user.php template?

// We refer to our homepage a few times in our site, so we preload a copy 
// here in a $homepage variable for convenience. 
$homepage = $pages->get('/');

The error I get is saying the issue is with the first foreach($homepage->and($homepage->children) as $child) line (93), but I thought it would be defined because user.php pulls in _init.php first?

Notice: Undefined variable: homepage in /var/www/html/site/templates/user.php on line 93

Fatal error: Uncaught Error: Call to a member function and() on null in /var/www/html/site/templates/user.php:93 Stack trace: #0 /var/www/html/wire/core/TemplateFile.php(268): require() #1 [internal function]: ProcessWire\TemplateFile->___render() #2 /var/www/html/wire/core/Wire.php(374): call_user_func_array(Array, Array) #3 /var/www/html/wire/core/WireHooks.php(549): ProcessWire\Wire->_callMethod('___render', Array) #4 /var/www/html/wire/core/Wire.php(399): ProcessWire\WireHooks->runHooks(Object(ProcessWire\TemplateFile), 'render', Array) #5 /var/www/html/wire/modules/PageRender.module(514): ProcessWire\Wire->__call('render', Array) #6 [internal function]: ProcessWire\PageRender->___renderPage(Object(ProcessWire\HookEvent)) #7 /var/www/html/wire/core/Wire.php(374): call_user_func_array(Array, Array) #8 /var/www/html/wire/core/WireHooks.php(549): ProcessWire\Wire->_callMethod('___renderPage', Array) #9 /var/www/html/wire/core/Wire.php(399): ProcessWire\WireHooks->runHooks(Object(ProcessWire\PageRender), 'renderPage', Arr in /var/www/html/site/templates/user.php on line 93
Error: Uncaught Error: Call to a member function and() on null in /var/www/html/site/templates/user.php:93
Stack trace:
#0 /var/www/html/wire/core/TemplateFile.php(268): require()
#1 [internal function]: ProcessWire\TemplateFile->___render()
#2 /var/www/html/wire/core/Wire.php(374): call_user_func_array(Array, Array)
#3 /var/www/html/wire/core/WireHooks.php(549): ProcessWire\Wire->_callMethod('___render', Array)
#4 /var/www/html/wire/core/Wire.php(399): ProcessWire\WireHooks->runHooks(Object(ProcessWire\TemplateFile), 'render', Array)
#5 /var/www/html/wire/modules/PageRender.module(514): ProcessWire\Wire->__call('render', Array)
#6 [internal function]: ProcessWire\PageRender->___renderPage(Object(ProcessWire\HookEvent))
#7 /var/www/html/wire/core/Wire.php(374): call_user_func_array(Array, Array)
#8 /var/www/html/wire/core/WireHooks.php(549): ProcessWire\Wire->_callMethod('___renderPage', Array)
#9 /var/www/html/wire/core/Wire.php(399): ProcessWire\WireHooks->runHooks(Object(ProcessWire\PageRender), 'renderPage', Arr (line 93 of /var/www/html/site/templates/user.php)

This error message was shown because: site is in debug mode. ($config->debug = true; => /site/config.php). Error has been logged.

Any insight anyone could provide would be appreciated! Lots to learn when using ProcessWire but it's a solid CMS and I'm really enjoying developing on the platform.


Regards,
VirtuallyCreative

Link to comment
Share on other sites

Hi VirtuallyCreative,

instead of

$homepage = $pages->get('/');
foreach($homepage->and($homepage->children) as $child) {

you might try this

$homepage = $pages->get("/");
$children = $homepage->children;
$children->prepend($homepage);

foreach($children as $child) {

(Found in this thread.)

  • Like 2
Link to comment
Share on other sites

1 hour ago, VirtuallyCreative said:

Do I have to specifically tell _init.php to append to user.php template?

The problem occurs because the "user" template is a system template. See this comment in PageRender.php:

Quote

// global prepend/append include files apply only to user-defined templates, not system templates

Specifically setting prepend/append files in the Files tab of Edit Template doesn't work either - you might need to use an include for _init.php in user.php.

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