[dancer-users] TT 'include file if exists'

Nathan Bailey web at polynate.net
Mon May 1 06:58:09 BST 2017

Hi all,
 More of a Template Toolkit question rather than Dancer per se, but is
there a better way to load a file only if it exists?

>From my Googling around, it appears the only approaches are (a) Try/Catch
in TT or (b) include a full <%PERL%> call (which I'm guessing may be a
little heavy to do on every page load).

I'd like to dynamically include a help file if it exists, otherwise provide
a 'No help available for this page' message. Current code looks like this:
<% help_file = 'help' _ request.request_uri.remove('\?.*') _ '.tt' %>
<% TRY %>
   <% USE File('/full_unix_path/views/' _ help_file) %>
   <% INCLUDE $help_file %>
<% CATCH File %>
   '<% help_file %>' doesn't exist: <% error.info %>
<% END %>

I'm not keen on the fixed path and it seems overkill to have to try/catch
if the file exists rather than just load it (and fail silently) or test if
it exists and then load.

Do people have better patterns for managing inline help?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20170501/ae8bf373/attachment.html>

More information about the dancer-users mailing list