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

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.


🦛

Replies are listed 'Best First'.
Re^7: Mojolicious with Template Toolkit
by marto (Cardinal) on Mar 08, 2023 at 15:52 UTC

    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.


      🦛

        mojo generate app hippo

        generates:

        .
        ├── hippo.yml
        ├── lib
        │   ├── hippo
        │   │   └── Controller
        │   │       └── Example.pm
        │   └── hippo.pm
        ├── public
        │   └── index.html
        ├── script
        │   └── hippo
        ├── t
        │   └── basic.t
        └── templates
            ├── example
            │   └── welcome.html.ep
            └── layouts
                └── default.html.ep
        
        9 directories, 8 files
        
        

        Tutorial explains defaults for static files, external templates and Home among others.