4

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?

2 Answers 2

17

Use compact.

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

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';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.