From 8cd54776f129175d48c30d9b549af32375d48979 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Sun, 23 Jan 2011 12:25:52 -0200 Subject: [PATCH] Created the method useTag in html, avoiding sprintf with Html tags in others helpers. --- cake/libs/view/helpers/html.php | 20 +++++++++++++++++++ .../cases/libs/view/helpers/html.test.php | 16 +++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 951cd430c..7e4db5fcb 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -763,6 +763,26 @@ class HtmlHelper extends AppHelper { 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. * diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index 142fe91b5..41d0aed9b 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -1231,6 +1231,22 @@ class HtmlHelperTest extends CakeTestCase { $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, '
'); + + $result = $this->Html->useTag('form', array('test' => 'ok')); + $this->assertTags($result, array('form' => array('test' => 'ok'))); + } + /** * testDiv method *