Skip to content

Commit

Permalink
added unicode support
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Ronacher committed Apr 15, 2008
1 parent 4dfccbf commit bba21e1
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 45 deletions.
32 changes: 29 additions & 3 deletions php/Twig/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,37 @@ function twig_load_compiler()
}


/**
* A helper function that can be used by filters to get the
* current active template. This use useful to access variables
* on the template like the charset.
*/
function twig_get_current_template()
{
return $GLOBALS['twig_current_template'];
}


/* the current template that is rendered. This used used internally
and is an implementation detail. Don't tamper with that. */
$twig_current_template = NULL;


/**
* This class wraps a template instance as returned by the compiler and
* is usually constructed from the `Twig_Loader`.
*/
class Twig_Template
{
private $instance;
public $charset;
public $loader;

public function __construct($instance)
public function __construct($instance, $charset=NULL, $loader)
{
$this->instance = $instance;
$this->charset = $charset;
$this->loader = $loader;
}

/**
Expand All @@ -50,9 +70,13 @@ public function render($context=NULL)
*/
public function display($context=NULL)
{
global $twig_current_template;
$old = $twig_current_template;
$twig_current_template = $this;
if (is_null($context))
$context = array();
$this->instance->render($context);
$twig_current_template = $old;
}
}

Expand All @@ -63,16 +87,18 @@ public function display($context=NULL)
class Twig_BaseLoader
{
public $cache;
public $charset;

public function __construct($cache=NULL)
public function __construct($cache=NULL, $charset=NULL)
{
$this->cache = $cache;
$this->charset = $charset;
}

public function getTemplate($name)
{
$cls = $this->requireTemplate($name);
return new Twig_Template(new $cls);
return new Twig_Template(new $cls, $this->charset, $this);
}

public function getCacheFilename($name)
Expand Down
128 changes: 86 additions & 42 deletions php/Twig/runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,45 @@
*/


$twig_filters = array(
// formatting filters
'date' => 'twig_date_format_filter',
'numberformat' => 'number_format',
'moneyformat' => 'money_format',
'filesizeformat' => 'twig_filesize_format_filter',
'format' => 'sprintf',

// numbers
'even' => 'twig_is_even_filter',
'odd' => 'twig_is_odd_filter',

// escaping and encoding
'escape' => 'htmlspecialchars',
'e' => 'htmlspecialchars',
'urlencode' => 'twig_urlencode_filter',

// string filters
'title' => 'twig_title_string_filter',
'capitalize' => 'twig_capitalize_string_filter',
'upper' => 'strtoupper',
'lower' => 'strtolower',
'strip' => 'trim',
'rstrip' => 'rtrim',
'lstrip' => 'ltrim',

// array helpers
'join' => 'twig_join_filter',
'reverse' => 'array_reverse',
'length' => 'count',
'count' => 'count',

// iteration and runtime
'default' => 'twig_default_filter',
'keys' => 'array_keys',
'items' => 'twig_get_array_items_filter'
);


class Twig_LoopContextIterator implements Iterator
{
public $context;
Expand Down Expand Up @@ -137,16 +176,6 @@ function twig_date_format_filter($timestamp, $format='F j, Y, G:i')
return date($format, $timestamp);
}

function twig_capitalize_string_filter($string)
{
return ucfirst(strtolower($string));
}

function twig_title_string_filter($string)
{
return ucwords(strtolower($string));
}

function twig_urlencode_filter($string, $raw=false)
{
if ($raw)
Expand Down Expand Up @@ -199,40 +228,55 @@ function twig_is_odd_filter($value)
}


$twig_filters = array(
// formatting filters
'date' => 'twig_date_format_filter',
'numberformat' => 'number_format',
'moneyformat' => 'money_format',
'filesizeformat' => 'twig_filesize_format_filter',
'format' => 'sprintf',
// add multibyte extensions if possible
if (function_exists('mb_get_info')) {
function twig_upper_filter($string)
{
$template = twig_get_current_template();
if (!is_null($template->charset))
return mb_strtoupper($string, $template->charset);
return strtoupper($string);
}

// numbers
'even' => 'twig_is_even_filter',
'odd' => 'twig_is_odd_filter',
function twig_lower_filter($string)
{
$template = twig_get_current_template();
if (!is_null($template->charset))
return mb_strtolower($string, $template->charset);
return strtolower($string);
}

// escaping and encoding
'escape' => 'htmlspecialchars',
'e' => 'htmlspecialchars',
'urlencode' => 'twig_urlencode_filter',
function twig_title_string_filter($string)
{
$template = twig_get_current_template();
if (is_null($template->charset))
return ucwords(strtolower($string));
return mb_convert_case($string, MB_CASE_TITLE, $template->charset);
}

// string filters
'title' => 'twig_title_string_filter',
'capitalize' => 'twig_capitalize_string_filter',
'upper' => 'strtoupper',
'lower' => 'strtolower',
'strip' => 'trim',
'rstrip' => 'rtrim',
'lstrip' => 'ltrim',
function twig_capitalize_string_filter($string)
{
$template = twig_get_current_template();
if (is_null($template->charset))
return ucfirst(strtolower($string));
return mb_strtoupper(mb_substr($string, 0, 1, $template->charset)) .
mb_strtolower(mb_substr($string, 1, null, $template->charset));
}

// array helpers
'join' => 'twig_join_filter',
'reverse' => 'array_reverse',
'length' => 'count',
'count' => 'count',
// override the builtins
$twig_filters['upper'] = 'twig_upper_filter';
$twig_filters['lower'] = 'twig_lower_filter';
}

// iteration and runtime
'default' => 'twig_default_filter',
'keys' => 'array_keys',
'items' => 'twig_get_array_items_filter'
);
// and byte fallback
else {
function twig_title_string_filter($string)
{
return ucwords(strtolower($string));
}

function twig_capitalize_string_filter($string)
{
return ucfirst(strtolower($string));
}
}

0 comments on commit bba21e1

Please sign in to comment.