[Zope] Reading File Contents

Cliff Ford Cliff.Ford at ed.ac.uk
Tue Nov 16 03:08:08 EST 2004


Your question has a small ambiguity - what do you mean by file? From the 
rest of your question I assume you mean an object containing text, where 
the object could be a dtml document. Anyway...

The gurus will say you are mixing logic with content - put the logic in 
a python script. So suppose you have a dtml document containing your 
list you could access it like this (adapted from a script by dlkita on 
ZopeLabs), call it (say) myList:

from Products.PythonScripts.standard import DTML

request = container.REQUEST
RESPONSE =  request.RESPONSE

myString = '<dtml-var file_name>'

# "Coerce" into a DTML-object
myDTML = DTML(myString)

# Return the rendered DTML by *calling* the DTML-object,
# in this case myDTML. context is the namepace - IMPORTANT
theText = myDTML(context, request, RESPONSE)

lines = string.split(theText, '\n')
for line in lines:
     (subject, email) = string.split(line, ',')
     print 'Subject=%s, Email=%s' % (subject, email)

return printed

Clearly you can adapt this to print the drop-down list. Then in your 
form you just plug in the list with a simple statement:

<dtml-var myList>

which keeps your form a lot clearer.

Cliff

Brokken, Allen P. wrote:
> I am working on a very simple "feedback" type form for my site.  I
> realize this can be achieved with the collector or other intermediately
> complex means, but for this site I need something very light weight and
> simple.  All I need is a table of "Subject","Email address" to be read
> from a file rather than hiding the values in the form.  Basically the
> method that renders the form reads the file and picks up the subjects to
> make a drop down, then when submitted it matches the subject to the
> appropriate email address.  If it's not a valid subject it drops the
> request, and only sends email to the right person.  If this were perl on
> a traditional web server I would have done this in like 60 seconds.  As
> it is I can't figure out how to read in the data from the text file so
> it goes into an array properly.  I was hoping it might be as simple as
> doing
> 
>  
> 
> file_name
> 
> subject1,email1 at host.com
> 
> subject2,email2 at host.com
> 
> subject3,email3 at host2.com
> 
>  
> 
> method
> 
>  
> 
> ... Form header and fields
> 
> <select>
> 
>  
> 
> <dtml-call file_name>
> 
> <dtml-in file_name>
> 
>     <dtml-let keytosplit=sequence-item
> 
>          feedback_name="_.string.split(keytosplit, ',')[0]"
> 
>          email_address="_.string.split(keytosplit, ',')[1]">
> 
>          <option value="<dtml-var feedback_name>"><dtml-var
> feedback_name></select>
> 
>   </dtml-let>
> 
> </dtml-in>
> 
>  
> 
> However it treats file_name as a string and won't let me <dtml-in> over
> that.  Is there a way to easily convert the contents of the file to a
> line by line input for a <dtml-in >statement, or some other quick and
> dirty way to do this?  I've come up with some more complex ways with
> adding a header character to each line to split on, with some form of
> loop through it all, but turning the other file into a set for
> <dtml-let> seems to be the most straight forward way to do it.
> 
>  
> 
> I've come up with half a dozen other ways to handle this that include
> MySQL, or other products, but I really would like it to be as simple as
> possible with as few external dependencies as possible.
> 
>  
> 
> ---
> 
> Allen Brokken
> 
> IAT Services - ISAM
> 
> University of Missouri
> 
> brokkena at missouri.edu
> 
>  
> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Zope maillist  -  Zope at zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
>  http://mail.zope.org/mailman/listinfo/zope-announce
>  http://mail.zope.org/mailman/listinfo/zope-dev )


More information about the Zope mailing list