Last active
August 29, 2015 14:25
-
-
Save bjorn-ali-goransson/760806c3e43864ac96c4 to your computer and use it in GitHub Desktop.
Bootstrap rendering of blocks in EPiServer, with AJAX support on On Page Editing. Bootstrap.cshtml should be put in /Views/Shared/DisplayTemplates/ and don't forget to use Tag = "Bootstrap" in the page template.
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
/* for On Page Editing to show vertical lines when dragging/dropping */ | |
.row > div > [data-epi-block-id] { | |
float: left; | |
width: 100%; | |
} |
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
@model EPiServer.Core.ContentArea | |
@{ | |
new BootstrapContentAreaRenderer().Render(Html, Model); | |
} |
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
using EPiServer.Editor; | |
using EPiServer.ServiceLocation; | |
using EPiServer.Web.PropertyControls; | |
using EPiServer.Web.WebControls; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.HtmlControls; | |
using System.Web.UI.WebControls; | |
using EPiServer.Core; | |
using System.Web.Mvc; | |
using EPiServer.Web.Mvc.Html; | |
public class BootstrapContentAreaRenderer : ContentAreaRenderer | |
{ | |
protected override void RenderContentAreaItems(HtmlHelper htmlHelper, IEnumerable<ContentAreaItem> contentAreaItems) | |
{ | |
if (!contentAreaItems.Any()) | |
{ | |
return; | |
} | |
var row = new TagBuilder("div"); | |
row.AddCssClass("row"); | |
var rowWidth = 0; | |
htmlHelper.ViewContext.Writer.Write(row.ToString(TagRenderMode.StartTag)); | |
foreach (var contentAreaItem in contentAreaItems) | |
{ | |
var cell = new TagBuilder("div"); | |
int cellWidth; | |
var displayOption = contentAreaItem.LoadDisplayOption(); | |
if (displayOption == null || !TryTranslateTagToColumnWidth(displayOption.Tag, out cellWidth)) | |
{ | |
cellWidth = 4; | |
} | |
//if (contentRenderer.CurrentData is IFullWidthBlock) | |
//{ | |
// cellWidth = 12; | |
//} | |
if (cellWidth == 3) | |
{ | |
cell.AddCssClass("col-xs-6"); | |
cell.AddCssClass("col-md-3"); | |
} | |
else if (cellWidth == 6) | |
{ | |
cell.AddCssClass("col-xs-6"); | |
} | |
else | |
{ | |
cell.AddCssClass("col-sm-" + cellWidth); | |
} | |
if (rowWidth + cellWidth > 12) | |
{ | |
htmlHelper.ViewContext.Writer.Write(row.ToString(TagRenderMode.EndTag)); | |
htmlHelper.ViewContext.Writer.Write(row.ToString(TagRenderMode.StartTag)); | |
rowWidth = 0; | |
} | |
rowWidth += cellWidth; | |
htmlHelper.ViewContext.Writer.Write(cell.ToString(TagRenderMode.StartTag)); | |
RenderContentAreaItem(htmlHelper, contentAreaItem, GetContentAreaItemTemplateTag(htmlHelper, contentAreaItem), GetContentAreaItemHtmlTag(htmlHelper, contentAreaItem), this.GetContentAreaItemCssClass(htmlHelper, contentAreaItem)); | |
htmlHelper.ViewContext.Writer.Write(cell.ToString(TagRenderMode.EndTag)); | |
} | |
htmlHelper.ViewContext.Writer.Write(row.ToString(TagRenderMode.EndTag)); | |
} | |
public static bool TryTranslateTagToColumnWidth(string tag, out int columnWidth) | |
{ | |
switch (tag) | |
{ | |
case "half": columnWidth = 6; return true; | |
case "third": columnWidth = 4; return true; | |
case "twothirds": columnWidth = 8; return true; | |
case "fourth": columnWidth = 3; return true; | |
default: columnWidth = default(int); return false; | |
} | |
} | |
} |
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
using EPiServer.Framework; | |
using EPiServer.Framework.Initialization; | |
using EPiServer.ServiceLocation; | |
using EPiServer.Web; | |
[InitializableModule] | |
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))] | |
public class DisplayOptionsInitialization : IInitializableModule | |
{ | |
public void Initialize(InitializationEngine context) | |
{ | |
var displayOptions = ServiceLocator.Current.GetInstance<DisplayOptions>(); | |
displayOptions.Add( | |
id: "half", | |
tag: "half", | |
name: "Half" | |
); | |
displayOptions.Add( | |
id: "third", | |
tag: "third", | |
name: "Third" | |
); | |
displayOptions.Add( | |
id: "twothirds", | |
tag: "twothirds", | |
name: "Two thirds" | |
); | |
displayOptions.Add( | |
id: "fourth", | |
tag: "fourth", | |
name: "Fourth" | |
); | |
} | |
public void Preload(string[] parameters) { } | |
public void Uninitialize(InitializationEngine context) { } | |
} |
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
@using EPiServer.Core | |
@using EPiServer.Web.Mvc.Html | |
@model EPiServerSite.Models.Pages.StartPage | |
<html> | |
<head> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="/ClientResources/Styles/bootstrap-rendering.css"> | |
</head> | |
<body> | |
<div> | |
@Html.PropertyFor(m => m.Blocks, new { Tag = "Bootstrap" }) | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment