Best way to render different ContentType?

Topics: Customizing Orchard, Writing themes
Mar 30, 2011 at 3:09 PM
Edited Mar 30, 2011 at 3:11 PM

Hello! What is the best way to render different content types?

For example, I want to keep "Published Date" for "ContentType = BlogPost" and skip it for "ContentType = Page".

Should I just put another "if case" in my Content.cshtml file, like that:

@if (Model.Meta != null && Model.ContentItem.ContentType == "BlogPost")
{
    @Display(Model.Meta)
}
Mar 30, 2011 at 5:43 PM
Edited Mar 30, 2011 at 5:45 PM

I think one way you could do this is to modify the placement.info file for your theme so that it overrides the default rendering that includes a publish date.  In your theme's placement.info file, you could match on one or more specific "ContentType" items that you DON'T want to use the normal/default rendering, and then figure out how to render the common metadata for the matched items without the publish date.  Problem is, I'm not that familiar with examples of doing that in placement.info files.  The advantage of this approach, if you can make it work, is it doesn't affect so much of the infrastructure for rendering content types in your site. 

Of course, you could also go into the common content .cshtml files that define the views, and modify those directly using logic like what you are suggesting.  For instance, I just tried this in the Parts.Common.Metadata.cshtml file, using code similar to your first line.  This code basically renders the publish date metadata for every content type BUT the page type.  When I create new blog posts, for instance, I see the pub date, but on pages in my test site, I no longer see it.  The problem with this approach is it's a pretty brute force method and affects a lot of your content.

@if (Model.Meta != null && Module.ContentItem.ContentType != "Page") {
      <div class="published">@Display.PublishedState(dateTimeUtc:  Model.ContentPart.PublishedUtc)</div>
}

Coordinator
Mar 30, 2011 at 6:39 PM

Yes, published date is a shape, so you should be able to do it with Placement.info.

Mar 30, 2011 at 8:50 PM
Edited Mar 30, 2011 at 8:57 PM

Yes, Timlt! That's what I was talking about. Overriding Content.cshtml affecting all content types.

Sebastienros, could you provide sample placement.info file for that case?

For example, we have site with Home page (Page), Blog page (Blog). Blog page have list of BlogPost items(BlogPost Summary) with links to individual BlogPost pages (BlogPost not Summary)

So we need 4 different rendering for

  • Page
  • Blog
  • BlogPost Summary
  • BlogPost
Coordinator
Mar 30, 2011 at 11:07 PM
Edited Mar 30, 2011 at 11:07 PM

Something like this should work:

<Placement>
  <Match ContentType="Page">
    <Place Parts_Common_Metadata="Meta:2"/>
  </Match>
  <Match ContentType="Blog">
    <Place Parts_Common_Metadata="-"/>
  </Match>
  <Match ContentType="BlogPost">
    <Match DisplayType="Detail">
      <Place Parts_Common_Metadata="Meta:2"/>
    </Match>
    <Match DisplayType="Summary">
      <Place Parts_Common_Metadata="-"/>
    </Match>
  </Match>
</Placement>


May 12, 2011 at 1:58 PM

Hi all.

I am using Orchard version 1.1.30 with the "Theme Machine" theme. Two questions:

1) is it possible to edit the  Placement.info  file to display just the Published Date without the time (for pages, blogs, everything) ?

2) In the meantime I have been able to remove the published date+time using ContentType = Page, Blog, BlogPost but still shows up in the homepage in the "recent posts" widget. How can I specify it in a Match statement for the Placement.info file ?

Thank you !

Mark

May 12, 2011 at 4:13 PM

I also used theme machine for my first site, I found the way to control the display of items like the date and time fields is in the site.css file. All elements rendered on page are defined in there, if you use firefox just get the firebug download and it will enable you to see which pieces of the css file are being used for each element. Then if you add 'shape tracer' to orchard you can use that to create new layout cshtml files for any section of any page seperately for total control. Combining the 2 makes orchard into a totally user friendly project as it is just a simple matter of trial and error untill you have everything the way you want!

May 12, 2011 at 4:26 PM

The only problem here is the Published Date has no template file, it's generated from code (in CoreShapes I think). But you can still override it with your own template, there's just no example to copy from.

May 12, 2011 at 4:42 PM

I have simply set all the Date and time fields to visibilty: hidden in the css for my site as I wanted none to be shown anywhere and am not a programmer. I have found that using the cascade format makes for a simple fix suitable for a single sitewide solution.

May 12, 2011 at 4:42 PM

Thank you Weedy for your hints.

I understand that (for example) editing the site.css I could use  display: none  to hide the whole date time.

 

the same could be obtained editing the .cshtml file obtained with  shape tracer:

<div class="published">@Display.PublishedState(dateTimeUtc: Model.ContentPart.PublishedUtc)</div>

removing the @Display content.

 

But what about displaying just the date and not the time ? I found no way of doing this in the docs. Seems I should add a custom implementation of  PublishedUtc   with different name to expose just the date.

I would like to avoid writing code in order to have a simpler upgrade path when next versions of Orchard will come out....

May 12, 2011 at 4:50 PM

You can provide your own PublishedState.cshtml which will override the code shape. Model.dateTimeUtc will be a DateTime which you can format how you like... Definitely the preferable way over CSS or shape code.

May 12, 2011 at 4:59 PM

yes ! thank you all !

this is what I just did:

1) used  shape tracer  to create my own copy of  Parts.Common.Metadata.Summary-BlogPost.cshtml  under my (now custom) theme

2) edited it like this:

@{

var dateOnly = Model.ContentPart.PublishedUtc.Date;

}

<div class="published">@dateOnly.ToString("MM/dd/yyyy")</div>

I will do the same for the few shapes I have to customize under this theme

Cheers,

Mark

Mar 4, 2012 at 10:54 PM

This certainly helped in allowing me to format the strings as ISO-8601 (the non-US International date time system)

What I did was copy the files:

Parts.Common.Metadata.cshtml
Parts.Common.Metadata.Summary.cshtml

from Core\Common\Views into my theme\Views folder.

I then edited them as follows:

@{var dateTime = Model.ContentPart.PublishedUtc.AddHours(-8);}
<div class="published">@dateTime.ToString("yyyy-MM-dd, HH:mm")</div>
Notice how I subtracted eight hours to allow the date to conform to my local timezone. Otherwise they would have shown the publish date in UTC, which would have been quite a bit off in many cases, from the Pacific (UTC-8) timezone I work in.

Apr 20, 2012 at 11:16 AM

Great step,

I removed the 2nd row and the date stamp does not show anymore, which I wanted.