Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how write a Dancer2 CGI app #1679

Open
toddr opened this issue Apr 26, 2023 · 3 comments
Open

Document how write a Dancer2 CGI app #1679

toddr opened this issue Apr 26, 2023 · 3 comments

Comments

@toddr
Copy link

toddr commented Apr 26, 2023

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
Copy link
Author

toddr commented Apr 27, 2023

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.

@cromedome
Copy link
Contributor

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

@xsawyerx
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants