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

Jay McCormick
Jay McCormick
6,646 Points

hide_email macro

Why is this not passing? The python works in the terminal, so I'm thinking it's my jinja template, or how I'm accessing the email attribute. Or both.

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) %}
{% email_list = list(User.email) %}
{% replace_list = range(1, User.email.index('@')) %}
{% for i in replace_list %}
    {% email_list[i] = '*' %}
{% endfor %}
{% show_email = "".join(email_list) %}
{{ show_email }}
{% endmacro %}

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

You can't assign to a variable using slice notation in a jinja2 template. You need to build up the output characters

 {% macro hide_email(User) %}
  {# split name on @ into username and domain #}
  {% with name, domain = User.email.split('@') %}
    {# concatenate first name char, loop over remaining name chars replacing #}
    {# with '*', the @-sign, and the domain #}
    {{ name[0] }}{% for letter in name[1:] %}*{% endfor %}@{{ domain }}
  {% endwith %}
{% endmacro %}

You must be careful, the jinja2 template wants to add a SPACE between template tags and at NEWLINES.

For additional solutions see this macros challenge post

Jay McCormick
Jay McCormick
6,646 Points

thanks for the help Chris!