in reply to testing for the existence of a tt template: better way?

Just trap the exception:
[% TRY; INCLUDE "/file/one"; CATCH file; INCLUDE "/file/two"; END; %]

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: testing for the existence of a tt template: better way?
by thpfft (Chaplain) on Jan 14, 2004 at 16:39 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.

Off-topic...
by Dylan (Monk) on Jan 14, 2004 at 04:31 UTC

    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. :)