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

Francis Wanyonyi
Francis Wanyonyi
6,768 Points

issue using python control blocks in html templates (flask web framework)

Hi I have been trying to answer the given challenge to no avail. The challenge requires me to take an email value and hide the details then print out the email format: t***@example.com for the email test@example.com. My idea was to implement it entirely using python code in the html template using control blocks but this hasn't worked. help!!

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) %}

  {% a,b = user.email.split() %}
  {% a = list(a), c = len(a), d=a[0:1] %}
  {% for num in range(1,c) %}
  {% d.append('*') %}
  {% endfor %}
  {% d = ''.join(d) %}

  {{ d+b }}

{% endmacro %}

1 Answer

Francis Wanyonyi
Francis Wanyonyi
6,768 Points

{% macro hide_email(user) %} {% with name, domain = user.email.split('@') %} {{ name[0] }}{% for n in name[1:] %}*{% endfor %}{{'@' + domain}} {% endwith %} {% endmacro %}

courtesy of Diallo West