hippo has asked for the wisdom of the Perl Monks concerning the following question:

I've decided to persevere with Mojolicious for a new web application* and am seeking expert advice on using Template as the templating engine. A search has pulled up this blog post which seems promising but (a) it's just a blog post from someone else who was apparently also rather stumbling about just trying to get it to work and (b) is from 12 years ago. So, my question is: is the advice contained in that blog post reflective of good practice for using TT with Mojolicious these days?

Before you ask, the reason to avoid the built-in templating engine is that I have tried to use Mojolicious a few times in the past and each time have run into problems and had to abandon it. Just in case that happens again, I would rather start with my templates in a format which I know I can re-use in another framework.

* Expect a meditation in due course (and probably further SoPWs as things progress :-)


🦛

Replies are listed 'Best First'.
Re: Mojolicious with Template Toolkit
by marto (Cardinal) on Mar 07, 2023 at 11:49 UTC

    While I've not used it Mojolicious::Plugin::TtRenderer seems like it has you covered. I've had no problems using the Mojolicious templates, they've made my life easier.

      Thanks. Following the blog post in the OP, I have used this plugin and it seems to be working well. I do have two minor follow-up questions, though.

      My preference has long been to keep the templates outside the document root and Mojo seems happy with this too. However, I also usually refer to their path(s) relative to the document root (eg. $ENV{DOCUMENT_ROOT}/../templates/foo) so there are no absolute paths running around. But $ENV{DOCUMENT_ROOT} is undefined in my Mojolicious app. The question is therefore how can I refer to the document root dynamically inside Mojolicious?

      Secondly, it seems that TtRenderer expects/insists on the template file extension being .html.tt - is there a way to over-ride this either in TtRenderer or in Mojolicious itself?


      🦛

        Use Mojolicious::Renderer path attribute to manage your target template directories. The second question I don't know, you'd need to look at the code of the module to figure out how to do this.

Re: Mojolicious with Template Toolkit
by 1nickt (Canon) on Mar 07, 2023 at 11:13 UTC

    I use Mojo at work for a collection of APIs so no templating, so I can't say whether the blog post is accurate. However I choose Dancer2 over Mojo whenever I can (including some projects at work), because it's not trying to be a world unto itself, and the Template Toolkit is 100% supported. Try Dancer2?


    The way forward always starts with a minimal test.

      Actually, I'm evaluating a number of different frameworks and attempting to make my code and my templates as agnostic as possible to allow for switching between them, hence the question. If it turns out that Mojolicious is sufficiently difficult to make work with TT (or any other well-supported independent templating system) then that will rule it out as a contender. Only leaving a few hundred others (Dancer2 included) to choose from :-)


      🦛

Re: Mojolicious with Template Toolkit
by cavac (Prior) on Mar 13, 2023 at 15:27 UTC

    I can't help with Mojo, but i do quite a bit of work with ye olde Template Toolkit. Not sure if thats any help to your project, but if there's anything i can help with, feel free to ask.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

      Thanks for the thought but I'm perfectly happy with Template Toolkit - been using it for years. It's the Mojolicious side which is new to me. I have pretty much given up on it for now and am looking at some other frameworks which are more forgiving of the way I would prefer to work.


      🦛