Skip to content

Instantly share code, notes, and snippets.

@rsleggett
Last active December 22, 2015 00:38
Show Gist options
  • Save rsleggett/6390335 to your computer and use it in GitHub Desktop.
Save rsleggett/6390335 to your computer and use it in GitHub Desktop.
Context Engine Demo Gist
private bool SmartPhoneContext(HttpContextBase context)
{
try
{
var contextEngine = new ContextEngine();
return contextEngine.Device.IsMobile && !contextEngine.Device.IsTablet && contextEngine.Device.DisplayWidth > 480;
}
catch(Exception)
{
return false;
}
}
private bool TabletContext(HttpContextBase context)
{
try
{
var contextEngine = new ContextEngine();
return contextEngine.Device.IsTablet;
}
catch (Exception)
{
return false;
}
}
using System;
using System.Web;
using System.Web.WebPages;
using Sdl.Tridion.Context;
namespace Your_Website.Classes
{
///<summary>
/// DemoDisplayModeHelper is responsible for filling the DisplayModeProvider with some custom tests
/// Author: Robert Stevenson-Leggett
/// Date: 2013-02-15
///</summary>
public class DemoDisplayModeHelper
{
public void AddDisplayModes(DisplayModeProvider provider)
{
provider.Modes.Insert(0, new DefaultDisplayMode("Tablet")
{
ContextCondition = (context => TabletContext(context))
});
provider.Modes.Insert(1, new DefaultDisplayMode("SmartPhone")
{
ContextCondition = (context => SmartPhoneContext(context))
});
provider.Modes.Insert(2, new DefaultDisplayMode("Mobile")
{
ContextCondition = (context => MobileContext(context))
});
provider.Modes.Insert(3, new DefaultDisplayMode("Desktop")
{
ContextCondition = (context => DesktopContext(context))
});
}
private bool DesktopContext(HttpContextBase context)
{
try
{
var contextEngine = new ContextEngine();
return !contextEngine.Device.IsMobile && !contextEngine.Device.IsTablet;
}
catch (Exception)
{
return true;
}
}
private bool MobileContext(HttpContextBase context)
{
try
{
var contextEngine = new ContextEngine();
return contextEngine.Device.IsMobile && !contextEngine.Device.IsTablet && contextEngine.Device.DisplayWidth < 481;
}
catch (Exception)
{
return false;
}
}
private bool SmartPhoneContext(HttpContextBase context)
{
try
{
var contextEngine = new ContextEngine();
return contextEngine.Device.IsMobile && !contextEngine.Device.IsTablet && contextEngine.Device.DisplayWidth > 480;
}
catch(Exception)
{
return false;
}
}
private bool TabletContext(HttpContextBase context)
{
try
{
var contextEngine = new ContextEngine();
return contextEngine.Device.IsTablet;
}
catch (Exception)
{
return false;
}
}
}
}
@using DD4T.Mvc.Html
@model Your_Website.Models.PresenterViewModel
<article>
@if(!string.IsNullOrEmpty(Model.Image))
{
<img src="@Model.Image.ResizeToWidth(220)" alt="@Model.Name" />
}
<h1>@Model.Name</h1>
<p>@Model.JobTitle - @Model.Organisation</p>
@Html.Raw(Model.Biography)
</article>
@using DD4T.Mvc.Html
@model Your_Website.Models.PresenterViewModel
<article>
<h1>@Model.Name</h1>
<p>@Model.JobTitle - @Model.Organisation</p>
@if(!string.IsNullOrEmpty(Model.Image))
{
<p>
<img src="@Model.Image.ResizeToWidth(120)" alt="@Model.Name" />
</p>
}
@Html.Raw(Model.Biography)
</article>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment