<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>Hi Nathan,</p>
<p>One way to accomplish what you want is to write your own
Template::Provider subclass. You'll need to override at least the
_template_content($path) method and there you can implement the
logic you need, like returning a (pre defined) content if $path
does not exist. <br>
</p>
<p><tt>my $provider = My::TT::Provider->new();</tt><tt><br>
</tt><tt>my $tt = Template->new({</tt><tt><br>
</tt><tt> ENCODING => "utf8",</tt><tt><br>
</tt><tt> LOAD_TEMPLATES => [ $provider ],</tt><tt><br>
</tt><tt>});<br>
$tt->process( ... );<br>
</tt></p>
See:
<a class="moz-txt-link-freetext" href="http://www.template-toolkit.org/docs/modules/Template/Provider.html#section_SUBCLASSING">http://www.template-toolkit.org/docs/modules/Template/Provider.html#section_SUBCLASSING</a>
<p>Hope this helps!</p>
<p>Best regards,<br>
Lennart<br>
</p>
<p><br>
</p>
<br>
<div class="moz-cite-prefix">On 01-05-17 07:58, Nathan Bailey wrote:<br>
</div>
<blockquote
cite="mid:CAKwoNTurKbOkRsucWj358f6bybib8Dk9LO14oAVay9zK4f8o8g@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>Hi all,</div>
<div> 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?</div>
<div><br>
</div>
<div>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).</div>
<div><br>
</div>
<div>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:</div>
<div><font face="monospace, monospace"><% help_file = 'help'
_ request.request_uri.remove('\?.*') _ '.tt' %></font></div>
<div><font face="monospace, monospace"><% TRY %></font></div>
<div><font face="monospace, monospace"> <% USE
File('/full_unix_path/views/' _ help_file) %></font></div>
<div><font face="monospace, monospace"> <% INCLUDE
$help_file %></font></div>
<div><font face="monospace, monospace"><% CATCH File %></font></div>
<div><font face="monospace, monospace"> '<% help_file
%>' doesn't exist: <% <a moz-do-not-send="true"
href="http://error.info">error.info</a> %></font></div>
<div><font face="monospace, monospace"><% END %></font></div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>Do people have better patterns for managing inline help?</div>
<div><br>
</div>
<div>thanks,</div>
<div>Nathan</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
dancer-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:dancer-users@dancer.pm">dancer-users@dancer.pm</a>
<a class="moz-txt-link-freetext" href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users</a>
</pre>
</blockquote>
<br>
</body>
</html>