Code Separation

#11
[eluser]gtech[/eluser]
take a look at the template parser class, its aim is to remove the need for php in the view files.

[url="http://ellislab.com/codeigniter/user-guide/libraries/parser.html"]http://ellislab.com/codeigniter/user-guide/libraries/parser.html[/url]

You can even use arrays (or variable pairs as they are labeled in the docs) which you build up in the controller, avoiding the need to use a foreach loop in the view. Its simple to use and the documentation is good and to the point.

example from docs:
view:
Code:
<body>
  <h3>{blog_heading}</h3>
  {blog_entries}
    <h5>{title}</h5>
    <p>{body}</p>
  {/blog_entries}
&lt;/body&gt;
controller:
Code:
$this->load->library('parser');
$data = array(
              'blog_heading' => 'My Blog Heading',
              'blog_entries' => array(
                                      array('title' => 'Title 1', 'body' => 'Body 1'),
                                      array('title' => 'Title 2', 'body' => 'Body 2')
                                      )
            );
// instead of using $this->load->view
$this->parser->parse('blog_template', $data);

#12
[eluser]phybertek[/eluser]
Again, thank you. I will take a look at this. Isn't this smarty? Does this disable caching? I wouldn't think so.

Regards,

Pt

#13
[eluser]adwin[/eluser]
No... Parser is different with Smarty.

btw with parser, how to do this things Smile

Code:
Controller:


$smarty = new Smarty;
$smarty->assign('cust_options', array(
            1001 => 'Joe Schmoe',
            1002 => 'Jack Smith',
            1003 => 'Jane Johnson',
            1004 => 'Charlie Brown'));
$smarty->assign('customer_id', 1001);
$smarty->display('index.tpl');


Views in Smarty:
<select name=customer_id>
    {html_options options=$cust_options selected=$customer_id}
</select>

#14
[eluser]cityzen[/eluser]
I would say this all depends on your working environment meaning if you plan on having other developers and designers involved. The more layers you add, (smarty, etc) the more your designer/developer needs to know. I personally work solo on a lot of projects so I keep things streAmlined. The view should have all your front end HTML anyhow, it *is* the presentation layer. It is pretty easy to have a designer build a flat page and throw your php in there. My experience with smarty has been frustrating at best since the syntax is somewhat convoluted. I also don't like how you end up with way too many small files with smarty, seems like one file ends up having 5 or more dependent files and its hard to track down what to edit if you don't know the original developers conventions. My personal goal with CI is to be able to work with designers and other developers, keeping things clean and separate.

Sorry for grammar mistakes, my first long post from the iPhone!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.