Display message if there is no content

Oct 7, 2014 at 10:53 PM
Edited Oct 7, 2014 at 10:54 PM
I have a Careers listing page using 2Sexy Content. The organization doesn't always have job postings so if there are no items, I would like to write a custom message outside of the template's loop. Is this possible? How would I do this?
Oct 7, 2014 at 11:03 PM
check the length of the list and if its 0 then display the message no need for the loop


define a boolean at the top of temaplte

check length of list and if its 0 then set the boolean to true

outside of the loop check the boolean anf if true display mesage

( sorry no code example its already midnight here my brains dont work correctly anymore ;)
Oct 8, 2014 at 10:24 AM
Thank you for answering nokiko. Here the code samples:

@if(List.Count == 0)
    <div>The list does not contain items</div>

@* And then continue with your foreach loop *@
@foreach(var e in List)
In a Token template it could be solved like this:
<div style="display:none;" class="co-noitems-[List:Count]">The list does not contain items</div>
    .co-noitems-0 { display:block; }
Oct 8, 2014 at 7:23 PM
Thanks for answering. I'm using Razor so I added that code to my template. I had one row as a placeholder so I deleted it, but when I did that, the module returned to it's 'default' state where it has the two dropdown selects for setting the content type and the template.

How do I prevent that?
Oct 8, 2014 at 7:50 PM
yeah if you use the data directly you will need to have at least one item

i usually have one item that i give the title "dummy"

Keep that in and check for a rowcount of 1

and in the for each ad a check that
@if(List.Count == 1)
    <div>The list does not contain items</div>

@* And then continue with your foreach loop *@
@foreach(var e in List)
    if(content.Title != "dummy")

if you only use the module one time and not on multiple pages you can also use App.Data["yourcontent"] method
Oct 8, 2014 at 8:27 PM
But if there's only 1 real job posting, then it wouldn't show.

I don't know what the App.Data["yourcontent"] method is. Is there documentation for that?
Oct 8, 2014 at 8:42 PM
Edited Oct 8, 2014 at 8:44 PM
What you then do is get all the data of one specific content type ( no matter on what module it is added ( check out my blog on the news module and app example it uses that for the news_recent template for instance )

see below for a code sample, the content type here is SampleProduct. this will show everything in the content type. What i do is just have a title content type give that a dummy value ( as you need one entry for the template to fire )
@using ToSic.Eav.DataSources
    int rowCounter = 0;
    var mid = Dnn.Module.ModuleID.ToString();

    var allItems = App.Data["SampleProduct"];

       @foreach (var dict in allItems.List)
            var Content = AsDynamic(dict.Value);
            var cid = Content.EntityId;      
            <li> @Content.Toolbar @Content.Product_Name, @Content.Product_Description </li>
            rowCounter = rowCounter + 1;

Armand - your friendly (un)official 2sexycontent support person :)
Oct 8, 2014 at 11:24 PM
Ah, right. Thanks.

Only problem with this method is that items removed from the list will appear. They would have to be deleted from inside the module "Manage Content Types" area.

But I see how that would work - just adds more management.

I'm going to revisit this later. Thanks guys.
Oct 8, 2014 at 11:34 PM
no you can modify the toolbar by telling it to what content it needs to apply, there is documentation for it

for instance instead of the toolbar i can do just the edit like this, I never tried a direct delte so you would have to see if there is an option for that as well
<ul class='sc-menu' data-toolbar='[{ "attributeSetName": "SampleProduct", "action": "new" }]'></ul>