ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Dynamic replacement of tagged words in Entries via Plugin

February 28, 2008 2:38pm

Subscribe [2]
  • #1 / Feb 28, 2008 2:38pm

    darien

    5 posts

    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.

  • #2 / Feb 29, 2008 3:25am

    darien

    5 posts

    I’ve worked this out.

    I was on the right track, but had chosen the wrong plugin to emulate. I found my final inspiration with the GeeVar plugin at http://studio625.com/ee/geevar/

    Moderator: You can delete this thread if desired.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases