[Zope] default dtml attrs, exception handling
amos
amos@digicool.com
Mon, 22 Feb 1999 11:49:50 -0500
Quinn Dunkan wrote:
> Is there a way to specify default dtml tag arguments? I have a bit of dtml
> that renders a database entry. Currently I fill null values from python, but
> I'd like to be able to specify them from dtml. Unfortunately that seems to
> mean putting a null= tag on every single #var, and changing them all when I
> change one. Is there a way you could put something like:
>
> <!--#defaults -->
> <!--#var null="(not given)" -->
> <!--#/defaults -->
>
> at the top of your document to accomplish that? Or is there some other way
> I'm missing?
There are a couple ways. If you are using DTML from Python, you can
specify a defaults in the DocumentTemplate constructor as keyword
arguments.
t=DocumentTemplate.HTML("""<!--#var foo-->""",foo="bar")
I believe that this is in the DTML User's Guide. If it isn't, it should
be.
Using the Zope framework, probably the easiest thing to do is to set
default values as properties on Folders or Documents.
> A suggestion for exception handling:
> Currently, it appears that exceptions are caught and handled by a Response
> object. It has hard-coded responses to exceptions, but you can make your own
> error pages by making your own exception with a __str__ method that returns
> text that contains "<html>". As far as I can tell, though, there is no way to
> replace the generic exception handler (so that if os.stat throws os.error (or
> OSError, they changed it in 1.5.2b1 (stop it, guys!)) you get something of
> your choosing rather than "Sorry, an error occurred." Also, there is no way
> to decide per-exception how to handle tracebacks. Even if you just want to
> raise a "invalid search string" FormError exception, you still spew a
> meaningless traceback at the user. It can be hidden if you unset Z_DEBUG_MODE
> or __bobo_hide_tracebacks__ = 1, but then what if you want "real" errors to
> spew a traceback?
>
> Currently, I do stuff like:
>
> error_doc = HTMLFile('error.dtml')
>
> class BoboError(Exception):
> title = "Generic Error"
> def __init__(self, msg):
> self.msg = msg
> def __str__(self):
> return error_doc(self)
>
> and then subclass BoboError. Unfortunately, I can't figure out how to hide
> tracebacks...
>
> I also have two questions re dtml: From my reading of
> DT_String.FileMixin.read_raw, the source file is reread every time the dtml is
> cook()ed, which is only the first time it is __called__. There is some
> mucking around with a __changed__ method, which I assume is an unimplemented
> attempt to not reread the file if its mtime is the same as before.
__changed__ is part of the BoboPOS persistence machinery.
> Is
> __changed__ supposed to del self.cooked? What's going on here?
> Rereading dtmls is desirable so I don't have to restart pcgis just because the
> dtml changed...
I'm not up on the latest details, but Jim tells me that if
ZOPE_DEBUG_MODE is set, then Documents should re-read their source
files... But I don't think this answers your question.
> The second question:
> I've seen references to the ability to create your own dtml tags, but no
> mention in the DTML docs on how to do that. Is this possible?
Yes, but its not currently documented. Andrew K. made a stab at
documentation on his starship Zope Hacks page.
> If so, where
> can I read about it? I'd actually like to create my own html tags, so I can
> use DT as a generic html/whatever generator, at least until I can do it in
> XML. Does this seem reasonable?
Yes, DTML can generate any kind of text you want. In fact, there is no
need to hack DTML to allow you to do this.
-Amos