Widget Title suppress if widget content is empty

Topics: Writing modules, Writing themes
Jul 1, 2011 at 3:02 AM

I have a widget which shows some content type data items.

I can control its UI by say RelatedContentWidget.cshtml template.

But if the data is empty I still see the widget Title.

Yes I can override the Widget-RelatedContentWidget.cshtml template

But the question is how on earth  can I  figure out  on Widget-RelatedContentWidget.cshtml that the list is empty.

There was an idea to add  some css class to upper Model on the level of RelatedContentWidget.cshtml - again no idea how

Now am I hacking this puzzle.

Won't reject any kind of help...  please  :)


Jul 1, 2011 at 3:04 AM

Can't you just As<> the content item into the list part?

Jul 1, 2011 at 12:29 PM

Alas no. It gives me an exception - I simply do not understand what should be in As<?>

In details it follows this way:

The driver returns this



            var items = query
                .Where(a => a.SlugList.Contains(currentPage.Slug))
                .Slice(part.MaxItems ?? 1);

            var list = shapeHelper.List();
            list.AddRange(items.Select(i => ContentManager.BuildDisplay(i, "Detail"))); 

            return ContentShape("Parts_RelatedContentWidget",
                () => shapeHelper.Parts_RelatedContentWidget(
                        List: list

on RelatedContentWidget template I can simply put 

@Disaplay(Model.List) and it works fine

So how can I get this Model.List on the upper level template to suppress display widget heder ?



Jul 2, 2011 at 12:10 AM

So if I understand correctly you have a RelatedSlugPart on the widget, right? Then you should be able to do something along the lines of Model.ContentItem.As<RelatedSlugPart>().SlugList.

Jul 2, 2011 at 12:28 AM

Probably I can but why?..

Well, what I need is just to change or remove the Widget Title (rendered by Widget.Wrapper.cshtml) dynamically

    if (HasText(title) || Model.Header != null)
            @if (HasText(title))

I tried this in the Driver

var widget = part.As<WidgetPart>();


if (items.Count() == 0) {
      widget.Title = "";

Yes it worked all right but surprisingly the Title was persisted (?!) in DB which I did not expect at all.

So again am searching the possibility.... just to control the WidgetPart data.

Or I have to completely rewrite the Widget.Wrapper.cshtml ? Or what ?


Jul 2, 2011 at 12:31 AM

Now I'm completely confused. Why are you setting the title property instead of just not rendering it?

Jul 2, 2011 at 12:38 AM

Wow this is me who is confused :) so how not to render it in case the list is empty?

That is what I cannot understand....

Jul 2, 2011 at 12:43 AM

with an if statement around the rendering?

Jul 2, 2011 at 1:10 AM

tried so far not succeeded ...

You mean in Widget.Wrapper.cshtml I can use something like Model.ContentItem.As<RelatedSlugPart>().SlugList.

Not I does not work. At this place I need the dynamic data which I build in the Driver and at this place I cannot reach it.

    var widget = ((IContent)Model.ContentItem).As<WidgetPart>();
    var title = widget.Title;

    var test = Model.ContentItem.As<RelatedSlugPart>().SlugList;

    var tag = Tag(Model, "article");

@* Strip wrapper for menu widget when displayed in Navigation zone and for Breadcrumbs part. *@
@if ((widget.Is<MenuWidgetPart>() && widget.Zone == "Navigation") || widget.Is<BreadcrumbsPart>()) {
else {
    if (HasText(title) || Model.Header != null) {
        @if (HasText(title)) {
    if (Model.Footer != null) {

Jul 2, 2011 at 1:14 AM

Two lines before, there is a ((IContent)Model.ContentItem).As<WidgetPart>(); Did you try to add the cast like in this call?

Jul 2, 2011 at 1:19 AM
Edited Jul 2, 2011 at 1:45 AM

Yes, yes  really I tried this one

var test = ((IContent)Model.ContentItem).As<RelatedSlugPart>();

Always get Null

Well so you mean that it is possible to reach the data prepared on the widget Driver at this place  if your are positive I will be trying again.

I would be just what I need.


You see on the Driver I query some arbitrary ContentType, build a list of it and put it on this RelatedWidget template.

But on Widget.Wrapper.cshtml  I deal only with the RelatedWidget data and know nothing about this  ContentType list which I build on the Driver.

RelatedSlugPart is the part of a ContentType not the Widget

So I do not understand how I can get to know if the list is filled or not at this place....

That is why I thought if just empty the Widget Title on the driver it wont be rendered.

Just fully frustrated.... it seems that I have to repeat the query code which I used on drive here - ugliest thing

Something or very much I still cannot perceive.

Jul 2, 2011 at 5:16 AM

Oh, I get it. This is not how it should be implemented at all. I did not notice before that you did not have the list of items on the content part, but that you were actually doing the database query from the driver and sticking the result directly onto the shape.

By the time the data arrives to the driver, it should already have all the data. This is done by properly implementing your parts and records (see http://orchardproject.net/docs/Creating-1-n-and-n-n-relations.ashx). Then you would be able to cast into the right part and get the list from there as I suggested before. But the way you do it, the list is only available from the shape, to which you do not have access from the driver because it does not yet exist (also, you are doing data access from the driver, which is not clean).

Jul 2, 2011 at 10:51 AM

Hi, Betrand thank you very much for your help.

I simply used this example http://www.deepcode.co.uk/2011/06/real-world-orchard-cmspart-7finding.html

it suited be fully.

 Well now I'll try what you suggest... sounds very encouraging.

see youu