Jump to content

$session->redirect($page->child->url); renders blank


Claus
 Share

Recommended Posts

Hello everybody!

This is my first foray into the world outside Wordpress, and ProcessWire looked like it would be able to do what I want to build without overhead, and at the same time an opportunity to learn. I have no coding skills at all. I am diving head-first into the wall here, so bear that in mind please.

I have a few questions on the site I’m working on. I’m using the ‘intermediate edition’ where ‘_main.php’ renders some divs of menus and content conditionally, if the variables are declared in the templates (that are loaded before the _main.php).

The home is a column of sub-pages like this:

Home

Products

News

About

Contact

Under the Products I have several sub-pages with products, which in turn have sub-pages of their own. I’d like to have a click on the Products to automatically link to the first product’s first child-page, so I saw this in another thread:

$session->redirect($page->child->url);

But when I place it in my Products template, and click on ‘Products’ in the main menu, my browser redirects one level at the time, until it gets to the first child of the first product, but the page is then blank (view source shows that the page is empty). The URL looks correct, and if I turn off the redirect and load the very same URL then the site renderes as expected.

So my question is what I’m doing wrong here?

Additionally I’d like to ask if there is a way to make a redirect go directly to the first child of the first product-page, instead of going in steps one level at the time?

*****

Another question entirely is that I’m using the ‘renderNav’ in ‘_func.php’ to render my menus, and in the sub-menus I get the name of the sub-menu at the top of the menu like this:

Products

Apples

Pears

Oranges

How do I remove the ‘Products’ from the top?

*****

Thanks!

Claus

Link to comment
Share on other sites

hi claus,

welcome to pw!

i'm sorry, i think i don't really understand what you are trying to achieve with your first question and especially WHY you want to do that?

Additionally I’d like to ask if there is a way to make a redirect go directly to the first child of the first product-page, instead of going in steps one level at the time?

as simple as that:

<?php $session->redirect($page->child->child->url); ?>

regarding renderNav: how do you call this function? it lists all pages you submit to the function, so the question is what pagearray you submit to it. read something about selectors: https://processwire.com/api/selectors/

you could do something like this:

markupsimplenavigation has some more options like (exclude/include root-page) for building menus: https://processwire.com/talk/topic/1036-markupsimplenavigation/

maybe you can give us a clear site structure like this:

 - home
   |- products
   |  |- apple
   |  |- banana
   |  '- orange
   |- news
   |  |- news1
   |  '- news2
   '- sitemap

or maybe it's already clear for others, so you will get good answeres soon ;)

Link to comment
Share on other sites

Hi Claus,

welcome to PW.
 

Under the Products I have several sub-pages with products, which in turn have sub-pages of their own.


I understand you have a structure like this:

-products
| --subproducts
| --- subproduct 1
| --- subproduct 2
| --- etc.
 

But when I place it in my Products template, and click on ‘Products’ in the main menu, my browser redirects one level at the time, until it gets to the first child of the first product,


Given the structure above this is the intended behavior. It redirects to the first child.
 

but the page is then blank (view source shows that the page is empty). The URL looks correct, and if I turn off the redirect and load the very same URL then the site renderes as expected.


This is indeed weird. I doubt that you load the same page with the same template. Make sure the page you are redirecting to has a template file assigned. Check in admin if there is a "view" button in the page tree for this very page. View it from the admin by using this link. Does it render? If not, something is wrong with the template.
 

Additionally I’d like to ask if there is a way to make a redirect go directly to the first child of the first product-page, instead of going in steps one level at the time?


Honestly I'm not sure how to do that but give this code a try:
 

$session->redirect($page->child->child->url);

But why would you like to do that? You would end up with all requests for /products/ redirected to /products/subproducts/subproduct1/. So, think twice maybe.

Hope this helps.

Link to comment
Share on other sites

Hello guys! Thank you for your replies. To make sure we understand each other, here is the site structure:

- home
   |- products
   |  '- apple
   |  |  |- description page
   |  |  |- features 1
   |  |  |- features 2
   |  |  '- features 3
   |  |
   |  |- banana
   |  |  |- description page
   |  |  |- features 1
   |  |  |- features 2
   |  |  '- features 3
   |  |
   |  '- orange
   |     |- description page
   |     |- features 1
   |     |- features 2
   |     '- features 3
   |  
   |- news
   |  |- news1
   |  '- news2
   |
   |- about
   '- contact

In the design of the site I have columns as menus, where the left column is the menu for the root-level, the next column the menu for the second level, and finally a third column as the menu for the third level. After that I have the main content. The menus two and three are only rendered if needed.

Eg. for the ‘Contact’ page there is only the first column shown, and then the content for the Contact page.

For the ‘News’ page I have the first and the second column menu, and then the content.

For the ‘Product’ page I have all three columns showing, and then the content.

It should look like this, if the user clicks on ‘Products’ in the first column menu:

 Home       *Apple     *Description    *Description for ‘Apple’…
*Products    Banana     Features 1
 News        Orange     Features 2
 About                  Features 3
 Contact

To make this happen I’ve made the following system, starting with the ‘_init.php’:

$menuLevel = count($page->parents);
$homepage = $pages->get('/'); 

Then I’m using the ‘renderNav’ function in the ‘_func.php’

Then I’m doing custom template files for the pages, here is eg. the product template:

