Jump to content

<p>Getting variables in templates</p>


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] => email@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

Edited by Nabeel


User Feedback

Recommended Comments

Mysterious Pilot

Posted

What if I have defined a class variable in the module class (the one that extends Codeon) and want to access that variable from the TPL (template) file?



Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...