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

Thanks, but I am already able to set absolute paths OK even without that attribute. The problem is specifying paths relative to the document root.

How can I refer to the document root dynamically inside Mojolicious?


🦛

  • Comment on Re^4: Mojolicious with Template Toolkit

Replies are listed 'Best First'.
Re^5: Mojolicious with Template Toolkit
by bliako (Abbot) on Mar 08, 2023 at 13:39 UTC

    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

      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;