Reply
Catching Conditions [Template System]
Old 04-09-2004, 02:25 PM Catching Conditions [Template System]
Christopher's Avatar
Iced Cap

Posts: 3,113
Location: Toronto, Ontario
Trades: 0
I've got a template system where you can insert custom conditions like this:
HTML Code:
<if="$variable == 'value'">do this</if>
I can do that fine, but with nested conditions, the script screws up because the <if="">'s do not match with the correct </if>'s. Example:
Code:
<if="something">   <-- Will match with...
    <if="something else">
        yeah
    </if>    <-- this, instead of
</if>   <-- this
I tried to solve this by re-calling the function in itself if there is another <if> statement nested within another one, this would evaluate the nested conditions so that no </if>'s remain, then the parent <if> will match up with the correct </if>. It's a little hard to wrap your head around, but if you understand then any help is appreciated Here's my code,

PHP Code:
    //============================================================================
    // doConditions
    //  - execute conditions
    //============================================================================
    
function doConditions($txt)
    {
        if(
strpos($txt'<if="') === false)
            return 
$txt;

        
////////////////////////////////////////////////
        // Set things up. More of a reference for me :)
        ////////////////////////////////////////////////

        
$if['s_start_len']  = strlen('<if="');          // IF start beginning length
        
$if['s_start_pos']  = false;                    // IF start beginning pos (<if=")
        
$if['s_end_len']    = strlen('">');             // IF start end length
        
$if['s_end_pos']    = false;                    // IF start end pos (">)
        
$if['cond_pos']     = false;                    // Position of starting condition
        
$if['s_last']       = false;                    // Last position of whole <if="">
        
$if['e_len']        = strlen('</if>');          // IF end length
        
$if['e_pos']        = false;                    // IF end pos (</if>)

        
$else['len']  = strlen('<else>');               // ELSE length
        
$else['pos']  = false;                          // ELSE pos (<else>)
        
$else['last'] = false;                          // Last position of whole <else>
        
$else['use']  = false;                          // Use else?



        ///////////////////////////////////
        // Loop thru until nothing is left
        ///////////////////////////////////

        
while($if['s_start_pos'] = strpos($txt'<if="') !== false)
        {
            
////////////////////////////
            // Fetch all this iffy info
            ////////////////////////////

            
$if['s_end_pos'] = strpos($txt'">'   $if['s_start_pos']);
            
$if['s_last']    = $if['s_end_pos'] + $if['s_end_len'];
            
$if['e_pos']     = strpos($txt'</if>'$if['s_last']);

            
$nested strpos($txt'<if="'$if['s_end_pos']);
            if(
$nested !== false && $nested $if['e_pos'])
            {
                
$txt substr($txt0$if['s_last']) . $this->doConditions(substr($txt$if['s_last']));
                
$if['e_pos'] = strpos($txt'</if>'$if['s_last']);
            }

            
$if['cond_pos'] = $if['s_start_pos'] + $if['s_start_len'] - 1;



            
//////////////////////
            // See about the else
            //////////////////////

            
$else['pos']  = strpos($txt'<else>'$if['s_last']);
            
$else['last'] = $else['pos'] + $else['len'];
            if(
$else['pos'] !== false && $else['pos'] < $if['e_pos'])
                
$else['use'] = true;


            
////////////////////////////
            // Grab certain needed text
            ////////////////////////////

            
$__txt_all     '';
            
$__txt_cond    '';
            
$__txt_true    '';
            
$__txt_false   '';
            
$__txt_replace '';

            
// All text for replacement later
            
$__txt_all substr($txt$if['s_start_pos'] - 1$if['e_pos'] - $if['s_start_pos'] + $if['e_len'] + 1);

            
// Condition
            
$__txt_cond substr($txt$if['cond_pos'], $if['s_end_pos'] - $if['cond_pos']);
            if(empty(
$__txt_cond))
                
$__txt_cond 'true';

            
// True
            
if($else['use'])
                
$__txt_true substr($txt$if['s_last'], $else['pos'] - $if['s_last']);
            else
                
$__txt_true substr($txt$if['s_last'], $if['e_pos'] - $if['s_last']);

            
// False
            
if($else['use'])
                
$__txt_false substr($txt$else['last'], $else['last'] - $if['e_pos']);

            
// Replacements
            
extract($this->varsEXTR_OVERWRITE);
            
$__olde error_reporting(E_ERROR);
            eval(
'$__txt_replace = (' $txt_cond ' ? $txt_true : $txt_false);');
            
error_reporting($__olde);

            
/////////////////
            // Lets dooo it!
            /////////////////
            
$txt str_replace($__txt_all$__txt_replace$__txt);
        }

        return 
$txt;
    } 
(btw, using preg_replace's would be no different I was going to use regx techniques but there are a few advantages to using this way)

Last edited by Christopher; 04-09-2004 at 03:41 PM..
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
 
When You Register, These Ads Go Away!
Old 04-25-2008, 12:49 PM Re: Catching Conditions [Template System]
Experienced Talker

Posts: 46
Trades: 0
Can't you just put them one over the other?

<if="something"> <-- Will match with...

</if>
<if="something else">
yeah
</if>
__________________
Squirrel bombing -> Home of the crasher squirrel
jeff_oneil is offline
Reply With Quote
View Public Profile Visit jeff_oneil's homepage!
 
Old 04-25-2008, 02:09 PM Re: Catching Conditions [Template System]
addonchat's Avatar
Super Talker

Posts: 113
Name: Chris Duerr
Trades: 0
Christopher -- A compiler design course would come in handy right about now In all honesty, I didn't look too close at your code, but to answer your question, one proper way to implement this would be to use a stack (FILO). PHP offers the array_push and array_pop functions to accommodate this. You can figure out what data to store in the array (maybe an object containing the condition, result, and else result) but basically you'd just push for each new <IF> and pop for each </IF>
__________________
Chris Duerr
AddonChat Java Chat Software
http://www.addonchat.com/ - Affiliate Program
addonchat is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Catching Conditions [Template System]
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.16584 seconds with 13 queries