Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template.render() Google App Engine (python)

I'm using the webapp framework from google for this. I'm using template.render() in a get method to render a template for me.

I'm using the following code to do this for me

path = os.path.join(os.path.dirname(__file__), file_name)
self.response.out.write(template.render(path, template_values))

Where file_name is the template to render and template_values is a dict() containing any values to be rendered. What if I don't have any values that I want rendered. Do I just pass in an empty dict() object? It doesn't seem like a great solution to me. Should I be using template.load() instead?

(I can't find the docs for the template class over on google app engine either, hence I'm asking.)

like image 365
Phil Avatar asked Feb 01 '26 00:02

Phil


2 Answers

You can pass an empty dictionary to it and it doesn't mind. You just have to send it something. Your templates just won't display anything.

template_values = {}

path = os.path.join(os.path.dirname(__file__), file_name)
self.response.out.write(template.render(path, template_values))
like image 65
mcotton Avatar answered Feb 02 '26 15:02

mcotton


Okay, thanks for all the answers.

What I've done is:

def render_template(template_name, template_values=dict()):
   path = os.path.join(os.path.dirname(__file__), template_name)
   self.response.out.write(template.render(path, template_values))

Which seems to be the most pythonic solution I could come up with.

like image 43
Phil Avatar answered Feb 02 '26 15:02

Phil