I had a code for a layout library which looked like :
$this->layout->setLayout($layout)->setTitle($title)->render()
etc.
Today I started writing a new code, simpler and smaller :
$this->layout->data(array(
'view_name' => 'layout_admin',
'title' => 'Administration',
'js' => 'admin/matrix.chat'
))->view('admin/index');
The previous setters/add methods were like :
public function setDoctype($doctype) {
$this->doctype = $doctype;
return $this;
}
public function addJs($name) {
if(!in_array($name, $this->js)) {
$this->js[] = $name;
}
return $this;
}
The new data method :
public function data($data = array()) {
if(isset($data['view_name'])) $this->ci->config->load($data['view_name']);
else $this->ci->config->load($this->data['view_name']);
// Mark as forbid because are stored in arrays and
// I can add values to those
$forbid = array('css', 'js', 'rss');
foreach($this->ci->config->config['layout'] as $key => $value) {
if(isset($data[$key]) && !in_array($key, $forbid))
$this->data[$key] = $data[$key];
else
$this->data[$key] = $value;
if(isset($data[$key]) && in_array($key, $forbid)) {
if(is_array($data[$key])) {
if(count($array) != count($array, COUNT_RECURSIVE))
foreach($data[$key] as $key2 => $value2)
$this->data[$key][$key2] = $value2;
else
foreach($data[$key] as $value2)
$this->data[$key][] = $value2;
} else
$this->data[$key][] = $data[$key];
}
}
return $this;
}
I changed to this because of the many things that I can update for every page. And use ->setThing would be annoying. But I don't find that my code is so OOP.
Note that I'm using CodeIgniter.
What's the best oop code ? If you want more indications, please tell me I'm new and don't know how to say more ;) .
-
\$\begingroup\$ You should add more details, especially code of your layout class. \$\endgroup\$Sven– Sven2013年04月19日 23:48:01 +00:00Commented Apr 19, 2013 at 23:48
-
\$\begingroup\$ It is done :) . \$\endgroup\$Madnx– Madnx2013年04月20日 00:21:45 +00:00Commented Apr 20, 2013 at 0:21
-
\$\begingroup\$ I like ->setThing, because i can use type ahead, also you can easily make a backtrace in the function and you have only backtrace for the manipulations of Thing and not for every other data in your class. \$\endgroup\$AlucardTheRipper– AlucardTheRipper2013年04月20日 12:16:07 +00:00Commented Apr 20, 2013 at 12:16
-
\$\begingroup\$ Your paths should start with '/', i.e /admin/index. So that they start at the root of the domain (especially if using mod_rewrite) \$\endgroup\$Eddie Jaoude– Eddie Jaoude2013年05月13日 16:51:45 +00:00Commented May 13, 2013 at 16:51
-
\$\begingroup\$ As John said, it's very effective. It's common practice in many frameworks... and in Code Igniter itself! \$\endgroup\$Quentin Pradet– Quentin Pradet2013年05月17日 06:48:28 +00:00Commented May 17, 2013 at 6:48
1 Answer 1
The core of your question is whether or not you should use setters ($tpl->setSomeParam()) or a simple array to bind template parameters. While using an array my not seem very OO, it's actually a pretty effective way of decoupling your data from your templates. As you've discovered, having to add a new setter to your layout manager every time you add a new parameter to one of your templates means the layout manager is tightly coupled to all of your templates – not good.
This is pretty common practice these days. Twig, Underscore.js, and many others do the same thing.