[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Простой хтмлген
Страницы: 1, 2, 3, 4, 5, 6, 7
dr.nomore
Полезен при необходимости сгенерить кучу хтмл-кода в циклах, с множеством условий и тп, что не типично-канонично, но иногда очень надо.

Уже третий мой подход к решению, надеюсь последний.

Устройство:

Свернутый текст

<?php
class
CTag {

private $_tag;
private $_id = 0;
private $_ob = 0;

private static $void=array(
'area','base','br','col',
'command','embed','hr','img',
'input','keygen','link','meta',
'param','source','track','wbr');

public function __construct($tag, $value = null) {
$this->_tag = array();
$this->att($tag, $value);
}

// new tag in collection @link->add
// return $this

public function add($name, $value = null) {
$this->_id++;
return $this->att($name, $value);
}

// attribute: @link->att(name, value)
public function att($name, $value = null) {
return $this->__call($name, array($value));
}

// attribute: @link->name(value)
public function __call($name, $args = null) {
$value = @$args[0];
if(isset($this->_tag[$this->_id][$name]) && $value !== null)
$this->_tag[$this->_id][$name] .= (' ' . $value);
else
$this->_tag[$this->_id][$name] = $value;
return $this;
}

// attach new tag to tag @link->ins(tag, node)
public function ins($name, $value = null) {
return $this->__invoke($name, $value);
}

// attach new tag to tag @link(tag, node)
public function __invoke($name, $value = null) {
$class_name = get_class();
return $this->_tag[$this->_id][$this->_ob++] = new $class_name($name, $value);
}

public function __toString() {

$echo = array();

foreach($this->_tag as $tag) {

$index = 0;

foreach($tag as $name => $value) {

if($index++ == 0) {

$s = '<' . $name;
$v = array();

if(in_array(strtolower($name), self::$void)) {
$c = ' />';
$g = null;
}
else {
$c = '</' . $name . '>';
$g = '>';
$v[] = $value;
}
}

elseif(is_numeric($name))
$v[] = $value;
elseif($value !== null)
$s .= ' ' . $name . '="' . $value . '"';

}

$echo[] = $s . $g . join(null, $v) . $c . PHP_EOL;

}

return join(null, $echo);

}

}




Юзанье

$form = new CTag('form');
$form->name('data')
->
action('index.php')
->
att('accept-charset', 'UTF-8')
->
method('post')
->
style('border:1px solid red;')
->
ins('input')
->
type('text')
->
name('login')
->
style('border: 1px solid grey;')
->
style('background-color: yellow;')
->
placeholder('type login')
->
maxlength(64)
->
add('input')
->
type('submit')
->
name('submit')
->
value('Subscribe');

echo $form;


Результ

<form name="data" action="index.php" accept-charset="UTF-8" method="post" style="border:1px solid red;">
<input
type="text" name="login" style="border: 1px solid grey; background-color: yellow;" placeholder="type login" maxlength="64" />
<input
type="submit" name="submit" value="Subscribe" />
</form>

Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.