Using a Qtag's target and attributes

The structure of a qtag Class

A Qtag class, has three constructor parameters: 

$env - The Environment (passed by default, can't be changed)

$target - The Qtag's target

$attributes - An array of other attributes of the Qtag 

Using a Qtag's target

For a simple example of using target, let's examine the [TEXT] Qtag.

class Text extends Qtag {
  /**
   * Render the Qtag.
   *
   * @return text
   *   The rendered Qtag.
   */
  public function render() {
    return t($this->getTarget());
  }
}

Using:

[TEXT:Hello World!]

anywhere in your code (index.html, templates, node body, etc.) will trigger the TEXT class (as included in TEXT.qtag.php) passing the target 'Hello World!

As you can see from the above class's body, this will result in a printed version of the target TEXT.

In example: Hello World!

Using a Qtag's Attributes

For a simple example of using attributes, let's examine the [RANDOM] qtag.

class Random extends Qtag {
  /**
   * Render the Qtag.
   *
   * @return string
   *   The rendered Qtag.
   */
  public function render() {
    $min = !empty($this->attributes['min']) ? $this->attributes['min'] : 0;
    $max = !empty($this->attributes['max']) ? $this->attributes['max'] : 1000000;
    return rand($min, $max);
  }
}

Using:

[RANDOM|min=10|max=20]

anywhere in your code (index.html, templates, node body, etc.) it will then trigger the RANDOM class passing the attributes 'min' = 10 and 'max' = 20. 

As you can see from the above class's body, this will result in a random number between min (10) and max (20).

In example: 15

Note that in this example we did not specify any target, that's implied null and not used for this qtag.

So what a full Qtag looks like?

Combining target and attributes, a typical qtag could look like:

[LINK|link_title=Go to wikipedia!|protocol=https|title=Go to wikipedia:www.wikipedia.org]

This will result in the link below:

Go to wikipedia