0

I have a Chameleon template stored as a string. I've got it in this form, because I need to do my own processing first. Following that, I want to parse the template, possibly using 'render to response':

render_to_response('templates/foo.pt',
        {'foo':1, 'bar':2},
        request=request)

However, I can't figure out how to parse a template stored in a string, rather than pointing to one in a file. Is this possible?

Liam M
  • 5,306
  • 4
  • 39
  • 55

1 Answers1

1

However, I can't figure out how to parse a template stored in a string, rather than pointing to one in a file. Is this possible?

It seems so, according to the api documentation:

from chameleon import PageTemplate

t = PageTemplate('some string template')
rendered_content = t.render(encoding='utf-8')
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thanks Burhan! That's what I was looking for. I've made a slight change to your answer, adding the required 'request' argument. – Liam M Mar 20 '13 at 10:24
  • Are you sure about `request`? It could be that you need to pass the variables that are in the template, so its not just the actual HTTP request. In the snippet I posted there are no variables to interpolate, hence no other arguments to `render`. If you had `Say hello ${foo}` then you would pass in the substitution for `foo` to `render(encoding='utf-8',foo='bar')`. – Burhan Khalid Mar 20 '13 at 10:30
  • When working with templates in the context of my question (Pyramid), passing the request was necessary. – Liam M Mar 24 '13 at 08:56