BlogEngine.Net Expanding Categories And Tags

Oct 06, 2008
by:   Tim Stanley

BlogEngine.Net provides the ability to selectively disclose information in the blog postings based on the configuration settings.  I took Dave Burke's concept on the Flexible Post Excerpt Display and expanded it to include expansion of all categories and tags even if the ShowDescriptionInPost setting was set to true.

BlogEngine.Net expands data in a blog as follows:

  1. ShowDescriptionInPost = true, shows the content in the description of the blog followed by .
  2. ShowDescriptionInPost = false, BreakPost extension enabled, shows the content of the blog entry until the tag followed by the text [..more].
  3. ShowDescriptionInPost = false, BreakPost extension disabled, shows the content of the entire blog entry.

The new logic will override these settings and expand the category and tags, but still take into account the BreakPost extension logic.  If the description isn't present, the first 300 characters of the post are used.

Recommended Best Practices

  1. Always place a good description in the entry description section (excerpt).
  2. Always place a tag around the first or second paragraph at the beginning of the entry.

The description section doesn't allow for formatted HTML or images, but expanding the description until the tag does.

Core BlogSettings.cs changes

Add ExpandCategories and ExpandTags properties.

/// <summary>
/// Expand categories if true. 
/// Overrides showDescriptionInPostList.
/// </summary>
public bool ExpandCategories
{
    get { return expandCategories; } 
 
    set { expandCategories = value; }
}
 
/// <summary>
/// Expand tags if true. 
/// Overrides showDescriptionInPostList.
/// </summary>
public bool ExpandTags
{
    get { return expandTags; }
 
    set { expandTags = value; }
}

Admin Settings.aspx.cs Changes

Add the ability to update the new properties via the Admin Settings page.

 

BlogSettings.Instance.ExpandCategories = cbExpandCategories.Checked;
BlogSettings.Instance.ExpandTags = cbExpandTags.Checked;
 
// In BindSettings()
cbExpandCategories.Checked = BlogSettings.Instance.ExpandCategories;
cbExpandTags.Checked = BlogSettings.Instance.ExpandTags;
 
// In btnSave_Click()
BlogSettings.Instance.ExpandCategories = cbExpandCategories.Checked;
BlogSettings.Instance.ExpandTags = cbExpandTags.Checked;

Admin Settings.aspx.cs Changes

<label for="<%=cbExpandCategories.ClientID %>">Expand Categories</label>
<asp:CheckBox runat="server" ID="cbExpandCategories" /><br />
 
<label for="<%=cbExpandTags.ClientID %>">Expand Tags</label>
<asp:CheckBox runat="server" ID="cbExpandTags" /><br />

User controls/PostList.ascx.cs Changes

Use the logic for the new properties at display time (PostList).

postView.ShowExcerpt = BlogSettings.Instance.ShowDescriptionInPostList;
bool isCategoryPostList = false;
bool isTagPostList = false;
if (HttpContext.Current.Request.RawUrl.IndexOf("category") != -1)
{ 
    isCategoryPostList = true;
    postView.ShowExcerpt = !BlogSettings.Instance.ExpandCategories;
}
if (HttpContext.Current.Request.RawUrl.IndexOf("tag") != -1)
{
    isTagPostList = true;
    postView.ShowExcerpt = !BlogSettings.Instance.ExpandTags;
}

References

Add to favorites Send to a friend Digg It! DZone It! StumbleUpon Technorati Reddit Del.icio.us NewsVine Furl BlinkList