[ZDP] BackTalk to Document The Zope Book/Advanced Zope Scripting

nobody@nowhere.com nobody@nowhere.com
Tue, 09 Apr 2002 15:26:36 -0400

A comment to the paragraph below was recently added via http://www.zope.org/Documentation/ZopeBook/ScriptingZope.stx#3-68


      One common use for scripts is to do string processing. Python has a
      number of standard modules for string processing. You cannot do regular
      expression processing from Python-based Scripts, but you do have access
      to the *string* module. You have access to the *string* module from
      DTML as well, but it is much easier to use from Python. Suppose you
      want to change all the occurrences of a given word in a DTML
      Document. Here's a script, *replaceWord*, that accepts two arguments,
      *word* and *replacement*.  This will change all the occurrences of a
      given word in a DTML Document::

        ## Script (Python) "replaceWord"
        ##parameters=word, replacement
        Replaces all the occurrences of a word with a
        replacement word in the source text of a DTML
        Document. Call this script on a DTML Document to use

        Note: you'll need permission to edit a document to
        call this script on the document.
        import string
        text=string.replace(text, word, replacement)
        context.manage_edit(text, context.title)

        % Anonymous User - Apr. 9, 2002 3:26 pm:
         Why no regular expressions? Perhaps this should be explained in greater detail. One can always use javascript
         if one needs regular expressions, but this is something that was quite surprising to me.