Yes.. You need some sort of response class that has a .write method... this can be a hacked up cStringIO or other.
Alternatively you can perform the same thing manually by passing in writer, which instead of the template engine writing ``response.write("%s")`` it could write whatever you want as a callable function. So for example, say you defined your ``CustomResponse`` class as earlier in the list... context['response'] = CustomReponse() exec(parse_template(...)) in context print context['response'].body.getvalue() SO yeah... in the end if we implemented your suggestion of overriding the response class on import, then the render function would work as expected. -- Thadeus On Mon, Jun 6, 2011 at 12:26 PM, Ryan Seto <mr.werew...@gmail.com> wrote: > Hm, that doesn't seem to be it either. I don't get a requirement for > the globals module, but what I get back is this: > > ~~~~~ > >>> print parse_template('view.html', path='templates', > context=dict(content='test')) > response.write(content) > response.write('\r\n', escape=False) > ~~~~~ > > Am I supposed to execute what parse_template returns? > > Thank you for your help. > > On Mon, Jun 6, 2011 at 11:53 AM, Thadeus Burgess <thade...@thadeusb.com> > wrote: > > What you really want is template.parse_template. Still requires > restricted > > but only for the exception raising. > > > > Very simple. > > > > from template import parse_template > > > > print parse_template('main.html', path='/path/to/custom/views/', > > context=dict()) > > > > -- > > Thadeus > > > > > > > > > > On Mon, Jun 6, 2011 at 12:55 AM, Massimo Di Pierro > > <massimo.dipie...@gmail.com> wrote: > >> > >> It is LGPL not GPL. very different. ;-) > >> > >> On Jun 6, 12:36 am, Karel Antonio Verdecia Ortiz <kverde...@uci.cu> > >> wrote: > >> > Hi, > >> > > >> > I've been using the web2py template engine for a while. I don't > remember > >> > if I had to make some change to the template.py module nor the version > >> > of the web2py this module comes from so I attach it in this email. > This > >> > was the way I could make it work (there is probably a simpler one): > >> > > >> > from template import TemplateParser > >> > > >> > context = {} > >> > output = cStringIO.StringIO() > >> > def response_writer(data, escape=False): > >> > output.write(unicode(data)) > >> > context['response_writer'] = response_writer > >> > source = self._template() > >> > exec(str(TemplateParser(source, context=context, > >> > writer='response_writer'))) in context > >> > content = output.getvalue() > >> > > >> > I have a question about this module. It's GPL3 license. If I modify it > >> > an ditribute it in an application, do this application have to be GPL3 > >> > licensed? > >> > > >> > I apologize if my english if very bad. My language is spanish. > >> > > >> > El 06/06/11 00:59, Ryan Seto escribi�: > >> > > >> > > >> > > >> > > >> > > >> > > >> > > >> > > Thanks! That does solve the import restricted dependency. > >> > > >> > > The import globals for the Response() object is still an issue. > >> > > >> > > I tried fiddling with my copy to build a mock Response() object if > we > >> > > can't import globals. > >> > > >> > > This is what I have so far: > >> > > >> > > gluon/template.py | line 867 > >> > > ~~~~~ > >> > > # Here to avoid circular Imports > >> > > try: > >> > > from globals import Response > >> > > except: > >> > > import cStringIO > >> > > from xml.sax.saxutils import escape, quoteattr > >> > > class Response(): > >> > > def __init__(self): > >> > > self.body = cStringIO.StringIO() > >> > > def write(self, data, escape=True): > >> > > if not escape: > >> > > self.body.write(str(data)) > >> > > elif hasattr(data,'xml') and callable(data.xml): > >> > > self.body.write(data.xml()) > >> > > else: > >> > > # otherwise, make it a string > >> > > if not isinstance(data, (str, unicode)): > >> > > data = str(data) > >> > > elif isinstance(data, unicode): > >> > > data = data.encode('utf8', > >> > > 'xmlcharrefreplace') > >> > > self.body.write(data) > >> > > ~~~~~ > >> > > >> > > I was planning to escape the data with the escape and quoteattr > >> > > provided from xml.sax.saxutils, but I wasn't successful with that, > so > >> > > I left it out for now. > >> > > >> > > Here's my code snippet: > >> > > >> > > nixie/util/text.py | line 19 > >> > > ~~~~~ > >> > > import os, subprocess, paths, template > >> > > >> > > def render(inFile): > >> > > content = pandoc(str(inFile)) > >> > > templateFile = os.path.join(paths.get_prog_root(), 'templates', > >> > > 'view.html') > >> > > styles = [] > >> > > styles.append(os.path.join(paths.get_prog_root(), 'css', > >> > > 'style.css')) > >> > > return template.render( > >> > > filename=templateFile, > >> > > context=dict(content=content, styles=styles) > >> > > ) > >> > > ~~~~~ > >> > > >> > > templates/view.html > >> > > ~~~~~ > >> > > <html> > >> > > <head> > >> > > {{for css in styles:}} > >> > > <link rel="stylesheet" href="{{=css}}" type="text/css" /> > >> > > {{pass}} > >> > > </head> > >> > > <body> > >> > > {{=content}} > >> > > </body> > >> > > </html> > >> > > ~~~~~ > >> > > >> > > When I run this, I get an error message that doesn't really help me > >> > > much. Here's the output: > >> > > >> > > ~~~~~ > >> > > C:\projects\nixie>c:\Python26\python.exe Nixie.py README.txt > >> > > Traceback (most recent call last): > >> > > File "C:\projects\nixie\nixie\qt\NixieAccessManager.py", line 41, > >> > > in > >> > > createRequest > >> > > reply = NixieReply(request.url(), self.GetOperation, > parent=self) > >> > > File "C:\projects\nixie\nixie\qt\NixieReply.py", line 30, in > >> > > __init__ > >> > > self.content = text.render(url.toLocalFile()) > >> > > File "C:\projects\nixie\nixie\util\text.py", line 22, in render > >> > > content = pandoc(str(inFile)) > >> > > File "C:\projects\nixie\nixie\util\text.py", line 63, in pandoc > >> > > cwd = cwd > >> > > File "c:\Python26\lib\subprocess.py", line 623, in __init__ > >> > > errread, errwrite) > >> > > File "c:\Python26\lib\subprocess.py", line 833, in _execute_child > >> > > startupinfo) > >> > > WindowsError: [Error 123] The filename, directory name, or volume > >> > > label syntax is incorrect > >> > > ~~~~~ > >> > > >> > > Although it looks like pandoc(str(inFile)) might be the culprit from > >> > > the stack trace, if I just use the output from pandoc(str(inFile)), > >> > > everything works fine, so I doubt that this is the cause. > >> > > >> > > I really appreciate your help. I've started trying Pandoc > >> > > (http://johnmacfarlane.net/pandoc/) instead of the python-markdown > >> > > module, and I noticed that Pandoc comes with it's own template > system. > >> > > So, it may make more sense for me to use Pandoc's templates > instead, > >> > > if I decide to go with it. > >> > > >> > > On Sun, Jun 5, 2011 at 10:45 PM, Massimo Di Pierro > >> > > <massimo.dipie...@gmail.com> wrote: > >> > >> check trunk. I removed it. I am sure we can do better. > >> > > >> > >> On Jun 5, 2011, at 9:26 PM, Ryan Seto wrote: > >> > > >> > >>> Thank you very much for your prompt response. > >> > > >> > >>> It looks like the file gluon/template.py does pull in some extra > >> > >>> dependencies, however. > >> > > >> > >>> It tries to import restricted on line 20 and import globals on > line > >> > >>> 863. > >> > > >> > >>> The restricted module dependency may be easy to remove, since it > >> > >>> appears that it only uses it for raising exceptions. However, it > >> > >>> looks like the Response object is used from the globals module. > >> > > >> > >>> On Sun, Jun 5, 2011 at 9:12 PM, Massimo Di Pierro > >> > >>> <massimo.dipie...@gmail.com> wrote: > >> > > >> > >>>> On Jun 4, 7:58 pm, Ryan Seto<mr.werew...@gmail.com> wrote: > >> > >>>>> I really like how elegant and simple it is to create views in > >> > >>>>> web2py. > >> > >>>>> Would it be possible to use the view/template engine in a > >> > >>>>> standalone > >> > >>>>> application? > >> > >>>> yes. > >> > > >> > >>>> you only need the file gluon/template.py > >> > > >> > >>>> look at the example inside. You only the render function. > >> > > >> > >>>>> I'm writing a desktop application to view formatted text, like > >> > >>>>> markdown, using PyQT's QtWebKit to render the generated html, > and > >> > >>>>> I > >> > >>>>> would like to integrate web2py's method for generating views > into > >> > >>>>> my > >> > >>>>> project. > >> > > >> > >>>>> I've been looking through web2py's source and the mailing list, > >> > >>>>> and it > >> > >>>>> seems that response.render( view_text, dict() ) might be the > >> > >>>>> closest > >> > >>>>> thing to what I'm looking for. However, it looks like there's a > >> > >>>>> lot > >> > >>>>> of dependencies wrapped around it and the objects weren't made > to > >> > >>>>> be > >> > >>>>> used in the context of another application. > >> > > >> > >>>>> If this is the case, would it make sense to compartmentalize the > >> > >>>>> parts > >> > >>>>> for rendering a view into it's own module so they can be used in > a > >> > >>>>> standalone application, similar to the dal? I would be willing > to > >> > >>>>> come up with a patch for this, if I could get some hints on > where > >> > >>>>> to > >> > >>>>> start. > >> > > >> > > >> > > >> > template.py > >> > 30KViewDownload > >> > > >> > kverdecia.vcf > >> > < 1KViewDownload > > >