1

I would like to do something like this, restricted on on line. I can't use multi line.

% if object.lang == 'fr_FR': 'Rappel de paimenet' % else: 'Payment reminder' %endif

I know that this is not the good syntax, but I did find in the documentation how I coiuld manage to do it ?

Cheers

Mark Hildreth
  • 42,023
  • 11
  • 120
  • 109
renard
  • 1,368
  • 6
  • 20
  • 40
  • There is an [old doc from Indico](https://getindico.io/legacy-docs/wiki/Dev/Technical/UsingMako.html) which proposes alternatives. – somenxavier Jan 16 '23 at 18:50

4 Answers4

2

I don't think you can do this with Mako, at least not in the way you want.

The % can appear anywhere on the line as long as no text precedes it;

One way you can do this is to not put the if-statement in the template. It seems you are trying to use a translation, so make the translation in the code you are using to feed the template:

text = 'Rappel de paimenet' if object.lang == 'fr_FR' else 'Payment reminder'
# Now your template doesn't need to use an if-statement
template = Template("""Status: {{text}}""").render(text=text)

Since it appears that you are using this for translations, I would look into using gettext and Babel. If you are using this in a web application, look into any translation-specific utilities (such as django-babel or flask-babel).

Mark Hildreth
  • 42,023
  • 11
  • 120
  • 109
0

You can do this in pure python.

var = {True: newValueIfTrue, False: newValueIfFalse}[condition]

It should be easy to wrap it in a template.

Ali Rasim Kocal
  • 528
  • 3
  • 14
0

We need to use "\n":

tmpl = "% if object.lang == 'fr_FR':\n 'Rappel de paimenet'\n  % else:\n 'Payment reminder'\n % endif"

If we use file template, we need to expand "\n". As commented in this post and unicode Chapter in official Mako documentation, one solution could be:

t2 = Template(filename="test.tmpl", output_encoding='utf-8')
result = t2.render(object.lang="English").decode("unicode_escape")
print(result)
somenxavier
  • 1,206
  • 3
  • 20
  • 43
-1
${object.lang == 'fr_FR' and 'Rappel de paimenet' or 'Payment reminder'}

Or if you want to extend it to more languages, with english as the default:

${{'fr_FR': 'Rappel de paimenet', 'es_ES': ...}.get(object.lang, 'Payment reminder')}
patstew
  • 1,806
  • 17
  • 21