in reply to Re^4: Mojolicious with Template Toolkit
in thread Mojolicious with Template Toolkit

Mojolicious offers Helpers of Mojolicious::Guides::Tutorial which are functions which you setup during startup. I am not sure if this is a good way, but I would do something like:

use Mojo::Base 'Mojolicious'; # This method will run once at server start sub startup { my $self = $_[0]; $self->app->config( hypnotoad => { listen => ['http://'.$SERVER_ADDRESS.':'.$SERVER_PORT] } ); my $DOCUMENT_ROOT = '...'; $self->helper(document_root => sub { $DOCUMENT_ROOT }); # I also add a logger object my $logger = Mojo::Log->new; $self->helper(applog => sub { $logger }); ... }

And at any route endpoint (in its own separate file):

use Mojo::Base 'Mojolicious::Controller'; sub logout { my $c = $_[0]; my $log = $c->applog; my $dr = $c->document_root; ... $c->render(...) return 1; }

There is also Mojolicious::Plugin::DefaultHelpers with some helpers before reinventing the wheel

FWIW, the template system I use is Text::Xslate via MojoX::Renderer::Xslate. It's surely idiosyncratic and the language associated with it is aptly named Kolon! I *think* I heard choroba saying that it was quite fast when I was looking around. And I use it for 3 years now, quite happy and with colon painless.

bw, bliako

Replies are listed 'Best First'.
Re^6: Mojolicious with Template Toolkit
by hippo (Archbishop) on Mar 08, 2023 at 14:42 UTC
    my $DOCUMENT_ROOT = '...';

    This line is precisely the problem. In other frameworks I do not need to set the document root explicitly by hand inside the application. The webserver does this and the application just picks it up through $ENV{DOCUMENT_ROOT}. The document root will necessarily vary depending upon how and where the app is deployed (eg. dev vs production) so I don't want anything inside the app itself to explicitly specify a document root with an absolute path.

    I'm getting the impression that Mojolicious is unable to tell where its own document root is, which seems odd. Hopefully that's just a misconception on my part. The closest thing so far which works is this:

    use Cwd; $ENV{DOCUMENT_ROOT} //= getcwd . '/..'; plugin 'tt_renderer' => { template_options => { INCLUDE_PATH => [ "$ENV{DOCUMENT_ROOT}/../templates/lib", ... ], ENCODING => 'utf8', } };

    But it's far from elegant and relies on the application being one level below the docroot.

    Update: Actually, thinking further there isn't really any need for the app to be under the docroot at all. I suppose it could be placed outside the docroot in a fixed position with respect to the templates dir and could load it from there, so INCLUDE_PATH would just be ../templates/lib for instance. That seems a better compromise and will also work for @INC.


    🦛

      Can you show the directory hierarchy you have setup? The output of tree perhaps. Also, see Mojo::Home:

      use Mojo::Home; my $home = Mojo::Home->new; print $home;

        Originally I had this (abridged):

        .
        ├── html
        │   ├── pix
        │   │   ├── cc2.png
        │   │   ├── dilad.png
        │   │   ├── favicon.png
        │   │   └── wallpaper_1080x2400.png
        │   ├── app
        │   │   └── mojo2.fcgi
        │   └── robots.txt
        ├── lib
        │   └── MyApp.pm
        ├── t
        └── templates
            ├── MyApp
            │   ├── home.html.tt -> home.tt2
            │   ├── home.tt2
            └── lib
                ├── footer.tt2
                ├── form.tt2
                └── header.tt2
        

        but now I've moved the application mojo2.fcgi up so that it's like this:

        .
        ├── fcgi
        │   └── mojo2.fcgi
        ├── html
        │   ├── pix
        │   │   ├── cc2.png
        │   │   ├── dilad.png
        │   │   ├── favicon.png
        │   │   └── wallpaper_1080x2400.png
        │   └── robots.txt
        ├── lib
        │   └── MyApp.pm
        ├── t
        └── templates
            ├── MyApp
            │   ├── home.html.tt -> home.tt2
            │   ├── home.tt2
            └── lib
                ├── footer.tt2
                ├── form.tt2
                └── header.tt2
        

        and this allows me to use the relative paths ../templates/lib etc in the plugin.

        Mojo::Home looks like it might be useful for other things, however, so thanks for the pointer. There's clearly a lot of domain knowledge here which I don't have - and that isn't unique to this framework by a long way.


        🦛