|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
daweedco Smarty n00b
Joined: 06 Nov 2014 Posts: 2
|
Posted: Thu Nov 06, 2014 2:02 pm Post subject: assign variables in different php file |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Nov 06, 2014 2:52 pm Post subject: |
|
|
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 |
|
daweedco Smarty n00b
Joined: 06 Nov 2014 Posts: 2
|
Posted: Thu Nov 06, 2014 3:32 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Nov 06, 2014 8:19 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Nov 21, 2014 12:28 am Post subject: |
|
|
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 |
|
|
|
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
|