// The secondary and tertiary menus
if($menuLevel >= '1') {
	$secondaryNav = renderNav($page->rootParent, 1);
}

if($menuLevel >= '2') {
	$tertiaryNav = renderNav($page->siblings, 1);
}

// Primary content is the page's body copy
$contentThree = $page->body;

And then finally it is all rendered in the ‘_main.php’ where the rendering of the menu-columns are dependant on whether the page template needs them.

<body>
	
	<!-- primary navigation -->
	<div id="primaryNav">
	<ul class='primaryNav'>
		<?php 
		// top navigation consists of homepage and its visible children
		foreach($homepage->and($homepage->children) as $item) {
			if($item->id == $page->rootParent->id) echo "<li class='current'>";
				else echo "<li>";
			echo "<a href='$item->url'>$item->title</a></li>";
		}
		
		// output an "Edit" link if this page happens to be editable by the current user
		if($page->editable()) echo "<li class='edit'><a href='$page->editURL'>Edit</a></li>";
		?>
	</ul>
	
	<form class='search' action='<?php echo $pages->get('template=search')->url; ?>' method='get'>
		<input type='text' name='q' placeholder='Search' value='<?php echo $sanitizer->entities($input->whitelist('q')); ?>' />
		<button type='submit' name='submit'>Search</button>
	</form>
	</div>
	<!-- end primary navigation -->
	
	<!-- content with one level of navigation -->
	<?php if (isset($contentOne)) {
			echo "<div id='contentOne'>";
			echo $contentOne;
			echo "</div>";
		}
	?>
	<!-- end content with one level of navigation -->	
	
	<!-- secondary navigation -->
	<?php if (isset($secondaryNav)) {
			echo "<div id='secondaryNav'>";
			echo $secondaryNav;
			echo "</div>";
		}
	?>
	<!-- end secondary navigation -->

	<!-- content with two levels of navigation -->
	<?php if (isset($contentTwo)) {
			echo "<div id='contentTwo'>";
			echo $contentTwo;
			echo "</div>";
		}
	?>
	<!-- end content with two levels of navigation -->	

	<!-- tertiary navigation -->
	<?php if (isset($tertiaryNav)) {
			echo "<div id='tertiaryNav'>";
			echo $tertiaryNav;
			echo "</div>";
	}
	?>
	<!-- end tertiary navigation -->
	
	<!-- content with three levels of navigation -->
	<?php if (isset($contentThree)) {
			echo "<div id='contentThree'>";
			echo $contentThree;
			echo "</div>";
		}
	?>
	<!-- end content with three levels of navigation -->
	
</body>

(I have to define ‘primaryNav’, ‘secondaryNav’, ‘tertiaryNav’, ‘contentOne’, ‘contentTwo’ and ‘contentThree’ in separate divs because the menu-columns are ‘position: fixed;’)

Now I tried to use the 

$session->redirect($page->child->child->url);

in the products template, but still gives me that blank page, and it still seems to load a page at each step if the path until it gets to the first child. If I have this re-direct enabled and click ‘View’ from the admin, I also get the blank page, so I have a feeling that I’m “sawing over the branch I’m sitting on” somewhere in the chain of templates. I just can’t figure out where I’m going wrong.

Link to comment
Share on other sites

Hi totoff

The purpose of the redirect would be to avoid that users would click on ‘Products’ in the first menu-column, and then see a list of products in the second menu-column but without any content. Without a redirect that would look like so when clicking on ‘Products’:

 Home        Apple     [EMPTY]
*Products    Banana
 News        Orange
 About
 Contact

And then clicking on ‘Apple’ would look like so:

 Home       *Apple      Description    [EMPTY]
*Products    Banana     Features 1
 News        Orange     Features 2
 About                  Features 3
 Contact

The user would have to click through the hierarchy of the primary, secondary, and tertiary menu-columns to finally get to see the content:

 Home       *Apple     *Description    *Description for ‘Apple’…
*Products    Banana     Features 1
 News        Orange     Features 2
 About                  Features 3
 Contact

This is what I want to avoid by having a click on a menu-item in the primary menu, automatically point to the first-found ultimate page of that given tree. 

Link to comment
Share on other sites

Okay, so a bit more fiddling brought me to this solution which feels somewhat ‘hacky’ to me, so do feel free to suggest improvements.

I made special templates for those category-pages that were just placeholders for deeper content and contained no content of their own, eg. ‘Products’ and ‘News’ in the top level, ‘Apple’, ‘Banana’, and ‘Orange’ in the secondary level.

For the category-pages that needed three levels to get to an actual content page I made a template with:

<?php
$session->redirect($page->child->child->url);

… and then assigned ‘Products’ and ‘News’ to this template.

And for the the category-pages that needed two levels to get to an actual content page I made a template with:

<?php
$session->redirect($page->child->url);

… and then assigned ‘Apple’, ‘Banana’, and ‘Orange’ to this template.

This works! Albeit with the annoyance of these redirects loading a page for each ‘step’ down to the final content-page. This is why I call it ‘hacky’.

Link to comment
Share on other sites

I made special templates for those category-pages that were just placeholders for deeper content and contained no content of their own

Back to desktop this is what I would have suggested - a dedicated template for this tasks. Can't think of a better solution, but not an expert myself. However, glad you got it working.

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

×
×
  • Create New...