I have a table with address information. It has these fields.
- address_1
- address_2
- city
- state
- zip
No field is required. I want to display this in a template and format it nicely. I want line breaks between address_1, address_2, and the city/state/zip line for any of those that exists. I also want a comma between city and state if both exist. What is the best way to accomplish this? I started writing an if statement in the template, but it seemed to be getting a bit unwieldy. The big problem is that the user could enter only a city and state, only a zip code, a full-formed address, or anything between.
Many ways of achieving this, none are likely to be that elegant. I’ll throw this into the ring… Do this in your handler, and pass
address_partsto the template context.