Skip to content

Instantly share code, notes, and snippets.

@RhysC
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save RhysC/1b6c3f0dfad2cd093b00 to your computer and use it in GitHub Desktop.

Select an option

Save RhysC/1b6c3f0dfad2cd093b00 to your computer and use it in GitHub Desktop.
Looking at a prisimic backed semi structure content delivery system...(TM) ;)
The idea being that there is a
- the above js file defines the default page document mask to be used in prismic
- collection defined in Prisimic called 'page' defined the fileter 'using mask' : 'page'
- the prismic proxy above allows for the querying of pages by key - it is assumes that the key will come from a route so it should be human and url friendly
- the proxy queries only the 'page' collection and filters by the key passed in
- the prisimc document return will be in sections and can be used to create the page (see the razor view example)
public class CmsController : BootstrapBaseController
{
private readonly PrismicProxy _prismicProxy;
public CmsController(PrismicProxy prismicProxy)
{
_prismicProxy = prismicProxy;
}
public ActionResult Dynamic(string id)
{
try
{
var document = _prismicProxy.GetPage(id);
return View(document);
}
catch (DynamicPageNotFoundException)
{
return View("Error");//should be a 404 page
}
}
}
@using prismic.extensions
@model Pickstar.Web.Cms.DocumentWrapper
@{
ViewBag.Title = Model.Document.GetText("page.title").GetOrElse("");//Note this should be structured text as per the Prisimic docs - https://developers.prismic.io/documentation/UjBeuLGIJ3EKtgBV/repository-administrators-manual#document-masks under structured text
ViewBag.MetaDescription = Model.Document.GetText("page.meta_description").GetOrElse("");
}
@section head{
<meta name="keywords" content="@Model.Document.GetText("page.meta_keywords").GetOrElse("")" />
}
@Html.Raw(Model.Document.Get("page.title").BindAsHtml(Model.DocumentLinkResolver).Value)
<article>
@Html.Raw(Model.Document.Get("page.content").BindAsHtml(Model.DocumentLinkResolver).Value)
</article>
{
"Headings" : {
"title" : {
"type" : "StructuredText",
"fieldset" : "Main title",
"config" : {
"single" : "heading1",
"placeholder" : "As displayed when with the content"
}
},
"pagekey" : {
"type" : "Text",
"fieldset" : "The page name used for routing ie in URLs. Must be lower case",
"config" : {
"placeholder" : "eg aboutus for the About Us page"
}
},
"explicittitle" : {
"fieldset" : "Optional explicit title",
"type" : "StructuredText",
"config" : {
"single" : "heading2",
"placeholder" : "Useful if your main title isn't explicit about the topic (most notably, for links)"
}
},
"illustration" : {
"fieldset" : "Main illustration",
"type" : "Image",
"config" : {
"constraint" : {
"width" : 1020
},
"thumbnails" : [ {
"name" : "Icon",
"width" : 100,
"height" : 100
}, {
"name" : "Column",
"width" : 320,
"height" : 320
}, {
"name" : "Wide",
"width" : 600,
"height" : 300
} ]
}
}
},
"Seo" : {
"meta_description" : {
"fieldset" : "Meta description",
"type" : "Text",
"config" : {
"placeholder" : "The pages meta description found in the head tag."
}
},
"meta_keywords" : {
"fieldset" : "Meta keywords",
"type" : "Text",
"config" : {
"placeholder" : "The pages meta keywords found in the head tag."
}
}
},
"Content" : {
"content" : {
"fieldset" : "Main content",
"type" : "StructuredText",
"config" : {
"placeholder" : "This is where your all your content goes.",
"minHeight" : "400px",
"imageConstraint" : {
"width" : 300
}
}
}
},
"Features" : {
"features" : {
"type" : "Group",
"fieldset" : "Features",
"config" : {
"fields" : {
"feature_title" : {
"type" : "Text",
"fieldset" : "Title",
"config" : {
"placeholder" : "The feature title."
}
},
"feature_image" : {
"fieldset" : "Image",
"type" : "Image",
"config" : {
"placeholder" : "The feature image.",
"thumbnails" : [ {
"name" : "regular",
"width" : 800,
"height" : 500
}, {
"name" : "icon",
"width" : 50,
"height" : 50
} ]
}
},
"feature_content" : {
"type" : "StructuredText",
"fieldset" : "Main content",
"config" : {
"single" : "",
"placeholder" : "A short piece on this feature"
}
}
}
}
}
},
"Metadata" : {
"related" : {
"fieldset" : "Related documents",
"type" : "Group",
"config" : {
"fields" : {
"link" : {
"type" : "Link",
"config" : {
"placeholder" : "Select an article",
"select" : "document",
"masks" : [ "article" ]
}
}
}
}
},
"date" : {
"fieldset" : "Post properties",
"type" : "Date",
"config" : {
"label" : "Post date"
}
},
"allow_comments" : {
"type" : "Select",
"config" : {
"label" : "Allow comments",
"options" : [ "Yes", "No" ]
}
},
"priority" : {
"type" : "Number",
"config" : {
"label" : "Priority",
"min" : 0
}
},
"author" : {
"fieldset" : "Author",
"type" : "Link",
"config" : {
"select" : "document",
"masks" : [ "author" ],
"placeholder" : "Select this article's author"
}
}
}
}
using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Web;
using Microsoft.Ajax.Utilities;
using Pickstar.DataAccess.Models;
using prismic;
using prismic.extensions;
using Api = prismic.Api;
namespace Pickstar.Web.Cms
{
public class DocumentWrapper
{
private readonly Api.Document _document;
private readonly Api.DocumentLinkResolver _documentLinkResolver;
public DocumentWrapper(Api.Document document, Api.DocumentLinkResolver documentLinkResolver)
{
_document = document;
_documentLinkResolver = documentLinkResolver;
}
public Api.Document Document
{
get { return _document; }
}
public Api.DocumentLinkResolver DocumentLinkResolver
{
get { return _documentLinkResolver; }
}
}
public class PrismicProxy
{
private readonly Api.Api _api;
public PrismicProxy()
{
var token = "MC5VOVpabXpJQUFDOEFVa2RH.WT5_77-9Y--_ve-_ve-_ve-_vQ7vv73vv71fVELvv70177-9a3EKHO-_vWnvv714Ee-_ve-_ve-_vXPvv70";
var url = @"https://pickstarrhystest.prismic.io/api";
_api = prismic.extensions.Api.Get(
token,
url,
cache: new prismic.ApiInfra.NoCache<Api.Response>(),
logger: (l, m) => { }).Result;
}
public DocumentWrapper GetPage(string name)
{
return new DocumentWrapper(_api.Forms["page"]
.Ref(_api.Master)
.Query(@"[ [:d = at(my.page.pagekey, """ + name.ToLower() + @""")]]")//NOTE the search is case sensitive so have a rule of lower case page keys
.SubmitableAsTask().Submit()
.Result.results.First(),
GetDocumentLinkResolver(_api));
}
private static Api.DocumentLinkResolver GetDocumentLinkResolver(Api.Api api)
{
//TODO - i am untested
return DocumentLinkResolver.For(
api,
(documentLink, maybeBookmark) => !documentLink.isBroken ? "/" : "/error");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment