Created the method useTag in html, avoiding sprintf with Html tags in others helpers.

This commit is contained in:
Juan Basso 2011-01-23 12:25:52 -02:00
parent 41e1aa7ece
commit 8cd54776f1
2 changed files with 36 additions and 0 deletions

View file

@ -763,6 +763,26 @@ class HtmlHelper extends AppHelper {
return sprintf($this->tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name); return sprintf($this->tags[$tag], $name, $this->_parseAttributes($options, null, ' ', ''), $text, $name);
} }
/**
* Returns a formatted existent block of $tags
*
* @param string $tag Tag name
* @return string Formatted block
*/
public function useTag($tag) {
if (!isset($this->tags[$tag])) {
return '';
}
$args = func_get_args();
array_shift($args);
foreach ($args as &$arg) {
if (is_array($arg)) {
$arg = $this->_parseAttributes($arg, null, ' ', '');
}
}
return vsprintf($this->tags[$tag], $args);
}
/** /**
* Returns a formatted DIV tag for HTML FORMs. * Returns a formatted DIV tag for HTML FORMs.
* *

View file

@ -1231,6 +1231,22 @@ class HtmlHelperTest extends CakeTestCase {
$this->assertTags($result, array('div' => array('class' => 'class-name'), '<text>', '/div')); $this->assertTags($result, array('div' => array('class' => 'class-name'), '<text>', '/div'));
} }
/**
* testUseTag method
*
* @return void
*/
public function testUseTag() {
$result = $this->Html->useTag('formend');
$this->assertTags($result, '/form');
$result = $this->Html->useTag('form', 'test');
$this->assertEqual($result, '<form test>');
$result = $this->Html->useTag('form', array('test' => 'ok'));
$this->assertTags($result, array('form' => array('test' => 'ok')));
}
/** /**
* testDiv method * testDiv method
* *