Axel Straschil wrote at 2005-9-20 04:07 +0000:
Hi!
Say I have a function that I want to make callable in my UI. It needs to be globally available, i.e. it needs to be callable *without* having an instance of my product yet. (This has to do with listing information about all acquirable instances of the product, so the user can choose from those available.)
In a Product's __init__.py:
def theSecret(self): return "A hobbit!"
methods = { 'whatIsFrodo': theSecret }
Save that i.E. in MyInstance/Products/MyFancy/__init__.py, restart Zope And than <dtml-var whatIsFrodo>.
And if you do not want to pollute the "Folder" (class) namespace, you can define it as a "constructors" (to "registerClass") and use it via "<objectManager>.manage_addProduct[<your product>].constructor(...)"
Very educational, thanks for the replies! I didn't understand Dieter's comment until I tried Axel's suggestion. It apparently works by adding a "theSecret" method to the Folder class and derived objects including the root App object. i.e. you can visit http://localhost:8080/theSecret and (if the method has a docstring) you'll see 'A hobbit!'. I have to say that's pretty darn obscure behavior. It's the antithesis of self-documenting, and I've never seen it used before. Is this documented somewhere? I see one example of it in OFS/tests/testProductInit.py... Also, AFAICT there's no way to declare security on that function. (which is OK in this case since I wanted something 'public' anyway). Anyway, in the end, I followed neither suggestion, since I don't want either to "pollute the Folder (class) namespace", nor do I want to register a constructor that isn't really a constructor ... the former would be obnoxious to users who install my product, the latter seems likely to confuse whoever has to maintain this code after me! Instead, I bit the bullet and rewrote the offending part of the UI as a PageTemplateFile; it wasn't much work after all and I can leave the rest as DTML for now. I like this solution because client code (the template) has to explicitly look up the function via the 'modules' mapping. -PW