So if I have a plugin, with a tag like this:

{exp:reggy mask="yes"}

And I want to replace {head-type} with "horse mask" in the plugin code, how do I do that? EE docs account for replacing the whole text or modifying it, but don't specifically address variable replacement.

2 Answers 2

$variables[] = array('variable_name' => 'Your value');
$variables[] = array('variable_name' => 'Different Value');
$variables[] = array('variable_name' => 'Another one');
$this->return_data = $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $variables);

It is important to not manually specify indexes for the $variables array. The above example would loop through your tag pair three times and replace the output. Of course, normally you wouldn't be manually creating each individual iteration and it would come from query results or some other set of data.

For more information on variable parsing, go here.

  • 3
    Just a quick note, you can also use the $this->EE->TMPL->parse_variables_row method so you don't have to have an indexed associated array Commented Feb 28, 2013 at 15:29

You want to use the EE->TMPL->parse_variables function. Here's an example block

public function reggy() {
   $variables = array();

   $tagdata = $this->EE->TMPL->tagdata;

   $variables[] = array('head_type' => 'horse mask');

   return $this->EE->TMPL->parse_variables($tagdata, $variables);

You can define other replacement variables by simply adding to the $variables array, the key will be the tag name.

  • 3
    I don't think this example would work within a tag pair because the $variables array can't use a variable name as an index. EE looks for index 0 first and returns an error if there isn't one. Each item in the $variables array should be an associative array of variable_name => variable_value pairs. Commented Feb 28, 2013 at 15:22
  • Darn it! You're right Ben ... wasn't paying attention when I typed in the example, fixed it (of course it's exactly like your answer now lol :) ) Commented Feb 28, 2013 at 16:32
  • I think having plenty of examples is a good thing. It sure beats the alternative. Commented Feb 28, 2013 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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