When a $placeholder lookup fails, Cheetah throws a NameMapper.NotFound error. But sometimes it's desirable to provide a default value. For instance, the BatchHandling proposal that is currently being implemented. It needs a CGI parameter 'start', which it sets on subsequent links to itself (= to the current page). But the first time around, the user may have come by typing the URL rather than following a hyperlink, and she may have forgotten (or didn't know about) the required 'start' parameter, which would be 'start=0' the first time around.
Current plan is to use:
$getVar('request.field("start")', 0)
if it works. This is verbose and requires too much quoting. But if =$getVar`` is not usable in this manner, the user will have to resort to:
#try
#set $start = $request.field("start")
#except NameMapper_.NotFound_
#set $start = 0
#end try
which is even less desirable. Compounding the problem is that the exception will not be thrown if an errorCatcher is active; instead it will substitute some HTML which is not numeric and will make the $batch() method barf.
The following solution would be the cleanest:
${request.field("start"), default=0}
The syntax follows precedent. Currently, 'default' would be would be considered a filter argument, to be passed to the #filter. I propose making 'default' a special name that Cheetah would substitute if NameMapper.NotFound is raised, before calling the filter, and then removing 'default' from the filter's argument set.
-- MikeOrr - 13 Nov 2001
$getVar and $varExists are what I hope to stick with. The implementation of the proposal above would probably have a speed impact across all Cheetah $placeholders. If someone wants to go to the effort of making the parser compile different code for this special case, go for it. Not something I'm interested in doing though -- it would probably be a full days work.
-- TavisRudd - 13 Nov 2001
I don't see why it should have any (significant) speed impact. If you change NameMapper.valueFromSearchList to take an optional default=NoDefault argument, then at the last line where it raises NotFound(name), instead test if default is NoDefault, and either return default or raise the error. _namemapper.valueFromSearchList already needs some work to give correct error messages anyway. I expect the parser isn't the hard part of implementing this? -- IanBicking - 13 Nov 2001