[TYPO3-team-core-v5] RFC: TagBuilder
Bastian Waidelich
bastian at typo3.org
Sun Apr 5 22:09:51 CEST 2009
Martin Kutschker wrote:
Hi Masi,
> Cumbersome.
I agree
'<em>' . $this->value . '</em>';
or
"<em>{$this->value}</em>"
is nicer than
$tag = new Tag('em', $this->value); // or rather some
t3lib_div::makeInstanceClassName hassle
$tag->render();
But I'm especially talking about more complex constructions that can't
be put in one line. Like this one (taken from the select view helper):
$out = '<select name="' . $name . '" ' . $this->renderTagAttributes() . '>';
$selectedValue = $this->getValue();
if ($this->arguments['options']) {
if ($this->arguments['optionKey']) {
foreach ($this->arguments['options'] as $domainObject) {
// [...]
$selected = '';
if ($domainObject == $selectedValue
|| ($this->arguments['multiple'] && in_array($domainObject,
$selectedValue))) {
$selected = 'selected="selected"';
}
// [...]
$out .= '<option ' . $selected . ' value="' . $key . '">' . $value .
'</option>';
}
} else {
foreach ($this->arguments['options'] as $key => $value) {
$selected = '';
if ($key == $selectedValue) {
$selected = 'selected="selected"';
}
$out .= '<option ' . $selected . ' value="' . $key . '">' . $value .
'</option>';
}
}
}
I think, this is quite hard to read, error-prone and difficult to test
(apart from the fact that this code could be refactored anyways).
But it's not only about readability or stability but reusability:
Instead of copying all the tag-creation code from a view helper to
create your custom version, you could extend the view helper and only
modify or add some attributes of the underlying tag:
class LinkViewHelper extends TagBasedViewHelper {
public function render($uri) {
$this->tag->addAttribute('href', $uri);
}
}
class SpeciallinkViewHelper extends LinkViewHelper {
public function render() {
$this->tag->addAttribute('rel', 'I\'m special');
}
}
All the best
Bastian
More information about the TYPO3-team-core-v5
mailing list