PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 21st, 2012, 04:58 AM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,587 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 19 h 8 m 33 sec
Reputation Power: 70
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.

Reply With Quote
  #2  
Old November 21st, 2012, 05:08 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,833 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 1 h 21 m 39 sec
Reputation Power: 811
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.

Reply With Quote
  #3  
Old November 21st, 2012, 05:52 AM
zxcvbnm's Avatar
zxcvbnm zxcvbnm is offline
A Change of Season
Click here for more information.
 
Join Date: Mar 2004
Posts: 1,587 zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level)zxcvbnm User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 19 h 8 m 33 sec
Reputation Power: 70
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.

Reply With Quote
  #4  
Old November 21st, 2012, 06:07 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,833 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 1 h 21 m 39 sec
Reputation Power: 811
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.

Reply With Quote
  #5  
Old November 21st, 2012, 12:10 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Smarty: Assigning global variable from controller

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap