Because empty tags are bad

By on (tags: mvc, categories: code)

Well, not always, but there are times when they are so…what can I do to avoid them.

Up to mvc 3 there’s a problem – if I want to render something wrapped in let’s say a paragraph, but I don’t want that paragraph to appear if the content is empty, then the markup looks like this:

That’s a bit verbose. If I skip that check, then an empty hole is rendered on the page so again – not good.

What I came up with today is this:

The <text> tags are Razors way of saying render pure so as a result, this will render just the value of content variable. The nice thing about this is that you can do things like this:

The two last parameters are optional and have defaults but are there for greater flexibility. So without further ado, here’s the code: