Skip to content

Instantly share code, notes, and snippets.

View mgerring's full-sized avatar

Matthew Gerring mgerring

View GitHub Profile
@mgerring
mgerring / filters.py
Created August 8, 2012 20:37
Django "replace" filter
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def replace(value, arg):
args = arg.split(',')
return value.replace(args[0], args[1])
@mgerring
mgerring / humanize.php
Created August 27, 2012 19:25
Humanizes a list, in the form of an array, by adding commas and the word "and". No Oxford commas, because fuck you.
<?php
function humanize($array) {
while(count($array) > 0) {
$output .= array_pop($array);
if(count($array) > 1) {
$output .= ', ';
} elseif(count($array) > 0) {
$output .= ' and ';
}
}
@mgerring
mgerring / actions.py
Created September 5, 2012 22:04
"Export to CSV" action for django admin
import unicodecsv
from django.http import HttpResponse
def export_as_csv_action(description="Export selected objects as CSV file",
fields=None, exclude=None, header=True):
"""
This function returns an export csv action
'fields' and 'exclude' work like in django ModelForm
'header' is whether or not to output the column names as the first row
"""
@mgerring
mgerring / functions.php
Last active December 11, 2015 01:58
Automatically look for different templates depending on what page template you're using
<?php
function get_page_template_name() {
global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
return str_replace('.php','',$template_name);
}
@mgerring
mgerring / breakpoints.css.scss
Last active December 17, 2015 21:19
Responsive placeholders in SASS
@mixin media-breakpoint($point) {
// &{ @content; } doesn't mean anything in the compiled output,
// but will workaround a SASS compiler issue that doesn't let
// us use %placeholders inside nested media queries.
@if $point == 'tablet-up' {
@media screen and (min-width: 48em) { &{ @content; } }
}
@else if $point == 'desktop-up' {