HTML Helper
This helper provides HTML abstraction to simplify your view/element/layout
Eg.:
Some wrappers can accept optional additional arguments, the css and cssInline can accept an extra argument for media type, eg.:
Can either set
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">;
Or to one of the preset values (not case-sensitive):
Version is optional and can be exact (1.8.2) or just version-major (1 or 1.8)
The
Syntax is very flexible:
This method will only operate from the first occurrence in your code, subsequent calls will not output anything but you should add it anyway as it will make sure that your code continues to work if you later remove a previous call to
Tools provided:
Set
If you have an SSL page, it will prepend the http protocol (and your hostname) to all local URLs that do not include a protocol (http:// or https://), this makes it so that users who follow local non-SSL links from an SSL-enabled page do not stay in SSL mode when they leave the page.
Use this method to create non-linked anchor tags (page anchors.)
Your list values can contain subarrays to generate multi-tiered lists. Tier levels are infinite, you can nest an infinite amount of lists.
API >> mvc >> helpers >> html
Each element in $links takes the URL as a key and the label as the value. If you add an element with a dynamically-assigned key (or a numeric key) it will display as plain text instead of as a link. Eg.:
would return:
Code wrappers
These produce XHTML-strict/XML-valid containers for:js
- a JavaScript filejsInline
- a block of JavaScript codejsInlineSingleton
- a block of JavaScript code that will only execute once (even if echoed out twice) and will try to be put added to the document head, if the head has already been echoed out then this method will returned the code block to echo out inline.css
- a CSS filecssInline
- a block of CSS codecssInlineSingleton
- a block of CSS code that will only execute once (even if echoed out twice) and will try to be put added to the document head, if the head has already been echoed out then this method will returned the code block to echo out inline.
Eg.:
echo $html->jsInline('alert("Hello world")');
Some wrappers can accept optional additional arguments, the css and cssInline can accept an extra argument for media type, eg.:
echo $html->cssInline('body {background: #FFF;}', 'print');
Basic wrappers
Basic wrappers also exist solely as an alternative formatting that makes it faster to modify HTML tag types at a later time (eg. changing an h2 to h3,) use if you like or just ignore and use regular HTML:div
li
p
h1
throughh4
setDocType($docType)
Allows modification of the docType header. This method must be called before theheader()
method.Can either set
$docType
to an actual doctype definition like:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">;
Or to one of the preset values (not case-sensitive):
Mobile
(alias for the most-strict Mobile DTD, currently 1.2)Mobile 1.2
(alias for XHTML Mobile 1.2)Mobile 1.1
(alias for XHTML Mobile 1.1)Mobile 1.0
(alias for XHTML Mobile 1.0)XHTML Mobile 1.2
XHTML Mobile 1.1
XHTML Mobile 1.0
XHTML
(Alias for XHTML 1.1, the default DTD, there is no need to apply this method for an XHTML 1.1 doctype)XHTML 1.1
(this is the default DTD, there is no need to apply this method for an XHTML 1.1 doctype)XHTML 1.0
(Alias for XHTML 1.0 Strict)XHTML 1.0 Strict
XHTML 1.0 Transitional
XHTML 1.0 Frameset
HTML 5
HTML
(Alias for HTML 4.01)HTML 4.01
header($args) and footer($args) methods
$html->header()
and $html->footer()
methods exist to open and close the HTML body and are meant only for use in your layout. Both take a single associative array as an argument.Header()
Argument keys are:title
- this is the only required argumentContent-Type
- default is UTF-8metaheader
- this expects an array of key-value pairs and will convert them to meta http-equiv statementsmeta
- same as metaheader except converts to meta name statementsfavicon
- URL of an ICO fileanimatedFavicon
- URL of an animated GIF filecss. js, cssInline, jsInline, cssInlineSingleton, jsInlineSingleton
- abstraction to the helper methods of the same nameshead
- add raw markup into the head section
footer()
The only argument key thatfooter()
recognizes is GoogleAnalytics
- just pass along your Google Analytics tracker ID and your page traffic will be logged by Google Analytics.jsLoad($library, $version = null, array $options = array())
Load a JavaScript library via Google's AJAX API.Version is optional and can be exact (1.8.2) or just version-major (1 or 1.8)
The
$version
and $options
arguments are only used when $library
is a string (not an array.)Syntax is very flexible:
echo $html->jsLoad('jquery');
echo $html->jsLoad(array('yui', 'mootools'));
echo $html->jsLoad(array('yui' => 2.7, 'jquery', 'dojo' => '1.3.1', 'scriptaculous'));
//You can also use the Google API format JSON-decoded in which case the version is required & name must be lowercase
$jsLibs = array(array('name' => 'mootools', 'version' => 1.2, 'base_domain' => 'ditu.google.cn'), array(...));
echo $html->jsLoad($jsLibs);
jsTools($noJsWrapper = false)
Establishes a basic set of JavaScript tools, justecho $html->jsTools()
before any JavaScript code that will use the tools.This method will only operate from the first occurrence in your code, subsequent calls will not output anything but you should add it anyway as it will make sure that your code continues to work if you later remove a previous call to
jsTools()
.Tools provided:
dom()
- method is a direct replacement fordocument.getElementById()
that works in all JS-capable browsers Y2k and newer. Does virtuall the same thing as the$()
in many JavaScript frameworksvork
object - defines a global storage space; use by appending your own properties, eg.:vork.widgetCount
Set
$noJsWrapper
to true
if calling from within a $html->jsInline()
wrapperflash($filename, array $args = array())
Displays a Flash movie using XHTML 1.1 syntax. First argument is the URL of the Flash file, second is an optional associative array containing any or all possible keys:object
- an associative array of properties to add to the "object" HTML containerparams
- an associative array of name/value pairs that will become individual "param" HTML containers.noFlash
- a string (text or HTML) that gets displayed when the user does not have the Flash plugin installed
isEven($number)
Convenience function that returns Boolean, true if the number you send it is even, false if odd.str2ascii($str)
Converts a string to the ASCII equivelent. Note if you echo out the ASCII response in your browser it will get parsed and displayed as the original string (there will be no visible difference except when viewing the source code.)getEmailLink($email)
Returns a spam-resistant email link. Just wrap around each email address, eg.:echo 'Contact me at ' . $html->getEmailLink(') . ' and please include your contact info';
link($href, $text = null, array $args = array())
Returns an accessibility-friendly link.If you have an SSL page, it will prepend the http protocol (and your hostname) to all local URLs that do not include a protocol (http:// or https://), this makes it so that users who follow local non-SSL links from an SSL-enabled page do not stay in SSL mode when they leave the page.
$href
- (Rrequired) relative or absolute URL$text
- optional, if omitted $href
will be used as the $text
$args
- optional, associative array elements become properties of the anchor tag. If you do not include a key for title
(required for W3C WAI AAA accessibility standards) a title will be made automatically for you based on the other data supplied.$args['ajax']
- Set to an ID of an HTML container or a form text element and clicking the link will then load the data into the element via AJAX (if AJAX-capable browser, if not then this will work like an ordinary link.)anchor(array $args)
This is similar tolink()
(above) but provides more fine-grained control; it also does not modify your links when in SSL mode. $args
are the same as link()
plus keys for href
and text
.Use this method to create non-linked anchor tags (page anchors.)
img(array $args)
Returns an XHTML-valid accessibility-friendly image. All$args
become properties of the img tag. An alt
key is optional, but recommended, if omitted this helper will add an empty alt property per the Section 508 accessibility standards for images without contextual purpose.image($src, $width = null, $height = null, $alt = '', $args = array())
Convenience function to simplify access theimg()
helper method. $src
is required, $width, $height
and $alt
are optional as is $args
which gets passed directy to img()
.linkList(array $links, $listType = 'ul')
Takes an associative array of links formated with URL as the key, title as the value and returns an HTML list of links. Valid values for the optional$listType
argument are ul
(bulleted unordered list), dl
(indented definition list) and ol
(numeric ordered list.)Your list values can contain subarrays to generate multi-tiered lists. Tier levels are infinite, you can nest an infinite amount of lists.
crumbs(array $links, $delimiter = ' >> ')
Creates a breadcrumb trail of links like:API >> mvc >> helpers >> html
Each element in $links takes the URL as a key and the label as the value. If you add an element with a dynamically-assigned key (or a numeric key) it will display as plain text instead of as a link. Eg.:
$links['/documentation'] = 'Documentation';
$links['/faq'] = 'Frequently Asked Questions';
$links[] = 'How much wood can a woodchuck chuck?';
$html->crumbs($links)
would return:
<a href="/documentation">Documentation</a> >> <a href="/faq">Frequently Asked Questions</a> >> How much wood can a woodchuck chuck?
alternator()
Simple method that returns an alternating Boolean each time you call it. This is useful to generate alternating background colors within looping structures.$colors = array(true => 'gray', false => 'white');
echo '<div style="background: ' . $colors[$html->alternator()] . ';">...</div>'; //gray background
echo '<div style="background: ' . $colors[$html->alternator()] . ';">...</div>'; //white background
echo '<div style="background: ' . $colors[$html->alternator()] . ';">...</div>'; //gray background