Best solution to display authors profile with Kirby 2.2

Hey there,

in Kirby 2.2 I’m now able to add custom Fields to users blueprints. I’m running my blog on Kirby and have around 5 to 10 editors. Now I can add the twitter account, bio and so on directly to the users own site.

The question is: how can I display the authors profile pages best? Till 2.1 I used a hack that i created a new page with the same username for each author (so that the URI is /authors/schmidtflo). On the article I was able to select the author from the existing users, and I get the connection to the authors profile page with the username. This is now not nessecary anymore.

What I want: I want automatically generated author profile pages (getting the informations from the users account page) and display them with the URI /author/username. How do I do it best?

1 Like

I think the best way to achieve this would be a router that calls a template and passes the user name from the url to the template. Not tested though.

You can, of course, also create real pages automatically by using a hook, but I don’t think this is necessary (and would just mean duplicating content)

Hi @texnixe,

I’m interested in the router solution to show author profiles. Could you put a code example of passing the user name and call the author template?

Thanks

Sure.
The router:

c::set('routes', array(
  array(
      'pattern' => 'authors/(:any)',
      'action'  => function($user) {
        $site = kirby()->site(); 	
        tpl::load(kirby()->roots()->templates() . DS . 'authors.php', array('user' => $user, 'site' => $site), false);
      }
  )
));   

In your authors template:

<?php echo $site->user($user)->lastname() ?>

So if you call a URL http://yourdomain.com/authors/username, the template is called with that username.

1 Like

It works, but when I add the header snippet I get an error:

‘Undefined variable: page…’

Any idea about how to fix it?

What are you doing with the page variable in the header snippet? Depending on that you might be able to inject a page variable (e.g. for home) into the template and snippet.

Isn’t that always going to pass the home page because the authors pages don’t really exist and won’t be found?

@FabianSperrle You are probably right.

@11bits: You would also have to pass the $site variable to the header and footer snippets:

<?php snippet('header', array('site' => $site)) ?>

Maybe you can leave the $page variable alone and just wrap the $page thingy in the header in an if-statement:

<?php if(isset($page)) { echo $page->title()->html(); } ?>

Edit: I deleted the post above because nonsense.

1 Like

Ok, the solution of the conditional to check if $page is set and the site variable works fine.

Now I need to pass the $pages variable for the menu, I tried to do it like the $site but it doesn’t work.

This does not work?

c::set('routes', array(
  array(
      'pattern' => 'authors/(:any)',
      'action'  => function($user) {
        $site = kirby()->site(); 	
        $pages = $site->pages();
        tpl::load(kirby()->roots()->templates() . DS . 'authors.php', array('user' => $user, 'site' => $site, 'pages' => $pages), false);
      }
  )
));

Exactly like @FabianSperrle posted above, plus you need to pass the $pages variable to the menu snippet in the authors.php template as well.

 <?php snippet('menu', array('pages' => $pages)) ?>
1 Like

It works perfectly. Now I can add custom fields for the user and show them in the author template.

Thank you for your help