pythonmako

Mako, use if / else control structure in one line


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


Solution

  • 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).