1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Fuel\Common |
4
|
|
|
* @version 2.0 |
5
|
|
|
* @author Fuel Development Team |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2010 - 2015 Fuel Development Team |
8
|
|
|
* @link http://fuelphp.com |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fuel\Common; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Helper class to deal with common HTML related operations. |
15
|
|
|
* |
16
|
|
|
* @package Fuel\Common |
17
|
|
|
* |
18
|
|
|
* @since 2.0 |
19
|
|
|
*/ |
20
|
|
|
abstract class Html |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Returns a HTML tag |
24
|
|
|
* |
25
|
|
|
* @param string $name Name of the tag to render. (Eg, img, form, a...) |
26
|
|
|
* @param array $attributes Any attributes to apply to the tag to render |
27
|
|
|
* @param null|string $content If not set to null will create a tag of the form "<name<content</name<". Otherwise will render as a single tag. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function tag($name, $attributes = array(), $content = null) |
32
|
|
|
{ |
33
|
|
|
$tag = '<' . $name; |
34
|
|
|
|
35
|
|
|
$attributeString = static::arrayToAttributes($attributes); |
36
|
|
|
|
37
|
|
|
//Add the attribute string if needed |
38
|
|
|
if ( !empty($attributeString) ) |
39
|
|
|
{ |
40
|
|
|
$tag .= ' ' . $attributeString; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
//Work out how we are going to close the tag |
44
|
|
|
if ( is_null($content) ) |
45
|
|
|
{ |
46
|
|
|
//No content for the tag so just close it. |
47
|
|
|
$tag .= '/>'; |
48
|
|
|
} |
49
|
|
|
else |
50
|
|
|
{ |
51
|
|
|
$tag .= '>' . $content . '</' . $name . '>'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $tag; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Produces a string of html tag attributes from an array |
59
|
|
|
* |
60
|
|
|
* array('name' => 'test', 'foo' => 'bar') |
61
|
|
|
* becomes: |
62
|
|
|
* 'name="test" foo="bar"' |
63
|
|
|
* |
64
|
|
|
* @param array $attributes |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public static function arrayToAttributes(array $attributes) |
69
|
|
|
{ |
70
|
|
|
//Build a list of single attributes first |
71
|
|
|
$attributeList = array(); |
72
|
|
|
|
73
|
|
|
foreach ( $attributes as $key => $value ) |
74
|
|
|
{ |
75
|
|
|
// If the value is not false add the attribute. This allows attributes to not be shown. |
76
|
|
|
if ( $value !== false ) |
77
|
|
|
{ |
78
|
|
|
if ( is_string($key) ) |
79
|
|
|
{ |
80
|
|
|
$attributeList[] = htmlspecialchars($key) . '="' . htmlspecialchars($value) . '"'; |
81
|
|
|
} |
82
|
|
|
else |
83
|
|
|
{ |
84
|
|
|
$attributeList[] = $value; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return implode(' ', $attributeList); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|