Jump to content

Load page via AJAX and redirect to parent


Recommended Posts

Hello, a page with url "site/teachers" shows the layout:

| main section | aside |

"main" shows a list of teachers, and when clicked in one teacher its info is loaded in "aside" via ajax.

Now I want to use URL segments in page "teachers" to display a teacher info in "aside" when URL = "site/teachers/teacher1"

But when url "site/teachers/teacher1" is entered browser gets directed to "teacher1" page itself (and causes a mess because is loaded in main not aside...) instead of "teachers" page plus url segment and teacher info in aside.

In page "teachers" i use the code:

foreach ($page->children as $c) {
	if($input->urlSegment1 == '{$c->name}') {
		$page->aside .= "... page content...";

First time using url segments, so I think i dont understand how to make it work properly. I hope I explained the problem more or less understandable.

Thanks in advance, Alejandro.

Link to comment
Share on other sites

Existing pages always take precedence over urlSegments. You have several options:

  • Use urlSegments that don’t exist as children of /site/teachers/, for example by adding a string, or by using 2 urlSegments. E.g. /site/teachers/selection/teacher1/.
  • Use GET parameters instead: /site/teachers/?teacher=teacher1. You could either process the parameter with PHP or catch it with JavaScript and use your existing AJAX solution. Which would probably be the simplest option. Just add one or two lines of JS.
  • Or, if you don't want the actual page under /site/teachers/teacher1/ to be reachable at all, just display /site/teachers/ in that template. Example below:

template of /site/teachers/teacher1/:

$options['teacher'] = $page; //pass the teacher to the parent page using options array

echo $page->parent->render($options); //render the parent page (/site/teachers/)

template of /site/teachers/:

if(isset($options['teacher'] && $options['teacher'] instanceof Page) {
    $aside_teacher = $options['teacher'];

//Now render the page as you normally would, and you can use $aside_teacher to put its contents where you need them

This also has the added bonus that you can use standard page caching for the child pages.

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...