Zone gets rendered when placing an empty field in it

Topics: General
Nov 1, 2012 at 2:16 PM

1. In my Layout.cshtml I got the following code:

 @if (Model.Featured != null) {
            <section id="promo" class="section alt">
                <div class="container">
                    @Zone(Model.Featured)
                </div>
            </section>
        }

2. In my Placement.info I got the following placement

    <Place Fields_Common_Text-MyField="/Featured:2" />

So basically I configure the custom field "MyField" to render in the "Featured" zone .

If I have a page where I do not provide any value for "MyField" the "Featured" zone gets rendered anyway. I assume this happens because the value of MyField is an empty string rather than null...

Is there any way to avoid this behaviour? What I would like is to keep the placement definition like stated, however, when I do not provide a value for MyField the Featured zone should not be rendered.

is there a way to add perhaps additional checks?

PS: working in webmatrix here

Developer
Nov 1, 2012 at 7:16 PM

One way you could solve this is to write a shape table provider that describes the Fields_Common_Text shape. In the OnDisplaying event, check for the value of the field. If it's null or empty, add the shape to a non-existing zone so it won't be rendered.

Nov 2, 2012 at 3:31 PM

I must apologise, but that's a bridge to far for my knowledge.

I was hoping I could get away with a fix that I could implement using WebMatrix... 

 

Nov 2, 2012 at 4:24 PM
Edited Nov 2, 2012 at 4:24 PM

FYI; found a solution in the meanwhile, although it is a patch rather than solving the root cause

I wrote a jquery function to find myself empty zones, or more exactly in the case described above, the zone gets rendered with 2 line feeds in it. So, this script finds the zone and removes the closest section above it. 

function cleanEmptyFeaturedZone() {
    var featuredZone = $("div.zone-featured");
    if (!/\S/.test(featuredZone.text())) {
        featuredZone.closest("section").remove();
    }
}
Coordinator
Nov 2, 2012 at 4:42 PM

You could also render the Zone like that:

var zoneContent = Zone(Model.Featured);

Then check if the content is empty or full of space and render it:

@if(!String.IsNullOrEmptyWhiteSpace(zoneContent)) {
  @zoneContent
}