Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

assign variables in different php file

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
daweedco
Smarty n00b


Joined: 06 Nov 2014
Posts: 2

PostPosted: Thu Nov 06, 2014 2:02 pm    Post subject: assign variables in different php file Reply with quote

Hi, i'm new to smarty, i've been looking for the solution for 2 days and still can't find it.

It's a simple problem , i have 2 controllers (dashboard.php, header.php) and 2 views(dashboard.tpl, header.tpl).

If i declare my object ($smarty->new Smarty) in the dashboard.php, i can't assign variables to smarty in my header.php .

If i declare my object in the index.php, i can only assign variables in the index.php file.

What do i need to do to assign variables in different php files?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Nov 06, 2014 2:52 pm    Post subject: Reply with quote

This sound like a PHP variable scoping issue. If you include header.php inside dashboard.php, your variables should be visible there.

dashboard.php:

$smarty = new Smarty();
include("header.php");

header.php:

$smarty->assign('foo','bar');
Back to top
View user's profile Send private message Visit poster's website
daweedco
Smarty n00b


Joined: 06 Nov 2014
Posts: 2

PostPosted: Thu Nov 06, 2014 3:32 pm    Post subject: Reply with quote

Thanks for the reply,

Maybe i did not express the exact problem:

In my dashboard.php :

class ControllerCommonHome extends Controller {
public function index() {

// Smarty config
require_once('../Smarty-3.1.21/libs/SmartyBC.class.php');
$smarty = new SmartyBC;
$smarty->template_dir = "view/template/common";
$smarty->compile_dir = "tmp"

$this->document->setTitle($this->language->get('heading_title'));


$smarty->assign('heading_title', $this->language->get('heading_title'));

............
}

when i do that i can use 'heading_title' in my dashboard.tpl, that's what i want. But now that i declared $smarty in dashboard.php, i can't declare it in header.php right?

But in the header.php i want to assign some variables

header.php :

class ControllerCommonHeader extends Controller {
protected function index() {

$smarty->assign('title', $this->document->getTitle());

........
}
I can't do that because i didn't declare $smarty in the header file.
How can i assign variables in the header.php AND in the dashboard.php?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Nov 06, 2014 8:19 pm    Post subject: Reply with quote

It looks like you create separate controller objects, each with their own smarty objects. If you want to share the smarty object, you have to setup your controllers to use a shared instance (singleton), or some other way of using the same Smarty object. Here is an example:

http://www.smarty.net/forums/viewtopic.php?t=16604
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Nov 21, 2014 12:28 am    Post subject: Reply with quote

In either case, you should NOT, never EVER include a class definition file inside a method of another class. Not to mention, class constructor.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP