#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171

    Smarty: Assigning global variable from controller


    Hello;

    I use $this->load->vars($data); in controllers to assign global variables with Codeigniter. For example:
    PHP Code:
    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);
        } 
    I was wondering what is the equivalent of the code above Smarty. Cause $this->smarty->assign("places", $list_places); does not create global variables!
    PHP Code:
    if($places)
        {
            foreach(
    $places as $val)
                {
                    if(
    $val['suburb']!=$val['city'])
                        {
                            
    $this->suburb ", ".$val['suburb'];
                        }
                    else
                        {
                            
    $this->suburb="";
                        } 
                    
    $list_places[] = array('title'=>html_escape(ucwords(strtolower($val['title']))), 'city_suburb'=>html_escape(ucwords(strtolower($val['city'].$this->suburb))), 'date_added'=>$val['date_added'], 'AID'=>$val['AID']);
                }
        }
    $this->smarty->assign("title""Share accomodation in Australia");
    $this->smarty->assign("places"$list_places); 
    Thank you.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,664
    Rep Power
    171
    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.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I see. Can't you just have the model methods return their data, collect the data in the controller and then pass it to the template?

    Like this, basically:
    PHP Code:
    // somewhere in the controller
    $model = new Model();
    $view = new View();
    $view->setData(array(        // this will be passed to the internal Smarty instance
        
    'cities' => $model->cities_dropdown();
        
    'places' => $model->latest_places();
    ));
    $view->display(); 
    This looks like the cleanest and least arcane solution to me. But of course you could also collect the data in a (static) property of the view.

    I mean, you don't really want the values to be global, anyway. They're just supposed to "live" between different method calls.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    There are few codeigniter plugins for integrating smarty and codeigniter you might want to check out, there may be more examples in the code of how to use smarty and ci:

    http://ilikekillnerds.com/2010/11/using-smarty-3-in-codeigniter-2-a-really-tiny-ci-library/

    https://github.com/Vheissu/Ci-Smarty

    https://github.com/kzhiwei/codeigniter-smarty

IMN logo majestic logo threadwatch logo seochat tools logo