[Zope] Creating links dynamically

J Cameron Cooper zope-l at jcameroncooper.com
Wed Jun 1 12:05:04 EDT 2005


John Poltorak wrote:
> On Tue, May 31, 2005 at 07:17:46PM +0300, Vital Lobachevsky wrote:
> 
>>John Poltorak wrote:
>>
>>>On Tue, May 31, 2005 at 06:23:11PM +0300, Vital Lobachevsky wrote:
>>>
>>>>John Poltorak wrote:
>>>>>I want to provide a set of links on a Zope site dynamically. ie don't want 
>>>>>to hard code a number of links, but would like to load them from an 
>>>>>external source which I can add to independently, but I have no idea as to 
>>>>>how to set about it. 
>>>>>
>>>>>Any ideas?
> 
> 
>>Well, it's really easy. Create 'linkList' (Python Script) in the folder 
>>where you page lives or somewhere higher in folder hierarchy:
>>
>>## Script (Python) "linkList"
>>return [
>>   ('http://www.google.com/', 'Google'),
>>   ('http://www.yahoo.com/', 'Yahoo'),
>>]
>>
>>If your page is Page Template, it maybe something like this
>>
>><html>
>><body>
>><h1>Search Engines</h1>
>><tal:block repeat="item here/linkList">
>>   <a tal:content="python:item[1]"
>>      tal:attributes="href python:item[0]"></a><br />
>></tal:block>
>></body>
>></html>
>>
>>If you page is DTML Method, you can do the same using <dtml-in> tag.
 >
> How would I extend this so that the Python script could read data from an 
> independently maintained text file which could uploaded periodically?
> 
> ie using a file containing something like:-  
> 
> http://www.google.com/,Google
> http://www.yahoo.com/,Yahoo

To get to the local file system, you must use an external method, which 
works very much like a Python script, but gets its code from a Python 
file on the file system. This is for security. You can read all about 
this in the Zope book.

Your external method should return a list just like the Python script 
above. How you generate it is up to you. For your file format example, 
you could use the Python 'file' object's 'readlines' method and use 
'split' to decode the lines::

   f = open('myfile.links')
   retval = ()
   for line in f.readlines():
     elt = line.split(',')
     retval += (elt,)
   return retval

You might also be able to use the 'csv' module.

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.

		--jcc

-- 
"Building Websites with Plone"
http://plonebook.packtpub.com/


More information about the Zope mailing list