Jump to content
Sign in to follow this  

Getting variables in templates


Nabeel
  • Templates all have variables defined in them which you can use. But it's hard to see sometimes which variables are actually available. But we can see it with one line. We'll use an example. Open pilot_public_profile.tpl, which is the profile of a pilot. At the top, we'll add one line (Yes, the pre tags are there on purpose - otherwise it will look like a jumbled mess):

    <pre><?php print_r(get_defined_vars()); ?></pre>

    As of phpVMS 2.1, you can do:

    <?php echo Template::showVars(); ?>

    And save it. When we load a pilot's profile, we'll see this (I removed the irrelevant chunks):

    Array
    (
    [tpl_name] => pilot_public_profile.tpl
    [ret] => 
    [checkskin] => 1
    [force_base] => 
    [tpl_path] => /var/projects/phpvms/app//core/templates/pilot_public_profile.tpl
    [allpages] => Array
    (
    	[0] => stdClass Object
    	(
    		[pageid] => 1
    		[pagename] => Test Information
    		[filename] => testinformation
    		[order] => 0
    		[postedby] => Nabeel Shahzad
    		[postdate] => 2009-10-15 10:03:36
    		[public] => 1
    		[enabled] => 1
    	)
    
    )
    
    [userinfo] => stdClass Object
    (
    	[pilotid] => 143
    	[firstname] => Test
    	[lastname] => Pilot
    	[email] => mw113@yahoo.com
    	[code] => TAY
    	[location] => AL
    	[hub] => EDDT
    	[password] => b3ebd49f578fded0a1f4938084dbaa15
    	[salt] => 221f6c8c2320a9103679eaeb42a14235
    	[bgimage] => 
    	[lastlogin] => 0000-00-00
    	[totalflights] => 0
    	[totalhours] => 0
    	[totalpay] => 0
    	[transferhours] => 0
    	[rankid] => 1
    	[rank] => New Hire
    	[ranklevel] => 1
    	[confirmed] => 0
    	[retired] => 0
    	[joindate] => 2010-04-09 14:51:35
    	[lastpirep] => 0000-00-00 00:00:00
    	[lastip] => 198.181.165.130
    	[rankimage] => http://dev.phpvms.net/lib/images/ranks/newhire.jpg
    	[payrate] => 45
    )
    
    [allfields] => Array
    (
    	[0] => stdClass Object
    	(
    		[fieldid] => 4
    		[title] => VATSIM ID
    		[type] => text
    		[fieldname] => VATSIM_ID
    		[fieldvalues] => 
    		[value] => 
    		[public] => 1
    	)
    
    )
    
    [pireps] => 
    [pilotcode] => TAY0143
    [allawards] => 
    )
    
    

    A bit confusing, but it returns an array of what's currently available. So, for example, to get the userinfo, we do:

    $userinfo
    

    Right in our template. We can see it says:

    [userinfo] => stdClass Object
    

    Meaning $userinfo is an object, and it lists all the properties, which we can access as:

    echo $userinfo->hub;
    

    The first level shown are all globals. If we look at 'allfields' it's an array, so we can loop through it, and output all the fields:

    foreach($allfields as $field)
    {
    echo "{$field->title} = {$field->value}<br>";
    }
    

Getting variables in templates
Sign in to follow this  


User Feedback

Recommended Comments



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×