     self.handler = handler
   }
 }
+
+func handler(expansion:((variable:String, value:String) -> (String)))(variable:String, value:String?) -> String {
+  if let value = value {
+    return value
+  }
+
+  return ""
+}
+
+func expandPercentEscaped(variable:String, value:String?) -> String {
+  if let value = value {
+    return value.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
+  }
+
+  return ""
+}
+
+func expandPercentEncoded(variable:String, value:String?) -> String {
+  if let value = value {
+    return value.percentEncoded()
+  }
+
+  return ""
+}
+
+func expandValue(variable:String, value:String?) -> String {
+  if let value = value {
+    return value
+  }
+
+  return ""
+}
+
+func expandKeyValue(variable:String, value:String?) -> String {
+  if let value = value {
+    return "\(variable)=\(value)"
+  }
+
+  return ""
+}
+
+// MARK: Extensions
 
 extension NSRegularExpression {
   func substitute(string:String, block:((String) -> (String))) -> String {

    return CFURLCreateStringByAddingPercentEscapes(nil, self, nil, ":/?&=;+!@#$()',*", CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding))
  }
}

public struct URITemplate : Printable, Equatable {
  let template:String

      if let expander = expander {
        expression = expression.substringFromIndex(expression.startIndex.successor())
      } else {
        expander = Expander(prefix: "", joiner: ",", expandPercentEncoded)
      }

      return expander.prefix + expander.joiner.join(expression.componentsSeparatedByString(",").map { variable -> String in
