Welcome Guest, Not a member yet? Register   Sign In
Best way to avoid duplicating html in views
#11

[eluser]Polarity[/eluser]
[quote author="ericsodt" date="1229667867"]You said:
[quote author="Polarity" date="1229665461"] ..(updating only the content, would be a problem because you call a view inside the view.)...[/quote]


This is not true, you can instantiate a view and pass in true as the third argument to get the content returned. This is great for ajax calls. You can take the returned content and shove it in a <div>'s innerHTML property.

[/quote]
yea i know. thats what i used if you look at my example. but if you load a view inside a controller, and that view loads also a view, how would you seperate between a ajax call and a complete site reload with only one view file?

[quote author="ericsodt" date="1229667867"]

Secondly, I don't know why you think having a call to render a view inside a "template view" is bad coding practice. Yes, it says within the class page that views should be called from controllers, but you are. You're telling your template class to load a particular view. If you think of your view as a template then your really just calling a view from a "middle-end" controller. This may shed some light on what I mean and how it is used in the Java realm.
[/quote]
what if you want to fill complex data in the sidebar section on every site? lets say you want on every page a list of recent articles. you have to call on every controller function the model and pass the data to your view and then to the sidebar view. so you need on 10 sites 10 times the same call with the same code. you can avoid this only if you make a model call directly in the view file (whats a bad practice) or make it this way i described.
#12

[eluser]mortifrog[/eluser]
lol - thank you all - I know you all mean well, but your talk of template engines and helpers is melting my brain! (I haven't fully taken on board the purpose of helpers yet) I'm really, really new to OO and CI (as well as being O,S & W as opposed to Y,G & B) so I have a need to keep things really, really simple! I am re-reading this thread and am slowly taking on board some of the stuff that's being said. It's one thing reading the user guide and viewing the tutorials, but for me, it doesn't sick in my head until I start doing it.
For now anyway, I've decided on the template folder approach, as I mean to have interchangable themes. Which I guess means, all my controllers will contain something like the following code.

Code:
&lt;?php
class Modulename extends Controller {
    

    function Modulename()
    {
        parent::Controller();    
    }
    
    function index()
    {    
        $data['page_title'] = 'Site Name - Module Name';
        $data['template'] = 'default';
        $data['masthead'] = $this->load->view("templates/$data['template']/masthead");
        $data['nav'] = $this->load->view("templates/$data['template']/nav");
        $data['content'] = $this->result_of_some_number_crunching_maybe;
        $data['content'] .= $this->load->view('an_extra_view');
        $data['content'] .= $this->load->view('another_view_perhaps');
        $data['footer'] = $this->load->view("templates/$data['template']/footer");
        $this->load->view("templates/$data['template']/index", $data);
    }
} ?&gt;

( or should those references read $this->data['template'] ? )

and my template view will be something like:

Code:
&lt;html
&lt;head>
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;title&gt;&lt;?php echo $page_title; ?&gt;&lt;/title&gt;
&lt;link href="http://&lt;?php echo $_SERVER["HTTP_HOST"]?&gt;/system/application/views/templates/&lt;?php echo $template; ?&gt;/style.css" rel="stylesheet" type="text/css" /&gt;
&lt;/head&gt;

&lt;body&gt;
<table class="layout"">
   <tr><td colspan="2">&lt;? php echo $masthead; ?&gt;</td></tr>
   <tr><td>&lt;? php echo $nav; ?&gt;</td><td>&lt;? php echo $content; ?&gt;</td></tr>
   <tr><td colspan="2">&lt;? php echo $footer; ?&gt;</td></tr>
</table>
&lt;/body&gt;&lt;/html>


Thanks again for all your input, it has been most instructive and has helped me to suss out how I'm going to approach this issue.

One last thing on architecture. I tried doing something like:
Code:
$data['content'] = $this->load->controller('some_controller');
- but it didn't work. I thought it would be really useful to be able to load the output of a controller into a string which is in turn passed to a view - but I reckon you all have a better way of doing the same thing. Perhaps I just include and instantiate the other controller?
#13

[eluser]Polarity[/eluser]
Code:
$data['content'] = $this->load->controller('some_controller',NULL,TRUE);
the third parameter "true" means .. return a string.

edit: sorry, that was wrong. thougt it was a view load.. ;D
#14

[eluser]Polarity[/eluser]
ok, one step after another
your code:
Code:
&lt;?php
class Modulename extends Controller {
    

    function Modulename()
    {
        parent::Controller();    
    }
    
    function index()
    {    
        $data['page_title'] = 'Site Name - Module Name';
        $data['template'] = 'default';
        $data['masthead'] = $this->load->view("templates/$data['template']/masthead");
        $data['nav'] = $this->load->view("templates/$data['template']/nav");
        $data['content'] = $this->result_of_some_number_crunching_maybe;
        $data['content'] .= $this->load->view('an_extra_view');
        $data['content'] .= $this->load->view('another_view_perhaps');
        $data['footer'] = $this->load->view("templates/$data['template']/footer");
        $this->load->view("templates/$data['template']/index", $data);
    }
} ?&gt;

your code with corrected syntax

Code:
&lt;?php
class Modulename extends Controller {
    

    function Modulename()
    {
        parent::Controller();    
    }
    
    function index()
    {    
        $data['page_title'] = 'Site Name - Module Name';
        $data['template'] = 'default';
        $data['masthead'] = $this->load->view("templates/".$data['template']."/masthead");
        $data['nav'] = $this->load->view("templates/".$data['template']."/nav");
        $data['content'] = $this->result_of_some_number_crunching_maybe;
        $data['content'] .= $this->load->view('an_extra_view');
        $data['content'] .= $this->load->view('another_view_perhaps');
        $data['footer'] = $this->load->view("templates/".$data['template']."/footer");
        $this->load->view("templates/".$data['template']."/index", $data);
    }
} ?&gt;
#15

[eluser]mortifrog[/eluser]
That gave me an error of

Fatal error: Call to undefined method CI_Loader::controller() in ...
#16

[eluser]mortifrog[/eluser]
Sorry that last reply was to the post you made about loading controllers from controllers.

I see where you have corected my syntax, you have escaped the strings to insert the variables. I thought that if you enclosed the string with double quotes in PHP it's supposed to interpolate the variables contained therein? Funnily enough I did it the way you did it at first - then changed it - because I was self consious about my code! ha! I must admit, I do usually escape strings to insert variables simply because it's more readable to me in a code editor.
#17

[eluser]Polarity[/eluser]
maybe $this->result_of_some_number_crunching_maybe doesnt exists ....
#18

[eluser]mortifrog[/eluser]
that's not what I wrote in my actual code - I called a real controller that I created.
#19

[eluser]Polarity[/eluser]
[quote author="mortifrog" date="1229675191"]Sorry that last reply was to the post you made about loading controllers from controllers..[/quote] sorry, i thougt it was a view load
#20

[eluser]mortifrog[/eluser]
Yes we ended up at cross purposes because we were both chatting too fast!

trying to do

Code:
$data['content'] = $this->load->controller('quatermass2',NULL,TRUE);

gives me Fatal error: Call to undefined method CI_Loader::controller() in ...


Anyhow Polarity - It's late and I've got to get up fairly early so I'm going call it night for tonight. It's been great chatting about all this stuff - it's helped me straighten out a few things in my head about CI. I really want to put the effort in and get good at it - and I feel I've made a little progress today, thanks to this forum. Thanks for all your help. I'll tackle the controller from controllers issue tomorrow.




Theme © iAndrew 2016 - Forum software by © MyBB