Welcome Guest, Not a member yet? Register   Sign In
[Solved] Where factorize HTML code ?
#1

[eluser]Kromack[/eluser]
Hello everyone,

I'm using CI to remake my own little "CMS", I coded it in a two layers way, just with a template engine, some files likes controllers and libraries that look like models.

I used to call a function in my controllers that produce a box in a string way :

Code:
//We make a long string with all the code HTML of boxes
$box = makeBox($title, $content);
$box .= makeBox($anotherTitle, $anotherContent);

//We inject this code
$template->parse('myBoxes', $box);

I'm a little confused because, with CI i think I should write directly all the HTML code of all my boxes in a view.

Ok, but I have 10 boxes on a same page, it make a big view file and duplicated code... Also, instead of call one function, I have to copy / paste an entire bloc of HTML (corresponding to the box).

So my question is :

How to simplify my boxes management ?

Using an custom helper ? I have tryed but, controllers have to work with HTML (not good for MVC xD...)

Giving data to the views in order to they call an helper (Hum many PHP in views... not good for MVC xD...)

I hope I am clear, thank you for your help Smile
#2

[eluser]xwero[/eluser]
I'm not sure what you exactly want but the way i would do the thing you want is to create 2 views
Code:
<!-- views/box.php -->
<div class="box">
<h1>&lt;?php echo $box->title; ?&gt;</h1>
&lt;?php echo $box->content; ?&gt;
</div>
&lt;!-- views/boxes.php --&gt;
&lt;?php foreach($boxes as $box){ echo $box; } ?&gt;
And in the controller
Code:
$boxes = array();
foreach($query as $row)
{
    $boxes[] = $this->load->view('box',array('box'=>$row),TRUE);
}
$this->load->view('boxes',array('boxes'=>$boxes));
This way the view files can be used for more than only displaying boxes. I hope this helps.
#3

[eluser]Kromack[/eluser]
What an elegant way !

Thank you very much xwero it is exactly the kind of solution I looked for !
#4

[eluser]fouadami[/eluser]
Well yes, the 3rd argument of view method should be mentioned in the user documents section too!
#5

[eluser]Derek Allard[/eluser]
Quote:Well yes, the 3rd argument of view method should be mentioned in the user documents section too!
Its noted on the loader page, but I've just also added it to the view page. I'll be part of the next release.
#6

[eluser]Pascal Kriete[/eluser]
[quote author="Derek Allard" date="1221417381"]I'll be part of the next release.[/quote]
Is this opt-in or opt-out?
#7

[eluser]wiredesignz[/eluser]
[quote author="Derek Allard" date="1221417381"]I'll be part of the next release.[/quote]

Sweet, I always wanted my very own copy of Derek Allard. :lol:
#8

[eluser]Derek Allard[/eluser]
LOL!

I'll be auto-loaded and without the derekallard.php library CI simply won't work!
#9

[eluser]Crimp[/eluser]
Wow, that's Matrix-style dedication: a merger of flesh and framework coming to CI in the new D n' A 2.0 feature.
#10

[eluser]drewbee[/eluser]
[quote author="Crimp" date="1221567067"]Wow, that's Matrix-style dedication: a merger of flesh and framework coming to CI in the new D n' A 2.0 feature.[/quote]


So what your saying is I can dodge bullets or something?

When were finished, you won't have to.

:coolgrin:




Theme © iAndrew 2016 - Forum software by © MyBB