Using custom route and template to create user profile page

Hi

I am trying to create a custom profile page to display for each my website users.

I am trying to use a route to a custom template, but something seems to be buggy.

My router is:

c::set('routes', array(
  array(
    'pattern' => 'user/(:any)',
    'action'  => function($usid) {
      // do something here when the URL matches the pattern above
    	return tpl::load(kirby()->roots()->templates() . DS . 'user.php', array('usid' => $usid), false);

    }
  )
));

Bu then, within the user.php file, when I try to call the $site object, nothing happens… so this:

<? echo $site->url(); ?>

will not return anything… However, this:

<? echo $usid ?>

does return the correct value passed by the router.

I am doing something wrong here?

The problem is that when you load a template via a route, you have to define your variables in the router and pass them to the template.

$kirby = kirby();
$site = $kirby->site();
return tpl::load(kirby()->roots()->templates() . DS . 'user.php', array('site' => $site, 'usid' => $usid), false);
3 Likes

Great, thanks it works!