[Zope] ZPT reading a file
Jay Dorsey
jay at jaydorsey.com
Fri Nov 28 20:56:30 EST 2003
On Fri, Nov 28, 2003 at 06:35:33PM -0600, J Cameron Cooper wrote:
> Jay Dorsey wrote:
>
> >This is probably a simple task, but its giving me a heck of a time.
> >
> >I've got a folder with some files in it. I'm using a PageTemplate to
> >loop through the folder and pull the attributes of the templates out.
> >I've got something like this:
> >
> ><div tal:repeat="item sorted_objects">
> > <div tal:content="item/title">title here</div>
> > <div tal:content="item/title">author here</div>
> > <div tal:content="structure item">content here</div>
> ></div>
> >
> >The PageTemplates originally contained HTML, but I'm not so
> >stuck on it that I can't use Structured Text instead (it doesn't
> >seem to me that there is an html option for tal:content tags).
> >Regardless of whether I use "structure" or "text", the content
> >comes back as text when I view the page.
> >
> You're all over the map terminologically, so I'm not sure that I can
> tell what you actually mean. Still:
I'm not sure what I actually mean either so we're even ;-)
>
> I'm assuming that 'sorted_objects' is defined previously by something
> like a sequence.sort() on the 'objectValues' of your Folder.
Correct. I've got something similar to this wrapped around the div
listed earlier:
<span
tal:define="objects container/blog_entries/objectValues;
sort_on python:(('date', 'cmp', 'desc'),
('title', 'nocase', 'desc'));
sorted_objects python:sequence.sort(objects, sort_on)">
I picked it up from somewhere on zope.org or zopelabs.com.
>
> I did a test of this with a folder 'ddd' containing some PageTemplates
> and Files containing fragmentary HTML. This worked fine::
>
> <div tal:repeat="item here/ddd/objectValues">
> <div tal:content="item/id">id here</div>
> <div tal:content="item/title">title here</div>
> <div tal:content="structure item">content here</div>
> </div>
>
> Notice that it's almost exactly what you wrote. The contents of the
> 'item' objects are output exactly as contained, which means the HTML
> renders. Remove the 'structure' keyword and the HTML is escaped and
> viewable on the page. (You are aware of the exact function of
> 'structure' right?) Now, if you want to render Structured Text (as
> contained in a File) you have to say something like::
>
> <div tal:define="pss modules/Products/PythonScripts/standard"
> tal:content="structure python:pss.structured_text(item)">
I gave the above a shot, I got either a blank page (when the
content was structure), or escaped HTML (when the content isn't
specified) which shows the item type and name (i.e.
<ZopePageTemplate at 2003_11_17_06_56>). The entire code is:
<span tal:define="objects container/blog_entries/objectValues;
sort_on python:(('date', 'cmp', 'desc'),
('title', 'nocase', 'desc'));
sorted_objects python:sequence.sort(objects, sort_on)">
<div
tal:repeat="item sorted_objects"
style="border:1px dashed #C0C0C0;padding:5px;margin:5px;"
tal:attributes="id item/id">
<h3 tal:content="item/title" style="margin-top:0px;margin-bottom:0px;">title</h3>
<div style="font-size:xx-small;margin-left:10px;margin-bottom:10px;">posted by <a tal:content="item/author">author</a> on <a tal:content="item/date">date</a></div>
<div
tal:define="pss modules/Products/PythonScripts/standard"
tal:content="structure python:pss.structured_text(item)">content goes here</div>
</div>
</span>
The content-type on the page templates was text/html--I tried changing this to text/plain
but get a parser error when I do so. I fooled around a bit and tried using HTML inside
the templates with content-type text/html (default), and using "structure item" on my
tal:content, which seemed to work. I assume I misunderstood the use of
structure--it should be used for HTML or structured text?
>
> >I'm assuming this is because I'm accessing the item incorrectly.
> >
> >What would be the easiest way to get the content out of each item
> >and use either structured text or just keep the HTML as well?
> >
> >Zope is just killing me right now--if this was plain ol' python I
> >would have a look at dir(item) to see if there was a special
> >attribute or method I needed. Thankfully I'm just learning zope on
> >my own time, else I'd feel horribly useless instead of only
> >terribly so ;-)
> >
> DocFinder (Or DocFinderEverywhere) may be of help here. You can, by the
> way, get a Python console into a live Zope. If you get 'zctl' or Zope
> 2.7 (which comes with 'zopectl' which is pretty much the same) you can
> just pass that script a 'debug' and off you go.
>
Sounds easy enough and I may look into it. I've got a starter account on
zettai.net which doesn't give me complete access to everything (or to
install my own products/programs--though zettai.net does accept requests to
install items).
> >The documentation seems lacking on the website, and on the net in
> >general, as far as basic "heres how you do X" goes. Zopelabs is
> >good, and one or two other sites I've found, but I know this
> >has to be a really easy task. I appreciate any assistance.
> >
> I suspect that this is well covered and you're just doing something
> trivially wrong or misunderstanding something (perhaps the way
> 'structure' works). But in any case, one can't cover everything you
> might try to do. That's an awfully big space, even among "simple" tasks.
>
If I understand how structure works properly, the text in the included file
(in my example) was written using structured text, so when its output it should
convert the text to HTML (**this** becomes <b>this</b>). I didn't realize that
it would apply to HTML as well.
I appreciate the assistance. :)
--
Jay Dorsey
jay at jaydorsey dot com
More information about the Zope
mailing list