Created
April 14, 2015 18:33
-
-
Save Dillie-O/3bae0bc84b4b793a9152 to your computer and use it in GitHub Desktop.
SiteFinity ContentBlock Template with ShortCode Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="C#" %> | |
<%@ Import Namespace="Telerik.OpenAccess" %> | |
<%@ Import namespace="Telerik.Sitefinity.GenericContent.Model" %> | |
<%@ Import Namespace="Telerik.Sitefinity.Model" %> | |
<%@ Import namespace="Telerik.Sitefinity.Modules.GenericContent" %> | |
<%@ Import Namespace="Telerik.Sitefinity.Taxonomies" %> | |
<%@ Import Namespace="Telerik.Sitefinity.Taxonomies.Model" %> | |
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %> | |
<script type="text/C#" runat="server"> | |
public static class ShortcodeProcessor | |
{ | |
/// <summary> | |
/// Expands all shortcodes within the specified text. | |
/// </summary> | |
/// <param name="textToProcess">The text to process.</param> | |
/// <returns></returns> | |
public static string Execute(string textToProcess) | |
{ | |
// expand shortcode text here | |
textToProcess = FeeValueShortcodeProcessor.Expand(textToProcess); | |
textToProcess = RateValueShortcodeProcessor.Expand(textToProcess); | |
return textToProcess; | |
} | |
} | |
public static class FeeValueShortcodeProcessor | |
{ | |
/// <summary> | |
/// Expands all FeeValue shortcodes in the specified text | |
/// </summary> | |
/// <param name="inputText">The input text.</param> | |
/// <returns>String with replaced short code values</returns> | |
public static string Expand(object inputText) | |
{ | |
if (inputText == null) return string.Empty; | |
var result = inputText.ToString(); | |
// use regex to find matches | |
const string regex = @"\[FeeValue:(?<title>.*?)\]"; | |
var matches = Regex.Matches(result, regex); | |
foreach (Match match in matches) | |
{ | |
// Get Fee Title | |
var feeTitle = match.Groups["title"].Value; | |
// Replace shortcode text with retrieved Fee value from shared | |
// content blocks. Filter by the FeeValues category so that we don't | |
// get things mixed up with any actual content blocks. | |
var taxManager = TaxonomyManager.GetManager(); | |
var catManager = taxManager | |
.GetTaxonomies<HierarchicalTaxonomy>() | |
.SingleOrDefault(s => s.Name == "Categories"); | |
var feeValuesCategory = catManager.Taxa.FirstOrDefault(c => c.Title == "FeeValues"); | |
var categoryId = feeValuesCategory.Id; | |
var manager = ContentManager.GetManager(); | |
var contentItem = manager | |
.GetContent() | |
.FirstOrDefault(x => x.GetValue<TrackedList<Guid>>("Category").Contains(categoryId) | |
&& x.Title == feeTitle | |
&& x.Status == ContentLifecycleStatus.Live); | |
var feeValue = contentItem != null ? contentItem.Content.ToString() : "FEE VALUE NOT FOUND"; | |
result = result.Replace(match.Value, feeValue); | |
} | |
return result; | |
} | |
} | |
public static class RateValueShortcodeProcessor | |
{ | |
/// <summary> | |
/// Expands all RateValue shortcodes in the specified text | |
/// </summary> | |
/// <param name="inputText">The input text.</param> | |
/// <returns>String with replaced short code values</returns> | |
public static string Expand(object inputText) | |
{ | |
if (inputText == null) return string.Empty; | |
var result = inputText.ToString(); | |
// use regex to find matches | |
const string regex1 = @"\[RateValue:(?<title>.*?)\]"; | |
var matches = Regex.Matches(result, regex1); | |
foreach (Match match in matches) | |
{ | |
// Get Rate Title | |
var rateTitle = match.Groups["title"].Value; | |
// Replace shortcode text with retrieved Rate value from shared | |
// content blocks. Filter by the RateValues category so that we don't | |
// get things mixed up with any actual content blocks. | |
var taxManager = TaxonomyManager.GetManager(); | |
var catManager = taxManager | |
.GetTaxonomies<HierarchicalTaxonomy>() | |
.SingleOrDefault(s => s.Name == "Categories"); | |
var rateValuesCategory = catManager.Taxa.FirstOrDefault(c => c.Title == "RateValues"); | |
var singleCategoryId = rateValuesCategory.Id; | |
var manager = ContentManager.GetManager(); | |
var contentItem = manager | |
.GetContent() | |
.FirstOrDefault(x => x.GetValue<TrackedList<Guid>>("Category").Contains(singleCategoryId) | |
&& x.Title == rateTitle | |
&& x.Status == ContentLifecycleStatus.Live); | |
var rateValue = contentItem != null ? contentItem.Content.ToString() : "RATE VALUE NOT FOUND"; | |
result = result.Replace(match.Value, rateValue); | |
} | |
return result; | |
} | |
} | |
protected override void Render(HtmlTextWriter writer) | |
{ | |
// Do not expand shortcodes in design mode | |
if (this.IsDesignMode()) | |
{ | |
base.Render(writer); | |
return; | |
} | |
// Expand shortcodes, add additional codes inside the Execute method | |
contentHtml.Text = ShortcodeProcessor.Execute(contentHtml.Text); | |
base.Render(writer); | |
} | |
</script> | |
<asp:Literal ID="contentHtml" runat="server"></asp:Literal> | |
<sf:BrowseAndEditToolbar ID="browseAndEditToolbar" runat="server" Mode="Edit" Visible="false"> | |
</sf:BrowseAndEditToolbar> | |
<asp:PlaceHolder ID="socialShareContainer" runat="server"></asp:PlaceHolder> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment