Passing variables when building addons for GEOCore Classifieds - geoTemplate::loadInternalTemplate

Posted 2 years ago

A little gotcha I just come across when building an addon for GEOCore Classifieds was handling variables passed into a a .tpl file.

I’m going to assume you have an addon already set up. Below is the code to display “Hello” followed by a name.


function display_hello($params, $smarty){
  $tpl_vars = array(); // Define a blank array (can be variable)
  
  $tpl_vars['name']     = 'Chris';
  $tpl_vars['weather']  = 'Cold';
  
  return geoTemplate::loadInternalTemplate($params, $smarty, 'name.tpl', 
    geoTemplate::ADDON, $this->name, $tpl_vars);
}

OK let’s break this down a little.

‘name.tpl’ is the name of our template file.

geoTemplate::ADDON is defining the folder you’re going to be storing the template in. If you changed this to “SOMETHING” it would be looking in “SOMETHING” for the template name.

$this->name is basically what we set previously (name.tpl). If you replaced this with “name-new.tpl” and kept ‘name.tpl’ it would be looking for the template “addon/name-new.tpl” within your template directory.

$tpl_vars is an array of items we’ve defined in our code.

Now, open up ‘name.tpl’ within your addon folder.

We can access these variables like below.


{if $name}

Hello {$name}

{/if}

The weather is {$weather}

These variable names ($name & $weather) are what we specified previously with our $tpl_vars[‘name’].

Note: addon/name.tpl should be in your template directory
geotemplates
  -> Default
  -> YourTemplate
    -> addon
      ->name.tpl