Jump to content

decorator


Destramic

Recommended Posts

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;
	}
}
Link to comment
Share on other sites

Kinda depends how you want to model the behavior, whether you'll want to alter the child

 

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

.

#text ->  -> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.