4
\$\begingroup\$

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 ;) .

asked Apr 19, 2013 at 22:59
\$\endgroup\$
5
  • \$\begingroup\$ You should add more details, especially code of your layout class. \$\endgroup\$ Commented Apr 19, 2013 at 23:48
  • \$\begingroup\$ It is done :) . \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented May 17, 2013 at 6:48

1 Answer 1

1
\$\begingroup\$

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.

answered May 14, 2013 at 18:33
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.