Skip to content

Instantly share code, notes, and snippets.

@sniffdk
sniffdk / RazorHelpers.cs
Last active August 28, 2018 12:23
Snippets showing how to render razor files with WebPages and MCV
public class RazorHelpers
{
// This approach is using WebPages, where there is no concept of a Controller
public static string RenderTemplate(string template, object model)
{
var page = WebPageBase.CreateInstanceFromVirtualPath("~/templates/" + template + ".cshtml");
var context = new WebPageContext(new HttpContextWrapper(HttpContext.Current), page, null);
var htmlWriter = new StringWriter();
// Access your model through PageData["Model"] in the view
context.PageData["Model"] = model;
@sniffdk
sniffdk / CustomRenderControllerFactory.cs
Created October 8, 2015 07:07
Snippet to demo how to intercept the Umbraco route hijacking before a suitable controller is found
using System;
using System.Web.Routing;
using Umbraco.Web.Mvc;
public class CustomRenderControllerFactory : RenderControllerFactory
{
// Attempt to resolve an alternative controller
// This methods is called for all controllers in the request pipeline including surface controllers and possibly third party controllers.
// We need to make sure we only hijack the actual Umbraco page request.
public override Type GetControllerType(RequestContext requestContext, string controllerName)
@sniffdk
sniffdk / styles.less
Last active August 29, 2015 14:21
Change any svg attribute eg. stroke and fill in css by embedding them as data uris
// Inspiration taken from http://zslabs.com/articles/svg-background-fill/
// Note that this will replace all provided attributes in all elements in the svg, currently limited to two parameters
.icon-color(@src, @color, @attr) {
@escape-color: escape("@{color}");
@data-uri: data-uri('image/svg+xml;charset=UTF-8', "@{src}");
@attr1: extract(@attr, 1);
@attr2: extract(@attr, 2);
@val1: replace("@{data-uri}", "@{attr1}%3D%22(.*?)%22", "@{attr1}%3D%22@{escape-color}%22", "gi");
@val2: replace("@{val1}", "@{attr2}%3D%22(.*?)%22", "@{attr2}%3D%22@{escape-color}%22", "gi");
background-image: e(@val2);
@sniffdk
sniffdk / responsive.js
Created August 12, 2014 23:36
Small utility functions for handling responsive changes to javascript stuff.
var breakpoints = {
xs: 480,
sm: 768,
md: 992,
lg: 1200
},
currentWidth = 0,
currentBreakpoint = "",
breakpointMethods = []; // array of methods to be run whenever a breakpoint change is registered
@sniffdk
sniffdk / checksize
Last active February 22, 2018 08:19 — forked from anonymous/gist:1391040
declare @RowCount int, @tablename varchar(100)
declare @Tables table (
PK int IDENTITY(1,1),
tablename varchar(100),
processed bit
)
INSERT into @Tables (tablename)
SELECT TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_TYPE = 'BASE TABLE' and TABLE_NAME not like 'dt%' order by TABLE_NAME asc
declare @Space table (
name varchar(100), rows nvarchar(100), reserved varchar(100), data varchar(100), index_size varchar(100), unused varchar(100)
@sniffdk
sniffdk / GetContent.cs
Created April 2, 2014 13:46
Get IContent based on a property value
var contentService = ApplicationContext.Current.Services.ContentService;
var roots = contentService.GetRootContent();
IContent nodeItem = null;
foreach (var root in roots)
{
nodeItem = contentService.GetDescendants(root).FirstOrDefault(x => x.Properties.FirstOrDefault(y => y.Alias == "somePropertyAlias").Value.ToString() == "SomeValue");
if (nodeItem != null)
break;
}
@sniffdk
sniffdk / UmbracoHooks.cs
Last active August 29, 2015 13:58
Fix re-publish of already published descendants in Umbraco
public class UmbracoHooks : IApplicationEventHandler
{
private static readonly List<int> PublishFixes = new List<int>();
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Publishing += (sender, args) =>
{
foreach (var contentItem in args.PublishedEntities)
{
@sniffdk
sniffdk / Indexing.cs
Last active April 1, 2021 10:32
Small snippet for reindexing an entity in Examine
public void ReIndexEntity(IContentBase entity)
{
if (entity is IContent)
{
ExamineManager.Instance.ReIndexNode((entity as IContent).ToXml(), "content");
}
else if (entity is IMedia)
{
ExamineManager.Instance.ReIndexNode((entity as IMedia).ToXml(), "media");
}
@sniffdk
sniffdk / PageNotFoundContentFinder.cs
Created February 10, 2014 12:07
PageNotFoundContentFinder
public bool TryFindContent(PublishedContentRequest contentRequest)
{
if (contentRequest.Is404)
{
var site = SiteHelpers.GetSite();
var page404 = site.Get404Page();
if (page404 != null)
{
contentRequest.PublishedContent = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(page404.Id);
@sniffdk
sniffdk / ContextHelpers.cs
Last active February 28, 2022 10:03
Fake an UmbracoContext for use when doing published scheduling or other scenarios where UmbracoContext is normally null.
public class ContextHelpers
{
public static UmbracoContext EnsureUmbracoContext() {
if (UmbracoContext.Current != null)
{
return UmbracoContext.Current;
}
var httpContext = new HttpContextWrapper(HttpContext.Current ?? new HttpContext(new SimpleWorkerRequest("temp.aspx", "", new StringWriter())));