Skip to content

Instantly share code, notes, and snippets.

@Fustrate
Created September 7, 2011 21:08
Show Gist options
  • Save Fustrate/1201734 to your computer and use it in GitHub Desktop.
Save Fustrate/1201734 to your computer and use it in GitHub Desktop.
Potential tpl:format, untested
protected static $format_types = array();
public function tpl_format(ToxgBuilder $builder, $type, array $attributes, ToxgToken $token)
{
$this->requireEmpty($token);
$this->requireAttributes(array('value', 'as'), $attributes, $token);
$value = $builder->parseExpression('normal', $attributes['value'], $token);
$as = preg_replace('~[^a-z_-]~', '', strtolower($attributes['as']));
// Pass it off to the format handler
if (array_key_exists($as, self::$format_types))
call_user_func(self::$format_types[$as], $builder, $type, $attributes, $token);
// !!! Should there be a default output here? Maybe just emit what's in $value?
}
public static function addFormatType($name, $callback)
{
$name = preg_replace('~[^a-z_-]~', '', strtolower($name));
// Only allow one callback per format type, and make sure it's callable
if (!empty($name) && is_callable($callback))
self::$format_types[$name] = $callback;
}
public static function removeFormatType($name)
{
$name = preg_replace('~[^a-z_-]~', '', strtolower($name));
if (array_key_exists($name, self::$format_types))
unset(self::$format_types[$name]);
}
public static function getFormatTypes()
{
return self::$format_types;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment