Created Html::tag helper which allows you to create any HTML block tag. Html::div now uses this method.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6858 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
joelmoss 2008-05-13 23:04:52 +00:00
parent fb7ac680b0
commit 65454882f3
2 changed files with 38 additions and 10 deletions

View file

@ -81,6 +81,9 @@ class HtmlHelper extends AppHelper {
'block' => '<div%s>%s</div>', 'block' => '<div%s>%s</div>',
'blockstart' => '<div%s>', 'blockstart' => '<div%s>',
'blockend' => '</div>', 'blockend' => '</div>',
'tag' => '<%s%s>%s</%s>',
'tagstart' => '<%s%s>',
'tagend' => '</%s>',
'para' => '<p%s>%s</p>', 'para' => '<p%s>%s</p>',
'parastart' => '<p%s>', 'parastart' => '<p%s>',
'label' => '<label for="%s"%s>%s</label>', 'label' => '<label for="%s"%s>%s</label>',
@ -508,6 +511,27 @@ class HtmlHelper extends AppHelper {
} }
return $this->output(join("\n", $out)); return $this->output(join("\n", $out));
} }
/**
* Returns a formatted block tag, i.e DIV, SPAN, P.
*
* @param string $name Tag name.
* @param string $text String content that will appear inside the div element.
* If null, only a start tag will be printed
* @param array $attributes Additional HTML attributes of the DIV tag
* @param boolean $escape If true, $text will be HTML-escaped
* @return string The formatted tag element
*/
function tag($name, $text = null, $attributes = array(), $escape = false) {
if ($escape) {
$text = h($text);
}
if ($text === null) {
$tag = 'tagstart';
} else {
$tag = 'tag';
}
return $this->output(sprintf($this->tags[$tag], $name, $this->_parseAttributes($attributes, null, ' ', ''), $text, $name));
}
/** /**
* Returns a formatted DIV tag for HTML FORMs. * Returns a formatted DIV tag for HTML FORMs.
* *
@ -519,18 +543,10 @@ class HtmlHelper extends AppHelper {
* @return string The formatted DIV element * @return string The formatted DIV element
*/ */
function div($class = null, $text = null, $attributes = array(), $escape = false) { function div($class = null, $text = null, $attributes = array(), $escape = false) {
if ($escape) {
$text = h($text);
}
if ($class != null && !empty($class)) { if ($class != null && !empty($class)) {
$attributes['class'] = $class; $attributes['class'] = $class;
} }
if ($text === null) { return $this->tag('div', $text, $attributes, $escape);
$tag = 'blockstart';
} else {
$tag = 'block';
}
return $this->output(sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $text));
} }
/** /**
* Returns a formatted P tag. * Returns a formatted P tag.
@ -747,7 +763,7 @@ class HtmlHelper extends AppHelper {
* @see Helper::value * @see Helper::value
*/ */
function tagValue($fieldName) { function tagValue($fieldName) {
trigger_error(sprintf(__('Method tagValue() is deprecated in %s: see Helper::value', true), get_class($this)), E_USER_NOTICE); trigger_error(sprintf(__('Method tagValue() is deprecated in %s: see Helper::value', true), get_class($this)), E_USER_NOTICE);
$this->setEntity($fieldName); $this->setEntity($fieldName);
if (isset($this->data[$this->model()][$this->field()])) { if (isset($this->data[$this->model()][$this->field()])) {
return h($this->data[$this->model()][$this->field()]); return h($this->data[$this->model()][$this->field()]);

View file

@ -641,6 +641,18 @@ class HtmlHelperTest extends CakeTestCase {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
} }
function testTag()
{
$result = $this->Html->tag('div');
$this->assertTags($result, 'div');
$result = $this->Html->tag('div', 'text');
$this->assertTags($result, 'div', 'text', '/div');
$result = $this->Html->tag('div', '<text>', array('class' => 'class-name'), true);
$this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
}
function testDiv() { function testDiv() {
$result = $this->Html->div('class-name'); $result = $this->Html->div('class-name');
$this->assertTags($result, array('div' => array('class' => 'class-name'))); $this->assertTags($result, array('div' => array('class' => 'class-name')));