Quote:
| Originally Posted by Jacques1 Hi,I don't know what you mean by "global" in this context. A variable you pass to $smarty->assign() will be available in any template you display or fetch with $smarty. |
Hello Jaques1. Thank you for your reply. I am gonna try really hard to explain this as clear as possible.
Lets say I have a simple page that has 2 sections:
1 - Header that contains a dynamic drop down menu list of cities
2 - Main container that contains a dynamic list of cities
I found a way to do this using codeigniter (
without Smarty, yes with NO smarty yet). I call every method, retrieve the requested data from the model, assign them to the global variable using
$this->load->vars($data); and at the end when I am done with the methods, I call the view method that automatically sends all the data in an array to the view. Using
Loader Class , the data get cached and merged into one array for conversion to variables.
1 - A dynamic drop down menu list of cities
PHP Code:
public function cities_dropdown()
{
$this->load->model('cities_model');
$cities = $this->cities_model->load_cities();
foreach($cities as $val)
{
$data['cities'][] = array('name'=>$val['name'], 'id'=>$val['id']);
}
$this->load->vars($data); //Loader Class
}
2 - A dynamic list of cities
PHP Code:
public function latest_places()
{
$this->load->model('places_model');
$places = $this->places_model->load_places();
if($places)
{
foreach($places as $val)
{
if($val['suburb']!=$val['city'])
{
$this->suburb = ", ".$val['suburb'];
}
else
{
$this->suburb="";
}
$data['places'][] = array('title'=>html_escape(ucwords(strtolower($val['title']))), 'city'=>html_escape(ucwords(strtolower($val['city']))), 'date_added'=>$val['date_added'], 'suburb'=>$this->suburb, 'AID'=>$val['AID']);
}
$this->load->vars($data); //Loader Class
}
}
And this is the index method
PHP Code:
public function index()
{
$this->latest_places();
$this->cities_dropdown();
$this->view_things(); //All values assigned to $data get passed to the views $this->load->vars($data); //Loader Class
}
public function view_things()
{
$this->load->view('header_view');
$this->load->view('main_view');
$this->load->view('footer_view');
}
As you can see I am assigning different variables to $data and use $this->load->vars($data); to keep them global and send them all at once. Hopefully it is clear.
(Saying that this could be huge mvc design mistake but I dont know any better).
I wouldn't have been able to do this without
$this->load->vars($data); .
What happens if I want to do the above with Smarty?
Now my problem with smarty is that if I assign a variable in a method like
$this->smarty->assign("places", $list_places); then I have to call the view from within that method before the I close the function otherwise the value of
places gets lost and I get the notice of Undefined index. SO whats the solution? What if I have 10 methods and each assign a different array of data for different part of the view?
I hope this is clear and you can help.
Thank you.