Skip to content

Display Modes

Simon Yohannes edited this page Nov 30, 2019 · 5 revisions

display modes functionality was dropped from asp.net core but Puck supports the functionality anyway. basically, display modes allow you to pick different templates depending on the current request. for example you can check the user agent of the current request and decide to display a different template based on that user agent.

by default, there's a display mode registered for iPhones. here's the code, taken from Startup.cs:

var displayModes = new Dictionary<string, Func<Microsoft.AspNetCore.Http.HttpContext,bool>> {
                {"iPhone",(context)=>{return context.Request.Headers.ContainsKey("User-Agent") 
                    && context.Request.Headers["User-Agent"].ToString().ToLower().Contains("iphone"); } }
            };

this display mode checks that the user agent contains the string "iphone".

the dictionary value is a func which takes in a HttpContext parameter from the current request and returns a bool to indicate if there is a match for the display mode.

you can add more display modes to the dictionary or remove this default one if you like. the key of the dictionary is the name of the display mode and this will be used to look for an alternative version of the template for the current page. so if a request came in from an iPhone, and the template for the current page is ~/views/home/homepage.cshtml, Puck will first check to see if ~/views/home/homepage.iPhone.cshtml exists and will use that as the template if it does. if the dictionary key was "iPad", then Puck would check if ~/views/home/homepage.iPad.cshtml exists, if the the func returned true for the current HttpContext.

this requires a check to see if the alternative template file exists but the result is cached so you shouldn't worry about performance.