Skip to content

Instantly share code, notes, and snippets.

@zoghal
Last active December 25, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoghal/83d0c216c10fbaa8999b to your computer and use it in GitHub Desktop.
Save zoghal/83d0c216c10fbaa8999b to your computer and use it in GitHub Desktop.
HtmlBuilder
public function button($title, $options = array()) {
$options += array('type' => 'submit', 'escape' => false, 'secure' => false);
if ($options['escape']) {
$title = h($title);
}
if (isset($options['name'])) {
$name = str_replace(array('[', ']'), array('.', ''), $options['name']);
$this->_secure($options['secure'], $name);
}
return Html::el('button', $options)->setText($title);
}
//extend buttup
public function buttonBootstrap($title, $options = array()) {
$btt = $this->button($title, $options);
$btt->class('bootBtt');
return $btt;
}
<?php
/**
* Html
*
*
* @package Advenced Html Helper For CakePHP 2
* @author Saleh Souzanchi<info@soozanchi.ir
* @copyright Copyright (c) 2011 Saleh Souzanchi(http://soozanchi.ir)
* @version 2011
* @access public
*/
class Html implements ArrayAccess, Countable, IteratorAggregate {
private $name;
private $isEmpty;
public $attrs = array();
protected $children = array();
public static $xhtml = TRUE;
public static $emptyElements = array('img'=>1,'hr'=>1,'br'=>1,'input'=>1,'meta'=>1,'area'=>1,'embed'=>1,'keygen'=>1,
'source'=>1,'base'=>1,'col'=>1,'link'=>1,'param'=>1,'basefont'=>1,'frame'=>1,'isindex'=>1,'wbr'=>1,'command'=>1);
/**
* Html::el()
* Factory Class
* @param mixed $name
* @param mixed $attrs
* @return
*/
public static function el($name = null ,$attrs = null ){
$el = new self;
$parts = explode(' ', $name, 2);
$el->setName($parts[0]);
if (is_array($attrs)) {
$el->attrs = $attrs;
} elseif ($attrs !== NULL) {
$el->setText($attrs);
}
if (isset($parts[1])) {
foreach ( self::matchAll($parts[1] . ' ', '#([a-z0-9:-]+)(?:=(["\'])?(.*?)(?(2)\\2|\s))?#i') as $m) {
$el->attrs[$m[1]] = isset($m[3]) ? $m[3] : TRUE;
}
}
return $el;
}
/**
* Html::match()
*
* @param mixed $subject
* @param mixed $pattern
* @param integer $flags
* @param integer $offset
* @return
*/
private static function match($subject, $pattern, $flags = 0, $offset = 0)
{
if ($offset > strlen($subject)) {
return NULL;
}
$res = preg_match($pattern, $subject, $m, $flags, $offset);
if ($res) {
return $m;
}
}
/**
* Html::matchAll()
*
* @param mixed $subject
* @param mixed $pattern
* @param integer $flags
* @param integer $offset
* @return
*/
private static function matchAll($subject, $pattern, $flags = 0, $offset = 0)
{
if ($offset > strlen($subject)) {
return array();
}
$res = preg_match_all(
$pattern, $subject, $m,
($flags & PREG_PATTERN_ORDER) ? $flags : ($flags | PREG_SET_ORDER),
$offset
);
return $m;
}
/**
* Html::setName()
*
* @param mixed $name
* @return
*/
public function setName($name = null ) {
if ( $name !== null && !is_string($name) ) {
trigger_error('string nist' , E_USER_ERROR);
}
$this->name = $name;
$this->isEmpty = isset(self::$emptyElements[$name]);
return $this;
}
/**
* Html::getName()
*
* @return
*/
public function getName()
{
return $this->name;
}
/**
* Html::isEmpty()
*
* @return
*/
public function isEmpty()
{
return $this->isEmpty;
}
/**
* Html::addAttributes()
*
* @param mixed $attrs
* @return
*/
public function addAttributes(array $attrs)
{
$this->attrs = $attrs + $this->attrs;
return $this;
}
/**
* Html::__set()
*
* @param mixed $name
* @param mixed $value
* @return
*/
public function __set($name, $value)
{
$this->attrs[$name] = $value;
}
/**
* Html::__get()
*
* @param mixed $name
* @return
*/
public function &__get($name)
{
return $this->attrs[$name];
}
/**
* Html::__unset()
*
* @param mixed $name
* @return
*/
public function __unset($name)
{
unset($this->attrs[$name]);
}
/**
* Html::__call()
*
* @param mixed $method
* @param mixed $args
* @return
*/
public function __call($method , $args)
{
$prefixMethod = substr($method, 0, 3);
if ($prefixMethod === 'get' || $prefixMethod === 'set' || $prefixMethod === 'add') {
$method = substr($method, 3);
$method[0] = $method[0] | "\x20";
if ($prefixMethod === 'get') {
return isset($this->attrs[$method]) ? $this->attrs[$method] : NULL;
} elseif ($prefixMethod === 'add') {
$args[] = TRUE;
}
}
if (count($args) === 0) {
} elseif (count($args) === 1) {
$this->attrs[$method] = $args[0];
} elseif ((string) $args[0] === '') {
$tmp = & $this->attrs[$method];
} elseif (!isset($this->attrs[$method]) || is_array($this->attrs[$method])) {
$this->attrs[$method][$args[0]] = $args[1];
} else {
$this->attrs[$method] = array($this->attrs[$method], $args[0] => $args[1]);
}
return $this;
}
/**
* Html::href()
*
* @param mixed $url
* @param bool $full
* @return
*/
public function href( $url = null, $full = false )
{
$this->attrs['href'] = h(Router::url($url, $full));
return $this;
}
/**
* Html::setHtml()
*
* @param mixed $html
* @return
*/
public function setHtml($html)
{
if ($html === NULL) {
$html = '';
} elseif (is_array($html)) {
trigger_error("Textual content must be a scalar, " . gettype($html) ." given." , E_USER_ERROR);
} else {
$html = (string) $html;
}
$this->removeChildren();
$this->children[] = $html;
return $this;
}
/**
* Html::getHtml()
*
* @return
*/
public function getHtml()
{
$s = '';
foreach ($this->children as $child) {
if (is_object($child)) {
$s .= $child->render();
} else {
$s .= $child;
}
}
return $s;
}
/**
* Html::setText()
*
* @param mixed $text
* @return
*/
public function setText($text)
{
if (!is_array($text)) {
$text = h($text);
}
return $this->setHtml($text);
}
/**
* Html::getText()
*
* @return
*/
public function getText()
{
return html_entity_decode(strip_tags($this->getHtml()), ENT_QUOTES, 'UTF-8');
}
/**
* Html::add()
*
* @param mixed $child
* @return
*/
public function add($child)
{
return $this->insert(NULL, $child);
}
/**
* Html::create()
*
* @param mixed $name
* @param mixed $attrs
* @return
*/
public function create($name, $attrs = NULL)
{
$this->insert(NULL, $child = self::el($name, $attrs));
return $child;
}
/**
* Html::insert()
*
* @param mixed $index
* @param mixed $child
* @param bool $replace
* @return
*/
public function insert($index, $child, $replace = FALSE)
{
if ($child instanceof Html || is_scalar($child)) {
if ($index === NULL) {
$this->children[] = $child;
} else {
array_splice($this->children, (int) $index, $replace ? 1 : 0, array($child));
}
} else {
trigger_error("Child node must be scalar or Html object, " . (is_object($child) ? get_class($child) : gettype($child)) ." given." , E_USER_ERROR);
}
return $this;
}
/**
* Html::offsetSet()
*
* @param mixed $index
* @param mixed $child
* @return
*/
public function offsetSet($index, $child)
{
$this->insert($index, $child, TRUE);
}
/**
* Html::offsetGet()
*
* @param mixed $index
* @return
*/
public function offsetGet($index)
{
return $this->children[$index];
}
/**
* Html::offsetExists()
*
* @param mixed $index
* @return
*/
public function offsetExists($index)
{
return isset($this->children[$index]);
}
/**
* Html::offsetUnset()
*
* @param mixed $index
* @return
*/
public function offsetUnset($index)
{
if (isset($this->children[$index])) {
array_splice($this->children, (int) $index, 1);
}
}
/**
* Html::count()
*
* @return
*/
public function count()
{
return count($this->children);
}
/**
* Html::removeChildren()
*
* @return
*/
public function removeChildren()
{
$this->children = array();
}
/**
* Html::getIterator()
*
* @param bool $deep
* @return
*/
public function getIterator($deep = FALSE)
{
if ($deep) {
$deep = $deep > 0 ? RecursiveIteratorIterator::SELF_FIRST : RecursiveIteratorIterator::CHILD_FIRST;
return new RecursiveIteratorIterator(new RecursiveIterator(new ArrayIterator($this->children)), $deep);
} else {
return new RecursiveIterator(new ArrayIterator($this->children));
}
}
/**
* Html::hasChildren()
*
* @return
*/
public function hasChildren()
{
$obj = $this->current();
return ($obj instanceof IteratorAggregate && $obj->getIterator() instanceof RecursiveIterator)
|| $obj instanceof RecursiveIterator;
}
/**
* Html::getChildren()
*
* @return
*/
public function getChildren()
{
return $this->children;
}
/**
* Html::render()
*
* @param mixed $indent
* @return
*/
public function render($indent = NULL)
{
$s = $this->startTag();
if (!$this->isEmpty) {
if ($indent !== NULL) {
$indent++;
}
foreach ($this->children as $child) {
if (is_object($child)) {
$s .= $child->render($indent);
} else {
$s .= $child;
}
}
$s .= $this->endTag();
}
if ($indent !== NULL) {
return "\n" . str_repeat("\t", $indent - 1) . $s . "\n" . str_repeat("\t", max(0, $indent - 2));
}
return $s;
}
/**
* Html::__toString()
*
* @return
*/
public function __toString()
{
return $this->render();
}
/**
* Html::startTag()
*
* @return
*/
public function startTag()
{
if ($this->name) {
return '<' . $this->name . $this->attributes() . (self::$xhtml && $this->isEmpty ? ' />' : '>');
} else {
return '';
}
}
/**
* Html::endTag()
*
* @return
*/
public function endTag()
{
return $this->name && !$this->isEmpty ? '</' . $this->name . '>' : '';
}
/**
* Html::attributes()
*
* @return
*/
public function attributes()
{
if (!is_array($this->attrs)) {
return '';
}
$s = '';
foreach ($this->attrs as $key => $value) {
if ($value === NULL || $value === FALSE) {
continue;
} elseif ($value === TRUE) {
if (self::$xhtml) {
$s .= ' ' . $key . '="' . $key . '"';
} else {
$s .= ' ' . $key;
}
continue;
} elseif (is_array($value)) {
if ($key === 'data') {
foreach ($value as $k => $v) {
if ($v !== NULL && $v !== FALSE) {
$s .= ' data-' . $k . '="' . h((string) $v) . '"';
}
}
continue;
}
$tmp = NULL;
foreach ($value as $k => $v) {
if ($v != NULL) { // intentionally ==, skip NULLs & empty string
// composite 'style' vs. 'others'
$tmp[] = $v === TRUE ? $k : (is_string($k) ? $k . ':' . $v : $v);
}
}
if ($tmp === NULL) {
continue;
}
$value = implode($key === 'style' || !strncmp($key, 'on', 2) ? ';' : ' ', $tmp);
} else {
$value = (string) $value;
}
$s .= ' ' . $key . '="' . h($value) . '"';
}
$s = str_replace('@', '&#64;', $s);
return $s;
}
/**
* Html::__clone()
*
* @return
*/
public function __clone()
{
foreach ($this->children as $key => $value) {
if (is_object($value)) {
$this->children[$key] = clone $value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment