Google Accelerated Mobile Pages, Routes and Template

Hi, I just stumbled over the Google AMP project and wanted to integrate it in Kirby. The implementation as such seems relatively easy (tags, template etc.) but I’m not sure how to best generate it in Kirby.

I guess it could be achieved via routes, so that the head tag required by Google AMP <link rel="amphtml" href="http://site/blog/slug-comes-here/amp"> will be referring to the AMP url and the specific AMP template will be used. I’ve never used routes and wondered how to load a specific template when /amp is added as a suffix to the url. I tried following but it somehow doesn’t work. Any hints appreciated…

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

Something like this should work:

c::set('routes', array(
  array(
    'pattern' => 'blog/(:any)/amp',
    'action'  => function($article) {
      site()->visit('blog/' . $article);
      
      tpl::$data = array_merge(tpl::$data, array(
        'kirby' => kirby(),
        'site'  => site(),
        'pages' => pages(),
        'page'  => page()
      ), $page->templateData());
      
      tpl::load(kirby()->roots()->templates() . DS . 'amp.php');
      return false;
    }
  )
));

I have not tested this code though. So if something doesn’t work, let me know.

1 Like

Hi, thanks for your reply. Just tested it, getting a 500 server error. I added your code to the config.php, the amp.php is also in place, and I have regular access to the blog entries.

Please enable debugging using:

c::set('debug', true);

and check if there is any error message.

Notice: Undefined variable: page in /site/config/config.php on line 101

Fatal error: Call to a member function templateData() on a non-object in /site/config/config.php on line 101

So it obviously refers to $page->templateData());

Try to insert this line after site()->visit():

$page = page('blog/' . $article);

Or:

...
'action'  => function($article) {
      $page = page('blog/' . $article);
      site()->visit($page);
...

Both returns a blank page (no code is generated). :confused:

Oh yes, I seem to have forgotten to copy that line over from my text editor.

Do you get any error message now? Or is it HTTP 200 with a blank page? In that case, verify that your template exists and contains code.

amp.php exists with some dummy content and I’ve got following code set in config.php:

c::set('routes', array(
  array(
    'pattern' => 'blog/(:any)/amp',
    'action'  => function($article) {
      $page = page('blog/' . $article);
      site()->visit($page);
      
      tpl::$data = array_merge(tpl::$data, array(
        'kirby' => kirby(),
        'site'  => site(),
        'pages' => pages(),
        'page'  => page()
      ), $page->templateData());
      
      tpl::load(kirby()->roots()->templates() . DS . 'amp.php');
      return false;
    }
  )
));

I don’t get any error message though, just an empty page with no code.

D’oh, of course we need to output the template :smile::

c::set('routes', array(
  array(
    'pattern' => 'blog/(:any)/amp',
    'action'  => function($article) {
      $page = page('blog/' . $article);
      site()->visit($page);
      
      tpl::$data = array_merge(tpl::$data, array(
        'kirby' => kirby(),
        'site'  => site(),
        'pages' => pages(),
        'page'  => page()
      ), $page->templateData());
      
      echo tpl::load(kirby()->roots()->templates() . DS . 'amp.php');
      return false;
    }
  )
));
3 Likes

Isn’t this usually done by setting the third ($return) variable to false?

Either way is fine. But in this case that’s shorter and easier to understand.

:smile: good old echo… That did the job, thanks a lot!

1 Like

Just a short follow-up question… If I want to define a second route, the previous one doesn’t seem to work any longer. I just wanted to use a second route to get a print version of the particular page (loading print.php). Both versions work like a charm, but not at the same time since I guess the first gets overruled when I define the routes separately, like below. I also tried to pass on the second array into the first but c::set but didn’t succeed. I guess it’s again a tiny detail I’m missing here…

c::set('routes', array(
  array(
    'pattern' => 'blog/(:any)/amp',
    'action'  => function($article) {
Action 1
    }
  )
)); 
c::set('routes', array(
  array(
    'pattern' => '(:all)/print',
    'action'  => function($print) {
Action 2
    }
  )
));

It has to be like this:

c::set('routes', array(
  array(
    'pattern' => 'blog/(:any)/amp',
    'action'  => function($article) {
Action 1
    }
  ),
  array(
    'pattern' => '(:all)/print',
    'action'  => function($print) {
Action 2
    }
  )
));
1 Like

Ahhhh… Ok, got it. Thanks!

dudes theres a lot of last reply, i’m newbie in this, im just finding another light cms, kirby runns very well, and sorry my poor english (i talk spanish), so I’ll be direct, i’ve done all that you have cleared up, i need a cms with amp generator so, it doesnt work :sweat_smile: running kirby 2.5.2 and php 5.6, and cant make that works.
this is the url: http://gaared.com/kirby/

thanks

thanks

one year later, amp still “alive” i haven’t taken super much time reading about amp, is there any sort of unified template which is applied everytime?

do you mind sharing your amp.php file to take a look?

Hello Lukas,

The code works perfectly, but how can i use it for multiple languages?

Cheers, Maarten

Hi,

I also came up with Google AMP recently and saw this article. I understand what is done here with the routing but I don’t know if I get it right but I wanna achieve that on every page the “amp-head” is added automatically. I did not understand how this is able with the above routes. Do I miss something?

For Example I have the following pages:

  • startpage
  • blog
  • products
  • news
  • about us
  • sign up

Now for most of the pages I have the same header snippet. Is it easily possible to change that header snippet when going to /amp instead of / or /blog/amp instead of /blog?

Thx for your support and all the best - Moritz