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 trialEthan Chae
18,747 PointsBuild a Social Network with Flask - Macro
I keep getting k****** @treehouse.com as the result of this code. What would be the problem?
{% macro hide_email(User) %}
{% set name, domain = User.email.split("@") %}
{{% for letter_count in range(name|length) %}
{% if letter_count==0 %}
{{name[0]}}
{% else %}
{{"*"}}
{% endif %}
{% endfor %}
{{"@"+domain}}
{% endmacro %}
[MOD: added ```jinja formatting -cf]
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsIn your posted code, there is an extra left brace on the 4th line. But that's not the main issue. The issue with jinja2 templates is the addition of extra white space around items. There are two options:
#1 flatten out the loop:
{% macro hide_email(User) %}
{% set name, domain = User.email.split("@") %}
{% for letter_count in range(name|length) %}{% if letter_count==0 %}{{name[0]}}{% else %} {{"*"}}{% endif %}{% endfor %}{{"@"+domain}}
{% endmacro %}
#2 add jinja2 template markup to prevent the extra whitespace
Jinja2 templates allow you to put a "-" character at either end of the template tag. This template will also pass:
{% macro hide_email(User) %}
{% set name, domain = User.email.split("@") %}
{% for letter_count in range(name|length) %}
{%- if letter_count==0 -%}
{{name[0]}}
{%- else -%}
{{"*"}}
{%- endif -%}
{% endfor -%}
{{ "@"+domain}}
{% endmacro %}
Ethan Chae
18,747 PointsThank you for your answer Chris!