Question: I would like to output the following using django templates:

```
Manhattan, NY, USA

```

with the following code:

```
{{ city }}, {{ state }}, {{ country }}

```

But sometimes one or more of those variables might be empty. Let's say the state variable is empty, then the output will look like this:

```
Manhattan, , USA

```

So my question is what is the best way of dealing with commas without using multiple if statements.

Thanks

Answer: 
There may be a template filter to achieve this, but I'm unaware of it. As such, I'd write my own template filter to add a string to the end of my values. It would be used like this:

```
{{ city|suffix:", " }}{{ state|suffix:", " }}{{ country }}

```

Information on creating custom template filters can be found in the Django documentation here:

<https://docs.djangoproject.com/en/dev/howto/custom-template-tags/>

Your template filter would look something like this:

```
from django import template
register = template.Library()

@register.filter(name='suffix')
@stringfilter
def suffix(value, arg):
    "Suffixes a string with the argument, only if the string is not empty"
    if value:
         return value + arg
    return value

```

Although this will only work if `country` is always supplied.
You could put all the variables in a list and use the join filter or a for loop with a single if statement. Though either of these solutions would affect the clarity and readability of your template and I tend to prefer if statements in such cases especially if more complex HTML is involved between the comma-separated items.