Dynamical content loading question |
[eluser]felyx[/eluser]
Well I am not used to frameworks as I just going to start working with CI after a couple of weeks of research on frameworks. I find CI very very promising. However I am very new to MVC approach aswell. My problem is that I had my own "system" which I was developing by. You might find it a bad way of doing things but still it worked for me. As you can see below in the code I had several (usually 2-4 part depending on the website's needs) dynamically loaded part of the page which was handled by my index.php (the code seen below). In the example below the website had a dynamic head part, a left and a right side. I can put there php code or html or both doesn't matter. I can even say that a page does not need to have different head section then the default so it will load the default if it doesn't find a specified one. example: http://www.somedomain.com/home -> will load home.php, home_head.php and home_right.php http://www.somedomain.com/categories -> will load categories.php, categories_head.php and categories_right.php http://www.somedomain.com/about -> will load about.php, home_head.php and home_right.php My problem is that I do not know which one is the best (easiest) way of getting this work in a CI project. I read about extending controllers and that I can load views from within another views but I am not sure it will work how I want it to. If you could please help me I would be very grateful. Basic example of my index.php: Code: <?
[eluser]Popcorn[/eluser]
The code you were using originally isn't great practice, what if you want to change a little html in the header? You have to change 4 files instead of 1. I'd use xwero's method and use a base template.
[eluser]cereal[/eluser]
I use a similar approach, without templates: Code: function index()
[eluser]felyx[/eluser]
Thank you for your answer, but I am not sure I understand what you mean by a "base template". Anyways I do not want to use templates. If I start using tpl engines which I actually don't need then it would further slow down the websites I build which I could avoid.
[eluser]xwero[/eluser]
The code you showed in your post is the base template, maybe you have another name for it like master page or something like it. I use the name template because you can output html, xml, json all from the same data.
[eluser]felyx[/eluser]
So you are suggesting that I make a main view file like a skeleton of the page and for the dynamic parts of the page I should load different views depending on what page of my website it is. I got one question, if I have php code which generates something inside one of the dynamic parts where should I place it? Just inside the dynamically loaded view file? Looks a bit messy to me. As I mentioned it before, sometimes one of my "heading" files have php inside them generating meta data and sometimes I just place static html (meta, title etc..) for a specific page, depends on what I need to achieve there. Same goes for the other dynamic parts. That is my problem at this point. Another problem which does not really belong here but sometimes a project requires me to add a lot of pages which only contain html code. What I mean is that a couple pages require php which I execute in the controller then call the view file but sometimes I need to add 30+ pages which only load a view file. Can I avoid somehow the need of using controllers in these cases? Sounds weird I know but doing the same for all pages (loading a view file only) feels a bit complicated since with the previous "system" I had I could do it with one line of php code.
[eluser]xwero[/eluser]
For your first question you can load it in the controller. For example load a partial page in the header. Code: // views/header.php For your second problem you can create a controller named static with a html method. Route all the static pages to that controller and method and change the post_controller function to Code: function post_controller()
[eluser]felyx[/eluser]
xwero thanks for the answer again I will try what you have suggested. |
Welcome Guest, Not a member yet? Register Sign In |