views:

212

answers:

2

I'm using google appengine (python, of course :) ) and I'd like to do a string.replace on a string from the template file.

{% for item in items %}
  <p>{{ item.code.replace( '_', ' ' ) }}</p>
{% endfor %}

But that isn't working. So we cannot execute anything other than basic checks in the app engine templates. Is that Correct ?


Another related problem is I'm trying to shorten a string and make it available to the template.

Each furniture object has a name and a longer description field. In this view I'm rendering, I want only the first 50 characters of the description field.

So I tried something like

items = db.GqlQuery( 'select * from furniture' )

# edit:  if you change the above line to
# items = db.GqlQuery( 'select * from furniture' ).fetch( 1000 )
# the .fetch() command makes the addition of dynamic properties work!

for item in items :
  item.shortdescr = item.description[ 0:50 ]

# pass data off to template for rendering
self.response.out.write(
  template.render( 'furnitureAll.html', { 'items' : items } )
)

Template goes


{% for item in items %}
  <p>{{ item.name }}</p>
  <p>{{ item.shortdescr }}</p>
  <!-- items.shortdescr does not exist here,
  probably because I did not .put() it previously. -->
{% endfor %}

Since that didn't work, I tried changing the Gql Query to shorten the string instead. But I'm quickly realizing Gql isn't like SQL. I'm trying to write queries like

select name,LEFT( description, 50 ) from furniture

With little success

+1  A: 

Apart from the argument-less .fetch() call in your code, which I believe can't possibly work (you ALWAYS have to pass fetch an argument -- the max number of entities you're willing to fetch!), I can't reproduce your problem -- assigning a new attribute (including one obtained by processing existing ones) to each item just works fine in my tests.

Can you please reproduce your observed problem in as tiny as compass as possible and edit your question to include all relevant files pls? Seems to be the only way we could help you with your weird observed bug!

BTW, select name,LEFT( description, 50 ) or whatever OF COURSE won't work in GQL -- GQL, very very explicitly, ONLY supports select * to get whole entities, or select __key__ to get just the entities' keys -- that's all; NO selectivity of columns in the select, much less any OPERATIONS on them!-)

Alex Martelli
guilty. I added .fetch() w/o testing b/c it wasn't there before - works without it but I thought i should add it in the "official" version of the question.
bobobobo
Oh wow! adding .fetch( 100 ) makes the addition of dynamic fields work!
bobobobo
Without the fetch you don't (yet) have a list, you have a query (iterable, but not a list). But `fetch` demands an argument (max numbers of items to fetch) to return a list.
Alex Martelli
+1  A: 

I have little experience with Google AppEngine, but my understanding is that it is very closely related to Django. Your templates do not actually contain Python code, even if some of the structures you use in them look like it.

Both of your questions should be solved using template filters. If it was Django, I would use something like this for your second question:

{{ item.description|truncatewords:10 }}

For your first question (string replace), there may be no built-in filter you can use for that. You will need to write your own. Something like this;

from google.appengine.ext.webapp.template import create_template_register

register = create_template_register()

@register.filter
def replace_underscores(strng):
    return strng.replace('_', ' ')

Then, in your template, you can do this:

{{ item.code|replace_underscores }}
Ian Clelland
Thanks dude. ftr it seems there should be a colon there between truncatewords:arg ie. like truncatewords:10.
bobobobo
You're absolutely right -- I've edited it to fix that
Ian Clelland