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

As far as I can see from the TT2 docs, there is no native way to do this:

if (template address exists) { include template } else { include other template }

or more succinctly:

[% INCLUDE 'my/best/choice' || 'my/other/option' %]

Which seems a great shame, especially as TT croaks when a template component is missing. It would allow all sorts of useful structures, most obviously the chance to default to a generic component if no more specific version is available.

It's quite easy to implement from the outside, provided you still have the template path:

sub template_exists { my ($self, $file) = @_; my $path = $self->config->template_path; return 1 if grep { -e && -f && -r } map { "$_/$file" } @$path; return; }

But it isn't easy to make that look friendly in the templates, which I don't like (and which generates support work, which I also don't like :)

[% FOREACH thing = collection %] [% template = factory.template_exists("list/types/${thing.type}.ht +ml") ? "list/types/${thing.type}.html" : "list/types/default.html" %] [% INCLUDE $template %] [% END %]

So I mostly confine this to the darker corners of the template hierarchy. I'd like to use it more widely, and I can't be alone in that, which makes me think there's an odd bit of syntax or a clever trick that I've missed, and everything could look nice again. Can anyone advise, please, or suggest another approach?

Thanks,
Will

Replies are listed 'Best First'.
•Re: testing for the existence of a tt template: better way?
by merlyn (Sage) on Jan 14, 2004 at 04:23 UTC

      Thank you. That is much neater, but it's not quite the same thing: It tests that the template exists *and works*. Which would be useful in a public-facing system, but a bit of a drawback in development, where it would obscure (and worse, silence) an error of any kind that happened behind the TRY statement.

      I'd also be mildly concerned about using an error mechanism to handle an expected event: apart from the minor misapplication, I would guess that it's a lot more economical to call -e -f a few times than to go through the whole Template exception-handling mechanism and then discard its output. A minor difference, probably, but if the try and catch was in a list-display loop I could easily be causing and intercepting twenty or fifty deaths per page...

      So I guess I am going to have to stumble around inside the template parser and attempt to extend its syntax. I'm sure it will be very good for me.

      update Actually, maybe all that's needed is a configuration option that if set, causes the compiler to return false rather than raising an exception when a template file can't be found. It should be easier than going into the parser, anyway. Shudder.

      LOL! I posted almost the exact same thing, one minute after you.

      I guess they don't say "Merlyn: The fastest gun in the west." for nothing. :)

Re: testing for the existence of a tt template: better way?
by Dylan (Monk) on Jan 14, 2004 at 04:24 UTC

    Why not just use TRY ... CATCH?

    [% TRY; PROCESS foo; CATCH file; PROCESS bar; END; %]

    seems to work for me. :)

Re: testing for the existence of a tt template: better way?
by dragonchild (Archbishop) on Jan 14, 2004 at 03:29 UTC
    Why don't you just create an [% EXISTS foo %] directive and upload a patch?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.