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

Pete Jeffryes
Pete Jeffryes
5,752 Points

I need help with this macro in 'Build a Social Network with Flask', any ideas?

This won't pass. Another post suggested using the

{% hidden = (n[0] + ('*' * ((n|length)-1) + '@' + domain) %}

I also tried

{% hidden = (n[0] + ('*' * (len(n)-1) + '@' + domain) %}

but that didn't work either.

Please let me know if you know what the issue is, 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, domain = User.email.split('@')%}
{% n = [] %}
{% for l in name %}
{% n.append(l) %}
{% endfor %}
{% hidden = (n[0] + ('*' * ((n|length)-1) + '@' + domain) %}
{% print hidden %}
{% endmacro %}

2 Answers

So, in Flask/Jinja2 templates, you need to use the set command to set a variable within a template:

{% set name, domain = User.email.split('@')%}

And you 'print'/output expressions in the HTML using the double curly braces ( {{ }} ):

e.g.

{{ hidden }}

Note that you're adding in an additional loop and empty list that you don't need. You should be able to use the parts of the name variable that you need when you set hidden. In fact, you don't even really need to set hidden, you could just output the whole concatenated string within the double curly braces.

So I've realised now, while looking at your question and code, that despite what the code challenge description says, you don't actually need a for loop at all. Maybe something for Kenneth Love to look at?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

We didn't cover {% set %} in the videos so that's not a reasonable expectation for people. Most will solve it with a for loop, but, yes, it's possible to not use one.

Fair point Kenneth Love. But using a for loop, wouldn't you need to cut out the whitespace also? Was that covered in the videos?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You shouldn't have to cut out any whitespace.

Pete Jeffryes
Pete Jeffryes
5,752 Points

Thanks, Iain. Appreciate the help.

Just in case anyone is using this code for their solution, there is also a missing closing parenthesis in setting hidden.