2

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.)

4
  • 1
    Are you sure that template_values isn't optional? I don't have appengine installed on this box, but I thought I recall not needing to provide it. Your question does beg another question, however. If you're not passing in any template values, why are you using a template? Commented Feb 1, 2010 at 20:01
  • @Mike Probably because I'm so new to the GAE. I want (for insance) the url localhost:8080/foobar to render the template for me, I don't want users to navigate to locahost:8080/templates/foobar.html. That way when the users post back to /foobar I'll process their inputs and render the template accordingly. Perhaps I'm barking up the wrong tree? Commented Feb 1, 2010 at 20:05
  • @moderators - is it okay to start a new thread and ask a question that isn't the same but very similar? Commented Feb 1, 2010 at 20:54
  • 1
    If it's a different question, it deserves a new thread. Commented Feb 3, 2010 at 18:26

4 Answers 4

5

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)) 
Sign up to request clarification or add additional context in comments.

1 Comment

Could you replace template_values with {}? I know dict() would work, but I am unable to test the former parameter presently.
4

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.

Comments

1

Since you are rendering a Django template, you need to use render, and you probably can't provide an empty dictionary, as it will complain about not being able to find the variables it expects, unless you enclose each variable reference in an {% if %} block. You should provide a dictionary with all of the keys that the template expects but with empty strings as values.

6 Comments

I don't think I'm explaining this properly, although static files do sound like what I want to do. Basically I have a template foo.html that contain a form. When the form is posted to I want to process the inputs and re-render the page (foo.html) based on those inputs. But when the use just navigates to the form I don't want to render any values into the form. If I just serve the static html file it contains all my {{ }} template tags, I don't want to have to replicate the html file either.
Phil, what you are describing requires that you use template.render with an empty template_values dictionary.
@Adam That's currently what I'm doing (as per my original post).I'm asking is there a better solution than that.
I don't believe that there is.
What I have done for now is put the rendering code into its own method that takes the template file name and the template values as arguments. I've defaulted template_values to =dict() but this is essentially the same solution. I'll need to go back to the drawing board and work out a static solution.
|
1

If you have no template variables to pass in, just pass an empty dictionary. If you do use any variables in the template, they will all evaluate as None.

To make this easier, you could modify your helper code:

def render_template(template_name, template_values = None): if template_values is None: template_values = {} path = os.path.join(os.path.dirname(__file__), template_name) self.response.out.write(template.render(path, template_values)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.