Compiles an array of HTML attributes into an attribute string. Attributes will be sorted using HTML::$attribute_order for consistency.
echo '<div'.HTML::attributes($attrs).'>'.$content.'</div>';
string Core_HTML::attributes( [ array $attributes = null ] )
参数列表
参数 类型 描述 默认值 $attributes
array
Attribute list null
string
public static function attributes(array $attributes = null)
{
if (empty($attributes))return '';
$sorted = array();
foreach (HTML::$attribute_order as $key)
{
if (isset($attributes[$key]))
{
// Add the attribute to the sorted list
$sorted[$key] = $attributes[$key];
}
}
// Combine the sorted attributes
$attributes = $sorted + $attributes;
$compiled = '';
foreach ($attributes as $key => $value)
{
if ($value === null)
{
// Skip attributes that have null values
continue;
}
// Add the attribute value
$compiled .= ' ' . $key . '="' . str_replace('"', '"', $value) . '"';
}
return $compiled;
}