/// <summary> /// Handles the Saved event of the Post control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="BlogEngine.Core.SavedEventArgs"/> instance containing the event data.</param> private static void PostSaved(object sender, SavedEventArgs e) { if (e.Action != SaveAction.Update) { return; } var post = (Post)sender; if (RelatedPostsCache.ContainsKey(post.Id)) { RelatedPostsCache.Remove(post.Id); } }
/// <summary> /// Creates the list of related posts in HTML. /// </summary> /// <param name="relatedPosts"> /// The related posts. /// </param> private void CreateList(IEnumerable <IPublishable> relatedPosts) { var sb = new StringBuilder(); const string LinkFormat = "<a href=\"{0}\">{1}</a>"; const string DescriptionFormat = "<span>{0}</span>"; sb.Append("<div id=\"relatedPosts\">"); sb.Append("<h3>+++</h3>"); sb.Append("<div>"); var count = 0; foreach (var post in relatedPosts) { if (post != this.Item) { sb.Append(string.Format(LinkFormat, post.RelativeLink, HttpUtility.HtmlEncode(post.Title))); if (this.ShowDescription) { var description = post.Description; if (description != null && description.Length > this.DescriptionMaxLength) { description = string.Format("{0}...", description.Substring(0, this.DescriptionMaxLength)); } if (String.IsNullOrEmpty(description)) { var content = Utils.StripHtml(post.Content); description = content.Length > this.DescriptionMaxLength ? string.Format("{0}...", content.Substring(0, this.DescriptionMaxLength)) : content; } sb.Append(string.Format(DescriptionFormat, description)); } count++; } if (count == this.MaxResults) { break; } } sb.Append("</div>"); sb.Append("</div>"); RelatedPostsCache.Add(this.Item.Id, sb.ToString()); }
/// <summary> /// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"></see> object and stores tracing information about the control if tracing is enabled. /// </summary> /// <param name="writer"> /// The <see cref="T:System.Web.UI.HtmlTextWriter"></see> object that receives the control content. /// </param> public override void RenderControl(HtmlTextWriter writer) { if (!BlogSettings.Instance.EnableRelatedPosts || this.Item == null) { return; } if (!RelatedPostsCache.ContainsKey(this.Item.Id)) { var relatedPosts = Search.FindRelatedItems(this.Item); if (relatedPosts.Count <= 1) { return; } this.CreateList(relatedPosts); } writer.Write(RelatedPostsCache[this.Item.Id].Replace("+++", this.Headline)); }