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.
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 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:
Here we see the parameters that are entered into the dollar_function model to generate the $cookie functionality.
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.
Other gadget oriented models include: