I've always thought that in typical MVC design it is bad practice to build & concatenate HTML strings in any PHP file or class that isn't strictly a template.
I'm working on a large project with strict guidelines in place, and while it is forbidden to put any HTML code in the PHP controller, it is fair game to have PHP sub-objects that might generate lots of HTML code strings which then get passed into the template.
These "sub-objects" contain additional logic that could change the HTML depending on conditions.
Thus my 2 main questions are:
- Is this considered MVC?
- Are these objects considered part of the View, or are they a frankenstein of the Model and the View?
Example:
<?php
/* controller.php */
class KittenController {
protected $view = 'view.html';
public function render (Request $request) {
$data = $this->getDynamicData(); // get some data
$object = $this->getObject(); // get a PHP object
$this->setField('foo', $data['bar']);
$this->setField('baz', $object->render());
}
}
<?php
/* object.php */
class Object {
public function render () {
$html = '';
$html .= '<div class="my-cool-html">';
if ($this->hasDuck()) {
$html .= '<p>I have a duck...</p>';
}
else {
$html .= '<p>No duck here...</p>';
}
$html .= '</div>';
return $html;
}
}
<!-- an html template -->
<html>
<body>
<div class="blah">
[[foo]]
</div>
<footer>
[[baz]]
</footer>
</body>
</html>