The dollar_function model is the mother of all gadgets.

We have seen how dollar functions can be interspersed within textual content to generate dynamic code. GenHelm provides a model that allows you to generate new dollar functions to continuously extend the set of gadgets at your disposal.

Dollar Function Model

Recall that dollar functions allow you to "wrap" and parameterize any set of code to make it reusable. Not only does GenHelm supply over 100 dollar functions but it also includes an entire framework for creating new dollar functions starting with the dollar_function model.

$cookie Example

$cookie is a simple dollar function that is used to render a cookie value. Let's learn how this dollar function was created using the dollar_function model. This dollar function has one required parameter and two optional parameters which are:

  1. The name of the cookie (variable) is required.
  2. If the cookie is set but contains an empty value (zero or blank) you can supply an alternate (default) value to be returned.
  3. If the cookie is not set at all, you can supply an alternate default value to be returned.

Here we see the parameters that are entered into the dollar_function model to generate the $cookie functionality.

Dollar function specification for a cookie

The only thing else that is needed is the PHP code to implement $cookie's behavior. Here is the code in its entirety.

if (!empty($_COOKIE[$this->cookie_name])) {
  return $_COOKIE[$this->cookie_name];
}
// Use the third parameter as the default if the cookie variable is not set
if (isset($this->ifunset) and !isset($_COOKIE[$this->cookie_name])) { 
  return $this->ifunset; // Default only for undefined session variable
} 
// Use the second parameter as the default if the session parameter is empty
if (!isset($this->ifempty) or !isset($_COOKIE[$this->cookie_name])) {
  return ''; // No defaults supplied
}
return $this->ifempty; // Default

As with all dollar functions a help feature is built-in. This reminds the user (the web developer) as to the parameters used by the dollar function and allows them to assemble the function call automatically. Here is the help page for $cookie.

Dollar function help example

Other gadget oriented models include:

  1. codesample which is used to render code samples like the one shown above.
  2. googlemap which generates Google Maps that can contain any number of points which you define.
  3. googlemap_icon is used to create icons to be added to your generated Google maps.
  4. menu is used to generate multi-level responsive menus.
  5. model is used to build new custom models.