[Zope] Creating links dynamically
Nikko Wolf
nikko-wolf at earthlink.net
Wed Jun 1 15:26:06 EDT 2005
>>Now, if you're uploading files to Zope, rather than the file system,
>>it's a little different, in that you have to get the data from an
>>object, and you'll probably have to split it by newline yourself.
> John Poltorak wrote:
> This is the way I had been thinking of setting it up, but can't find an
> example of opening and reading such an object.
Here's a "Script (Python)" and a ZPT that does what I think you want.
Notes:
- I called the script "parse_file" so if you rename/relocate it
fix the line in the ZPT to correspond.
- I used a "File" in the same folder named "urls" hence I used
"file=context.urls"
- the CLONE thing is a bit confusing, but I needed it for my uses.
Cheers.
---------------- CUT HERE ------------------------
## Script (Python) "parse_file"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=file, sepr='', clone=False
##title=
##
# Returns a list of the lines of FILE; if SEPR if given the lines
# are split into lists.
# Get the "file" as a single string
filetext = str( file )
# Start with nothing:
alist = []
# Throw away CR (if any) then split at the NLs:
for line in filetext.replace('\r','').split('\n'):
if line == "":
continue
#
# Handle one-element-per-line differently:
#
if sepr and sepr in line:
alist.append(line.split(sepr))
elif clone and sepr:
alist.append([line,line])
else:
alist.append(line)
return alist
---------------- CUT HERE ------------------------
<html>
<head>
<title tal:content="template/title">The title</title>
</head>
<body>
<h2><span tal:replace="here/title_or_id">content title or id</span>
<span tal:condition="template/title"
tal:replace="template/title">optional template
title</span></h2>
This is Page Template <em tal:content="template/id">template id</em>.
<form name="aForm" method=GET action="/blah/blah">
<span
tal:define="opts
python:here.parse_file(file=context.urls,sepr=',',clone=1)">
<select name="url">
<tal:block repeat="opt opts">
<option value="optval"
tal:content="python:opt[1]"
tal:attributes="value python:opt[0]">
Option Text</option>
</tal:block>
<input type=submit>
</select>
</span>
</form>
</body>
</html>
---------------------- CUT HERE ------------------------
http://www.google.com/,Google
http://www.yahoo.com/,Yahoo!
http://www.zope.org/
---------------------- CUT HERE ------------------------
More information about the Zope
mailing list