Question

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

Was it helpful?

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

OTHER TIPS

You can do this in pure python.

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

It should be easy to wrap it in a template.

${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')}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top