Skip to content

Instantly share code, notes, and snippets.

@chrisdpeters
chrisdpeters / complex_implode.php
Last active November 20, 2021 06:15
Building long HTML class names
<?php
function build_banner_class(int $age) : array {
$banner_class = ['banner'];
if ($age >= 21) {
$banner_class[]= 'banner--boogie';
}
$banner_class = array_merge($banner_class, [
"color-$color",
@chrisdpeters
chrisdpeters / index.js
Last active May 18, 2019 19:18
Setting up all kinds of domain redirects in S3 and CloudFront http://blog.chrisdpeters.com/domain-redirects-s3-and-cloudfront/
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
let uri = request.uri ? request.uri : '/';
if (request.querystring) {
uri += '?' + request.querystring;
}
@chrisdpeters
chrisdpeters / _call_to_action_view.html.erb
Last active July 17, 2017 15:14
Alchemy CMS: great for websites integrated with Ruby on Rails http://blog.chrisdpeters.com/alchemy-cms/
<%- cache(element) do -%>
<%= element_view_for(element) do |el| -%>
<div class="cta">
<h3 class="cta-heading">
<%= el.render :headline %>
</h3>
<%= el.render :description %>
<div class="cta-actions">
@chrisdpeters
chrisdpeters / Address.cfc
Last active August 10, 2017 13:13
Progressively enhancing your CFWheels form with nested properties and jQuery http://blog.chrisdpeters.com/cfwheels-with-nested-properties-and-jquery/
component extends="Model" {
function init() {
belongsTo("contact");
}
}
@chrisdpeters
chrisdpeters / show-output-filtered.html
Last active August 10, 2017 13:14
Never output anything to a browser without a formatting filter http://blog.chrisdpeters.com/formatting-filter/
<h1>
<a href="/posts/fart">&lt;script&gt;>alert('All your base are belong to us!');&lt;/script&gt;</a>
</h1>
<p class="post-meta">
&lt;script&gt;sendCookieInfoToAnotherServer = function() { /* ... */ }; sendCookieInfoToAnotherServer();&lt;/script&gt;
</p>
<h2>Banana Comments</h2>
component extends="Model" {
function init() {
table(false);
// Set property labels for form fields and related error messages.
property(name="startDate", label="Start");
property(name="endDate", label="End");
//...
}
@chrisdpeters
chrisdpeters / contact.rb
Last active March 2, 2016 16:52
Users, Contacts, and Friendships
class Contact < ActiveRecord::Base
has_many :users
has_many :friendships
end
@chrisdpeters
chrisdpeters / TablelessModel.cfc
Last active December 16, 2015 16:54
Hiding complex model relationships behind form objects in CFWheels
<cfcomponent extends="Model">
<cffunction name="init">
<cfset table(false)>
</cffunction>
</cfcomponent>
@chrisdpeters
chrisdpeters / Issue.cfc
Last active August 25, 2015 21:57
CFWheels nestedProperties sortProperty fix
<cfcomponent extends="Model" output="false">
<cffunction name="init" output="false">
<cfscript>
hasMany(name="attemptedSolutions", shortcut="solution", dependent="delete");
nestedProperties(association="attemptedSolutions", allowDelete=true, sortProperty="sortOrder");
beforeValidation("$adjustAttemptedSolutionSortOrders");
</cfscript>
</cffunction>
<cffunction name="$adjustAttemptedSolutionSortOrders" access="private" hint="Adjusts array of attempted solutions before save happens so there aren't any null entries." output="false">
<cfoutput>
<!--- Instead of a lame check like `<cfif get("environment") eq "production">`,
we can instead do whatever the environment is calling for --->
<cfif
StructKeyExists(server.ENV, "GOOGLE_ANALYTICS_TRACKING_CODE")
and Len(server.ENV.GOOGLE_ANALYTICS_TRACKING_CODE)
>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){