Buffered content of a file unsuccessfully replacing the defined replace placeholder

Hi guys,

In my MVC framework’s viewer, I have run into a problem. At the point in which I buffer the contents of each section in the template, I’m having problems replacing a placeholder string within the section’s content with the content of another file. So…


public function define_content() {

        // go through all the variables set for this section
        // create a local variable of the same name and value
        // these will be used to replace any php variables
        // referenced in the file
        foreach( $this->vars as $key => $value )
            $$key = $value;       
            
        // we need to determine the path of the file associated with
        // this section
        $this->define_path();
                       
        // start buffer
        ob_start(); 

        // open the file
        @include_once( $this->path );        

        // assign buffer contents to variable
        $this->content = ob_get_contents(); 
                     
        // end buffer and remove buffer contents 
        ob_end_clean();       
        

So far no problems. The content of the section’s html markup has been transferred into the content field correctly. However, I run into the problems when I reach the following section of code. Here I start another buffer that requests the content of another file. The content of this file is then used to replace any occurrence of ‘{widget}’ found in $this->content.


        ob_start(); 
        @include_once( $this->widget_path );
        $widget = ob_get_contents();
        ob_end_clean(); 
        
        $this->content = str_replace("{widget}", $widget, $this->content ); 

What happens is the {widget} placeholder is replaced with an empty string, as opposed to the content of the file located at $this->widget_path.

For debugging purposes, I’ve tried outputting $widget before the str_replace function call and it actually outputs to the screen its content correctly. Additionally, I’ve tried to change the content of $this->widget_path’s to contain no more than the character, ‘a’ but to no avail; I did this to ensure it’s not the actual content of the second buffered file which is the problem.

Any guidance here would be much appreciated.

Try using single quotes around the placeholder:

$this->content = str_replace('{widget}', $widget, $this->content );   

It might be (I’m not sure) that inside double quotes { and } are considered special characters, used to delimit array variables so they can be interpreted. For example:

echo "Let's display a POST variable: {$_POST['variable']}";

Unfortunately, I had already tried that myself beforehand but with no successful results. The placeholder does get replaced fine if I use a local variable whose value isn’t the buffered content of a file i.e. $widget = “a”. Using that lead even if I changed the file’s content to just “a”, that didn’t make any difference.

However, here’s the $this->widget_path’s file contents just in case.


<div class="grid100 bg-blend-medium">
    <div class="padding-high">
        <div class="grid100 margin-bottom-small">
            <span class="font-serif"><strong>Basket</strong></span>
        </div>
        <div class="grid20">
            <span class="font-color-secondary">Qty</span>
        </div>
        <div class="grid60">
            <span class="font-color-secondary">Product</span>
        </div>
        <div class="grid20">
            <span class="font-color-secondary">Price</span>
        </div>  
        <span id="basket_products">  
        <?php
        $price = 0;
        for( $i = 0 ; $i < sizeof( $basket ) ; $i++ )
        {
        ?><span id="products<?php echo $i; ?>"><div class="grid20 newline">
            <input type="text" id="bskt_prd_new_quantity[<?php echo $i; ?>]" class="input-number font-xsmall padding-horizontal-normal" value="<?php echo $basket[$i]["quantity"]; ?>">
            <input type="hidden" id="bskt_prd_old_quantity[<?php echo $i; ?>]" value="<?php echo $basket[$i]["quantity"]; ?>">
        </div>
        <div class="grid60">
            <span class="font-xsmall line-height-medium">
                <span id="bskt_prd_title[<?php echo $i; ?>]"><?php echo $basket[$i]["title"]; ?></span>
                <button class="important-medium font-xsmall" id="bskt_prd_remove[<?php echo $i; ?>]">X</button>
                <input type="hidden" id="bskt_prd_id[<?php echo $i; ?>]" value="<?php echo $basket[$i]["id"]; ?>">
            </span>
        </div>
        <div class="grid20">
            <span class="font-xsmall line-height-medium">
                <strong>AED <span id="bskt_prd_price[<?php echo $i; ?>]"><?php echo $basket[$i]["price"]; ?></span></strong>
            </span>
        </div></span>  
        <?php  
        $price += $basket[$i]["price"] * $basket[$i]["quantity"];
        }
        ?>       
        </span>
        <div class="grid50">
            <button class="important-medium font-xsmall" id="update_quantity">Update Quantity</button>
        </div>                
        <div class="grid50">
            <p align="right">Total <span class="important-medium"><strong>AED <span id="bskt_price_total"><?php echo $price; ?></span></strong></span></p>
        </div>      
        <div class="grid100 margin-top-small">
            <p align="center">
                <button class="important-vlow font-xsmall" id="go_to_basket">More Info</button>
                <button class="important-low font-xsmall" id="proceed_to_checkout">Proceed to Checkout</button>            
            </p>    
        </div>    
    </div>       
</div>