Hello smart people. I’m a brand new EE user who has spent the last three weeks building out a fairly complex site. EE has been a pleasure to work with, and I think I’ve done most things “by the book”.
I’ve come across a situation that I can’t see to solve directly in EE, so I’m asking for some assistance. The site I’m building needs to display dynamic information on the page that is specified in the Entries, not the templates. Think: Bank wants to reference their current day’s rates within the body text of some entries.
Today’s savings rate is 4.32% and our checking rate is 3.45%.
The rates themselves are stored as a PHP array in a file on disk that is updated daily through external means.
// rates.php
$rate['savings'] = '4.32';
$rate['checking'] = '3.45';So, looking at the nature of plugins, it seems this would be a logical choice. Use a plugin that read the rates array, then performed a replacement on specially constructed tags found in an entry. Like this:
Today’s savings rate is {exp:rates}savings{/exp:rates}% and our checking rate is {exp:rates}checking{/exp:rates}]%.
I’m having two issues:
1. I can’t reference $rate[$1] as the replace variable in preg_replace. It croaks on the first dollar sign.
2. I should be able to use preg_replace_callback, but I’m having difficulty calling another local function in this class (I’m rather new at OO, more of a procedural guy)
Here’s what I have so far, and it’s almost there. Any gurus have an idea where I’ve missed something?
class Rates {
var $return_data;
function my_get_rate($matches)
{
$rates = array();
// Would love to reference the external rates.php file here, seemingly can't include from a class
$rates['test'] = '1.234';
$rates['rc_heloc'] = '1.234';
$rates['rc_savings'] = '1.234';
$rates['rc_certificates_12mo'] = '1.234';
$this_match = trim($matches[1]); // remove excess whitespace, if any
$this_rate = $rates[$this_match];
return $this_rate;
}
function rates($str = '')
{
global $TMPL;
if ($str == '')
$str = $TMPL->tagdata;
$str = preg_replace_callback('/(.*?)/', 'my_get_rate' ,$str);
// above only returns the original string
// How to get it to run the specified function?
//$str = preg_replace('/(.+)/', "BLAH=$1" ,$str);
// above correctly returns the string, prepended by BLAH=
$this->return_data = $str;
}
}Thanks for any insight.