26 November 2011
I'm writing some toy once again with Plack and Dancer (and Mojo later).

this time, I'm playing Plack::Middleware::OAuth and Dancer::Template::Xslate a bit. the website is http://kindergarden.fayland.org/ and I'll open source it if someone is interested. for now, it's just 'Login with ...' OAuth and nothing else. (layout is built with twitter bootstrap)

the Dancer::Template::Xslate has some bugs and I tried to submit few commits through github to fix it. (at least it's working for me now)

here is a tip to add function like gravatar into Xslate within Dancer.

problem as Text::Xslate supports function param when ->new but Dancer YAML config can't have Perl code inside. and it's very tricky or hard to fix the engine 'template' b/c we can never modify it. it has 'my $_engine;' inside code and you can't modify it at all.

after a while, I find a good solution with the module param of the Text::Xslate. it's very neat. sample code as below:

# config.yml
template: xslate
engines:
  xslate:
    syntax: 'TTerse'
    extension: 'tt'
    header:
      - 'layout/header.tt'
    footer:
      - 'layout/footer.tt'
    module:
      - KinderGardenX::Text::Xslate::Bridge::KinderGarden

# KinderGardenX::Text::Xslate::Bridge::KinderGarden
package KinderGardenX::Text::Xslate::Bridge::KinderGarden;

use strict;
use warnings;
use parent qw(Text::Xslate::Bridge);

use Gravatar::URL;

my %funtion_methods = (
    gravatar_url => \&gravatar_url,
);

__PACKAGE__->bridge(
    function => \%funtion_methods,
);

1;

# template

    <img src="[% gravatar_url( email => user.email, size => 30) %]" />
    <img src="[% gravatar_url( email => user.email, size => 50) %]" />
    <img src="[% gravatar_url( email => user.email) %]" />

Note there is always more than one way to do it. 

Thanks.


blog comments powered by Disqus