[Zope] HowTo Render txt-Files with LocalFS in PythonScript?

Max M maxm@mxm.dk
Mon, 05 Aug 2002 13:59:13 +0200


>
>
>>Subject: [Zope] HowTo Render txt-Files with LocalFS in PythonScript?
>>
>>the content is returned but without rendering (as expected).
>>Does anybody know how to get the rendering done?
>>    
>>
in globals which import HTMLFile you can also import HTML which takes a string as an argument. It can then render the text as if it was a normal HTMLFile.

like::

    # the normal way
    index_html = HTMLFile('path/to/file', globals())

    # the other way
    aText = open('path/to/file', 'r').read()
    index_html = HTML(aText)


I hope this is of some help.

if you want to render an arbitrary textfile selected dynamically, this would probably work (untested):

    def fileRender(self, filename):
        aText = open(filename', 'r').read() # get the text
        theDtmlFile = HTML(aText) # create the page
        return theDtmlFile(self, self.REQUEST) # render it


But my guess is that it can be pretty expensive to create a "HTML" object as the dtml will probably be parsed every time. So perhaps you should make some kind of dict to put the pages into. Like::

    class HtmlTxtFile:

        def updateFiles(self):
            pages = {}
            for file in textFiles:
                f = open(file, 'r')
                aText = f.read()
                key = file[file.rfind('/')+1:-len('.txt')]
                pages[key] = HTML(aText)
                f.close()
            self._pages = pages

        def __getattr__(self, attr):
            return self._pages.get(attr)(self, self.REQUEST)


regards max M

-- 


"Sorry I would Really Like To Help More On This Project,
But Am To Busy Doing Paid Work On A Tight Deadline"
    Max M