Graviton
Graviton

Reputation: 83274

Smarty autobind PHP variables to Smarty variables

In my smarty code I write a lot of code such as this:

$smarty->assign('priorityList', $priorityList);
$smarty->assign("groupview", $groupview);
$smarty->assign('processList', key($processList));
$smarty->assign('taskList', $taskList);

See how annoying it has become; I use the same name for Smarty variables and PHP variables, and yet I need to waste time and typing to connect the two.

Is there any option that I can set, so that the smarty variables will be automatically mapped to the PHP variables with the same name?

Upvotes: 4

Views: 957

Answers (2)

toxicroadkill
toxicroadkill

Reputation: 11

I prefer to just use a array, smarty assign by ref, when you create the smarty object, then just assign the array by ref. Put your variables as indexes to the array, makes life so much simpler.

$smarty->assignByRef('PAGE_DATA',$page_data,false); 

then later down you can just assign the variables using $page_data, and it gets mapped to "PAGE_DATA" in the smarty template.

These are just my personal prefs, use whatever suits your fancy and since you have assignbyref $page_data, you dont have to worry about forgetting to assign it.

Another great advantage I've found, since all variables' your using are contained in a array, debugging is super easy, just print out the array ($PAGE_DATA), and poof, all your variables are listed

$page_data['PAGE_TITLE']    = 'Home';

Upvotes: 0

gnud
gnud

Reputation: 78568

Use compact.

$smarty->assign(compact('priorityList', 'groupview', 'processList', 'taskList'));

Upvotes: 18

Related Questions