Stripping out Babel in Flask Jinja Template

I’m working with this tutorial which has the following lines:

            {% set user_link %}
                <span class="user_popup">
                    <a href="{{ url_for('main.user', }}">
                        {{ }}
            {% endset %}
            {{ _('%(username)s said %(when)s',
                    username=user_link, when=moment(post.timestamp).fromNow())  }}

It looks this was specifically because he uses Babel translate, which he talks about on his blog.

My question is, how can I make this work when I strip all the Babel code out, because I don’t need any translation, specifically the bit after end set.

This is the sort of thing I tried but could never get the replacement correct:

{{username, username=user_link }} said {{when,when=moment(post.timestamp).fromNow())}}

>Solution :

{{ user_link }} said {{ moment(post.timestamp).fromNow() }}

The arguments after the string define the replacement map for the different keywords, so:

  • %(username)s will be replaced by user_link, because username=user_link
  • $(when)s will be replaced by moment(post.timestamp).fromNow(), because when=moment(post.timestamp).fromNow()

The s ending the $(placeholder)s is there to indicate that the placeholder should be considered as a string.

Leave a Reply