Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Create a macro name hide_email... Challenge

So, my code here is a bit verbose. I looked and found a more succinct way of putting things (I will one day be good at this, I swear). The code below works in a python command prompt (on my computer). Why would it not work here? Just a curiosity.

Thanks

lunch.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    class User:
        email = None
    user = User()
    user.email = 'kenneth@teamtreehouse.com'
    return render_template('user.html', user=user)
templates/macro.html
{% macro hide_email(User) %}
  {% name, address = User.email.split('@') %}
  {% list(name) %}
    {% for iter, char in enumerater(name) %}
      {% if iter == 0 %}
        {% pass %}
      {% else %}
        {% name[iter] = '*' %}
      {% endif %}
    {% endfor %}

  {% coded_email = ''.join(name) + '@' + address  %}

  {{ coded_email }}

{% endmacro %}

1 Answer

Max Hirsh
Max Hirsh
16,773 Points

I think the reason it doesn't work here is that jinja2 doesn't allow the creation of new variables unless they are part of a for loop. You can get around this by directly manipulating things like user.email.split('@')[0][1]. Also remember that the user is imported to the template as the lowercase variable "user".