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? I'd be quite happy to have the links in a text file with one link per line. -- John
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?
I'd be quite happy to have the links in a text file with one link per line.
1) Create a script that returns a list of links: ## Script (Python) "linkList" return [ 'link1', ... 'linkN' ] 2) Use this script on your page. For example, <tal:block repeat="link here/linkList"> <span tal:replace="structure link" /> </tal:block>
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?
I'd be quite happy to have the links in a text file with one link per line.
1) Create a script that returns a list of links:
## Script (Python) "linkList"
return [ 'link1', ... 'linkN' ]
2) Use this script on your page. For example,
<tal:block repeat="link here/linkList"> <span tal:replace="structure link" /> </tal:block>
Many thanks for your suggestion. Is there a worked example of doing this anywhere? There seem to be quite a few steps involved in trying to get this working and I'm no expert in writing python scripts, and it will probably take me a couple of days to get this working. -- John
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?
I'd be quite happy to have the links in a text file with one link per line.
1) Create a script that returns a list of links:
## Script (Python) "linkList"
return [ 'link1', ... 'linkN' ]
2) Use this script on your page. For example,
<tal:block repeat="link here/linkList"> <span tal:replace="structure link" /> </tal:block>
Many thanks for your suggestion.
Is there a worked example of doing this anywhere?
There seem to be quite a few steps involved in trying to get this working and I'm no expert in writing python scripts, and it will probably take me a couple of days to get this working.
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.
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:
Many thanks for your suggestion.
Is there a worked example of doing this anywhere?
There seem to be quite a few steps involved in trying to get this working and I'm no expert in writing python scripts, and it will probably take me a couple of days to get this working.
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.
Thanks for the suggestion, but I really am a novice when it comes to all this and you are probably presuming more expertise than I have. I created a Page Template exactly as you suggested. It's called test and is in the testsite folder - the contents are pasted from your example. The python script was pasted into a file object called linkList which has a Content Type of text/x-unknown-content-type. When I load the 'test' file and click on the Test tab I get:- Zope Error Zope has encountered an error while publishing this resource. Error Type: AttributeError Error Value: __getitem__ Any ideas on what I have missed out or where to look for a working example? -- John
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:
Many thanks for your suggestion.
Is there a worked example of doing this anywhere?
There seem to be quite a few steps involved in trying to get this working and I'm no expert in writing python scripts, and it will probably take me a couple of days to get this working.
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.
Thanks for the suggestion, but I really am a novice when it comes to all this and you are probably presuming more expertise than I have.
I created a Page Template exactly as you suggested. It's called test and is in the testsite folder - the contents are pasted from your example.
The python script was pasted into a file object called linkList which has a Content Type of text/x-unknown-content-type.
Why did you put it in a File? Why not a "Script (Python)"? --jcc -- "Building Websites with Plone" http://plonebook.packtpub.com/
On Tue, May 31, 2005 at 02:51:18PM -0500, J Cameron Cooper wrote:
John Poltorak wrote:
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 you page is DTML Method, you can do the same using <dtml-in> tag.
Thanks for the suggestion, but I really am a novice when it comes to all this and you are probably presuming more expertise than I have.
I created a Page Template exactly as you suggested. It's called test and is in the testsite folder - the contents are pasted from your example.
The python script was pasted into a file object called linkList which has a Content Type of text/x-unknown-content-type.
Why did you put it in a File? Why not a "Script (Python)"?
I'm using a remote site and only have access via Zope's ZMI. Can I create a script through that?
--jcc
-- "Building Websites with Plone" http://plonebook.packtpub.com/
-- John
John Poltorak wrote:
On Tue, May 31, 2005 at 02:51:18PM -0500, J Cameron Cooper wrote:
John Poltorak wrote:
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 you page is DTML Method, you can do the same using <dtml-in> tag.
Thanks for the suggestion, but I really am a novice when it comes to all this and you are probably presuming more expertise than I have.
I created a Page Template exactly as you suggested. It's called test and is in the testsite folder - the contents are pasted from your example.
The python script was pasted into a file object called linkList which has a Content Type of text/x-unknown-content-type.
Why did you put it in a File? Why not a "Script (Python)"?
I'm using a remote site and only have access via Zope's ZMI. Can I create a script through that?
Certainly. It's in the dropdown. In fact, that's why I used the silly canonical name "Script (Python)" rather than "Python script". --jcc -- "Building Websites with Plone" http://plonebook.packtpub.com/
On Tue, May 31, 2005 at 03:08:36PM -0500, J Cameron Cooper wrote:
John Poltorak wrote:
Why did you put it in a File? Why not a "Script (Python)"?
I'm using a remote site and only have access via Zope's ZMI. Can I create a script through that?
Certainly. It's in the dropdown. In fact, that's why I used the silly canonical name "Script (Python)" rather than "Python script".
Ahhhh... I see now.... Thanks for pointing that out. There are just so many options which appear from that drop down menu and I've only got used to a few of them so far. Looks like I've learned something really worthwhile today!
--jcc -- "Building Websites with Plone" http://plonebook.packtpub.com/
-- John
On 31.Mai 2005 - 20:47:20, John Poltorak wrote:
Thanks for the suggestion, but I really am a novice when it comes to all this and you are probably presuming more expertise than I have.
Just a suggestion: Start reading the Zope Book (the latest version is on http://www.plope.com/Books/2_7Edition), it really getting to know Zope. Andreas -- Cold hands, no gloves.
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 -- John
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:-
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/
On Wed, Jun 01, 2005 at 11:05:04AM -0500, J Cameron Cooper wrote:
John Poltorak wrote:
On Tue, May 31, 2005 at 07:17:46PM +0300, Vital Lobachevsky wrote:
John Poltorak wrote:
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:-
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.
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. I thought there may be something here:- http://www.plope.com/Books/2_7Edition/BasicScripting.stx#1-2 but couldn't find anything which looked relevant.
--jcc
-- "Building Websites with Plone" http://plonebook.packtpub.com/
-- John
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 ------------------------
John Poltorak wrote at 2005-5-31 16:39 +0100:
... Many thanks for your suggestion.
Is there a worked example of doing this anywhere?
The CMF's "ActionsTool" is a prime example for such a link factory. "CMFDefault"s "main_template.pt" demonstrates how to iterate over the links generated by "portal_actions" (the "ActionsTool" instance in a CMF site). -- Dieter
On Tue, May 31, 2005 at 08:54:30PM +0200, Dieter Maurer wrote:
John Poltorak wrote at 2005-5-31 16:39 +0100:
... Many thanks for your suggestion.
Is there a worked example of doing this anywhere?
The CMF's "ActionsTool" is a prime example for such a link factory. "CMFDefault"s "main_template.pt" demonstrates how to iterate over the links generated by "portal_actions" (the "ActionsTool" instance in a CMF site).
Does that mean creating a CMF site? I'm finding it hard enough figuring stuff out without adding CMF into the mix...
-- Dieter
-- John
Hi John,
The CMF's "ActionsTool" is a prime example for such a link factory. "CMFDefault"s "main_template.pt" demonstrates how to iterate over the links generated by "portal_actions" (the "ActionsTool" instance in a CMF site).
Does that mean creating a CMF site?
I'm finding it hard enough figuring stuff out without adding CMF into the mix...
No, you can already do lots of things without CMF and using ZPT and python scripts. You asked for a working example, so, Dieter pointed you out to a feature of CMF that does it. Anyway, for the task you need, I think the example of Vital will do the job. It's up to you wheter or not install CMF or other Content management system. After all everybody has diferent needs. Regards, Josef
Josef Meile wrote:
The CMF's "ActionsTool" is a prime example for such a link factory. "CMFDefault"s "main_template.pt" demonstrates how to iterate over the links generated by "portal_actions" (the "ActionsTool" instance in a CMF site).
Does that mean creating a CMF site?
I'm finding it hard enough figuring stuff out without adding CMF into the mix...
No, you can already do lots of things without CMF and using ZPT and python scripts. You asked for a working example, so, Dieter pointed you out to a feature of CMF that does it. Anyway, for the task you need, I think the example of Vital will do the job.
It's up to you wheter or not install CMF or other Content management system. After all everybody has diferent needs.
Just to be picky, if you install Plone, you're installing most of the CMF functionality. Plone is heavily based on the CMF, at least tool-wise. (Most of the CMF templates, if not all, have gone away by now.) The actions tool, for instance, is almost straight from CMF; Plone over-rides one method. --jcc -- "Building Websites with Plone" http://plonebook.packtpub.com/
participants (7)
-
Andreas Pakulat -
Dieter Maurer -
J Cameron Cooper -
John Poltorak -
Josef Meile -
Nikko Wolf -
Vital Lobachevsky