In one of my templates, I have this:
<aside class="headline image right">
@RenderSection("HeaderImage", required: false)
</aside>
In the corresponding pages, I use this to populate the image:
@section HeaderImage { <img src="whatever.jpg" alt="" /> }
Is there some simple conditional logic I can wrap around that whole containing ‘aside’ to stop it outputting the code if HeaderImage is left empty / not provided in the page template? Just looking for something as straight forward and simple as possible as I don’t want to include the aside code anywhere where someone updating files could randomly delete / edit it etc.
You could use the
IsSectionDefinedmethod:I’ve now removed the
required: falseparameter as we are calling theRenderSectionmethod only if it has been defined. You could also have anelsecondition to specify some default markup if the section has not been defined in the view.