Destramic Posted October 3, 2014 Share Posted October 3, 2014 hey guys...im after a it of advise regarding wrapping html using a decorator...below i have a label class where im able to send attributes to a method and render a <label> tag. but what i'm confused at is the design pattern to wrap the <label> tag... $label = new Label; $label->render(array('for' => 'name', 'label' => 'Destramic')); // returning <label for="name">Destramic</label> but what would be the best way to wrap say a <div> tag around the label?...if anyone could help of have a tutorial on a good way of doing this...thank you <?php namespace Form\View; use HTML\Decorator as Decorator; class Label extends Decorator { protected $_attributes = array('for', 'form'); public function open_tag(array $attributes) { if (array_key_exists('label', $attributes)) { $label = $attributes['label']; } else { $label = " "; } $tag = sprintf("<label %s>%s", $this->create_attributes_string($attributes), $label); return $tag; } public function close_tag() { return "</label>\n"; } public function render(array $attributes) { return $this->open_tag($attributes) . $this->close_tag(); } } decorator <?php namespace HTML; class Decorator { protected $_html; protected $_global_attributes = array('accesskey', 'class', 'contenteditable', 'contenteditable', 'dir', 'draggable', 'dropzone', 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', 'translate'); public static $_placement = "PREPEND"; public function set_html($html) { $this->_html = $html; } public function wrap() { } public function create_attributes_string(array $attributes) { $string = null; $count = count($attributes); $i = 0; foreach ($attributes as $attribute => $value) { $i++; $global_attribute = substr($attribute, 0, 5); if (in_array($attribute, $this->_attributes) || in_array($attribute, $this->_global_attributes) || $global_attribute == "data-") { if ($count == $i) { $string .= $attribute . '=' . '"' . $value . '"'; } else { $string .= $attribute . '=' . '"' . $value . '" '; } } } return $string; } } Quote Link to comment https://forums.phpfreaks.com/topic/291420-decorator/ Share on other sites More sharing options...
requinix Posted October 3, 2014 Share Posted October 3, 2014 Kinda depends how you want to model the behavior, whether you'll want to alter the child later, if these decorators simply pile on more layers of HTML, that kind of thing. If I were doing it I'd probably have a simple class for an HTML element: tag name, arbitrary attributes, and child elements. Then a special sub-type of element representing just text. The "label" would start as the text element, then the Label decorator would take the element received and add it to a new element and return that. The Div decorator would also take the element it receives and wrap it in a new . #text -> #text -> #text Quote Link to comment https://forums.phpfreaks.com/topic/291420-decorator/#findComment-1492644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.