Dancer2 icon indicating copy to clipboard operation
Dancer2 copied to clipboard

Document how write a Dancer2 CGI app

Open toddr opened this issue 2 years ago • 3 comments
trafficstars

I worked with @xsawyerx today and we figured out how to write a Dancer2 app in CGI. We feel like it should be documented but we're not sure where:

use Dancer2;
use Plack::Loader;
use Template::Provider::FromDATA;

# Tell Dancer to use the DATA section for templates                                                                                                                                                                                                                                                                                                                                                     
my $provider = Template::Provider::FromDATA->new( { CLASSES => __PACKAGE__ } );
set engines => {template => { template_toolkit => { LOAD_TEMPLATES => [ $provider ] }}};
set template => 'template_toolkit';

get '/' => sub {
    template 'mytemplate' => { bar => "foo" };
};
Plack::Loader->auto->run(main->to_app);

__DATA__                                                                                                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                        
__mytemplate.tt__                                                                                                                                                                                                                                                                                                                                                                                       
Flib [% bar %]                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                        
__anothertemplate.tt__                                                                                                                                                                                                                                                                                                                                                                                  
Something else here.

toddr avatar Apr 26 '23 23:04 toddr

Technically the only code needed for a CGI script would be:

use Dancer2;
use Plack::Loader;

get '/' => sub { "Hello World!" };
Plack::Loader->auto->run(main->to_app);

The rest is how to embed templates in the DATA block.

toddr avatar Apr 27 '23 04:04 toddr

I would think either a new example app or the cookbook would make for good places to put this. I am open to suggestions!

cromedome avatar Jul 25 '23 01:07 cromedome

I think it would work well in a Cookbook. @toddr interested in picking this up?

xsawyerx avatar Dec 14 '23 16:12 xsawyerx