Welcome Guest, Not a member yet? Register   Sign In
newbie question: using templates
#1

[eluser]aligator[/eluser]
Hello,
I started to read some codeIgniter tutorials and try some code in a new project, to learn.
I am using templates to create a view. I mean i have separate php files for each component of a page(view): header, footer, left panel etc.
I stumbled upon this problem:
1. this is the view:
Code:
<!-- left panel quote box -->            
<?php $this->load->view('includes/quoteBox'); ?>
// quoteBox is a php file that has some html code for displaying

2. in the controller i have this:
Code:
$data['quote'] = $this->leftpanel_model->getLeftQuoteBox();
// the left panel exists and so the function. this function just retrives the content of a column in a table. Previous actions of getting data from db worked, so the connection or db stuff is not a problem.

3. in the view situated in the view folder: 'includes/quoteBox.php' has this code:
Code:
<?php
                    echo '<p>'.''.$quote.'</p>';
                ?&gt;



The flow is this: from controller send variable to the view. this view has that html part added dynamically, the code is in another php file.


My questions are these:
a) is a variable usable if is in another php file that is included in a view?
b) in the application/autoload.php i added the neccesary model. dunno why i need to do this? somebody suggested to add all models there so i can use them in a controller. can't i use multiple models in a controller without declaring them in the autoload.php file?

thank you, even if u took the time to read till here.
#2

[eluser]Tominator[/eluser]
Hi there!

First I have to note, that I never meet this problem ... but I think that problem is because you didn't passed any data into your quoteBox view file. You should try something like:

Code:
&lt;?php $this->load->view('includes/quoteBox', array('quote' => $quote)); ?&gt;

Maybe help.

But I think this is so complicated solution of template problem. Because what is template?
CodeIgniter is framework based on MVC - that means your PHP files are devided in 3 categories by their role. View files are just PHP files, which contains HTML code. But in my opinion templates seperates HTML and PHP.

Wrong template system (engine, parser or whatever you call it) makes your application so complicated, because of too much separating in wrong layers.

I made my own template parser, inspirated by phpBB template parser, called COMPER Template Parser (new version soon!), which separate PHP and HTML and provide cycling, conditioning and separating in template files without PHP. You won't have the same problem when you use it.

Tom.
#3

[eluser]techgnome[/eluser]
a) There seems to be a mixed opinion on this... I've seen people insist that if you call a view from another view, you need to re-pass the data through to it... my own experience has shown this to not be true. So I'm not sure what the deal is.

b) If you have models that you use on every single page, those are the ones you should autoload. This way you don't have to load it yourself each and every time in your controllers. And, yes you cna use multiple models in a controller with out autoloading them, but like I said, auto,oading them is simply a short cut if you have models that you use on every page.

-tg
#4

[eluser]Dennis Rasmussen[/eluser]
In your controller use this:
Code:
$this->load->vars($data);
And don't add $data to your $this->load-view('file');
Then you can use the $data in all of your views and sub-views... even in your models/libraries/helpers and extended controllers as well.
#5

[eluser]aligator[/eluser]
Hello,
Thx for all replies. i took all the steps from the begining and found my mistake. In autoload.php file i added to the array of models something stupid: "includes/" thinking of the templates. i deleted this row and it worked.

Data was passed in both situations(one or another):
Code:
&lt;?php $this->load->view('includes/quoteBox'); ?&gt;
&lt;?php $this->load->view('includes/quoteBox', array('quote' => $quote)); ?&gt;

Thank you for the tips, will take them in account. Smile




Theme © iAndrew 2016 - Forum software by © MyBB