Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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', username=post.author.username) }}">
                        {{ post.author.username }}
                    </a>
                </span>
            {% 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading