I often have optional fields on a content item. If the author fills out an optional field then that field should be displayed on the page along with any markup that goes with that field. For example, let's say that I have a field called Subheading (single line text). If the author enters text for Subheading then it should be displayed on the page like this:
<h4>[Subheading text]</h4>
For a normal field I would do it like this using Glass Mapper:
<h4><%=Editable(x => x.Subheading)%></h4>
But if there is nothing entered for Subheading then I don't want to display anything on the page including the h4 tags. Additionally the markup that goes with the field could be more complicated than that. This is just a simple example.
Is there some simple way using Glass Mapper that I can handle this?
I think you will have to use a wrapping if statement:
<%if(!Model.Subheading.IsNullOrEmpty()) {%>
<h4><%=Editable(x => x.Subheading)%></h4>
<%}%>
Glass doesn't have a way to do this out of the box because it is implementation specific.