Forums | Mahara Community

Developers /
Accessing $USER variable from a theme


anonymous profile picture
Account deleted
Posts: 2

26 October 2010, 12:33

Hi

I am new to coding in mahara and would appriciate it if someone could tell me how I can access the values held in the global $USER variable from within a theme. I am trying to make a dynamic theme that will change certain elements of the header depending on values in the users profile.  I have tried to put a call to the $USER variable from within the  header.tpl file but it ($USER) only seems to contain its inialised values (mostly null).

Thanks

anonymous profile picture
Account deleted
Posts: 214

27 October 2010, 3:26

Hi NIgel,

As you're probably aware, Mahara uses the Dwoo template engine - documentation is available from http://wiki.dwoo.org/index.php/Main_Page. However, we used to use the Smarty template engine. As a result, there are variables all of the place called $smarty and the template engine is instantiated by calling a function in Mahara which is really a wrapper around Dwoo.

If you're not familiar with either engine, before you can use a variable in the template, you must pass it from php to the template with the assign() function. The more you pass to  your template, the more bloated things get, and the more memory each page consumes. Ultimately this means that your pages will take longer to compile, and you'll require more resource to scale. The USER object is quite large. If at all possible, I would think that it would be worth handling as much of your logic in php as you can, rather than in the actual template, and passing just the information you need to the template - perhaps even just a boolean value.

There is a way of ensuring that your variable is assigned for all pages. As I mentioned before, the Dwoo class is instantiated by a wrapper. The wrapper lives in /htdocs/lib/web.php in the smarty function (about line 62 depending on the version of mahara you're using). At the end of the function, the function returns $smarty. At some point before this return statement, you need to add a $smarty->assign('templatevarname', $phpvarname) for your variables.

I guess that since you're creating a new theme, you're not just modifying the templates in the raw theme? Mahara themes support inheritance - there are some details on the Mahara wiki at http://wiki.mahara.org/Customising/Themes/1.2.

I hope that this points you in the right direction,

Andrew

anonymous profile picture
Account deleted
Posts: 2

27 October 2010, 5:48

Thanks Andrew,

I should be able to work out the rest. Laughing

3 results