[Zope] return and render dynamically created dtml from python script

Danny William Adair danny@adair.net
Wed, 19 Sep 2001 02:13:11 -0400


hi!

Yes, I've followed the thread about calling dtml methods, but I couldn't get very much out of it concerning my following problem (maybe I should read it again?). I have a similar problem, but my dtml is created dynamically:

folder "sayhello/":

  dtml method "template":
  -----------------------------------
  <dtml-var standard_html>
  <h3>$GREETING$</h3>
  $INTRODUCTION$
  <dtml-var standard_html_footer>

  python script "personalize" (only parameter: "document")
  -----------------------------------
  from string import replace
  personalized = getattr(context, document).document_src()
  personalized = replace(personalized, '$GREETING$', 'this is the greeting')
  personalized = replace(personalized, '$INTRODUCTION$', 'this is the introduction')
  return personalized

(of course the strings will be replaced by calls like "getGreeting", "getIntroduction" :-) just testing right now)

  dtml method "index_html"
  -----------------------------------
  <dtml-var "personalize('template')">

Yes, I could do the dollar sign stuff with dtml variables, but I don't want the editor to be able to use dtml in general, just a couple of "variables" provided by me. So I invented some names and give him the opportunity to include some simple texts/numbers that might be useful inside his text. (-> he can edit "template")

Of course this returns crap:
The python script takes the raw, unrendered dtml source, replaces a few strings, and returns that unrendered source.

"Dynamically created DTML" means I want to render the document_src *after* replacing.
I understand that I could create an external method contatining something like

from OFS.DocumentTemplate.DT_HTML import HTML

def renderDTML(DTML, namespace=None, **kw):
  ob = HTML(DTML)
  return ob(namespace, kw=kw)

(which I would do if totally necessary), but since I'm using an INSTANCE_HOME, I don't know how do the import correctly, I always get an error when trying to add an external method containing the above, that sits in my "Extensions" directory.