php和模板的问题

时间:2011-03-05 17:16:03

标签: php templates

Php代码:

    class build {

        function panel() {

    $dummy="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vestibulum velit ultricies orci pharetra elementum. In massa mauris, varius sed tempus a, iaculis sed erat. Ut sollicitudin tellus mollis arcu laoreet semper. Suspendisse ut felis odio. Aliquam auctor, tortor sit amet suscipit elementum, nunc ante dictum lectus, ac accumsan justo nunc sed velit. Sed sollicitudin varius tortor vitae varius. Aliquam interdum, nisl consectetur laoreet commodo, metus massa sagittis nisl, non venenatis lacus mi nec tortor. Ut malesuada auctor dolor, id pulvinar est malesuada sed. Aliquam sed posuere orci. Proin porttitor euismod condimentum. Integer suscipit nibh nec augue facilisis ut commodo nisi ornare. Nam sed mauris vitae justo convallis placerat. Curabitur viverra, ipsum id volutpat sollicitudin, mi nisi condimentum nulla, nec dapibus velit libero eget orci. Nam purus lectus, imperdiet pharetra pulvinar ac, sodales sit amet sem. Ut vel mollis ante. Vivamus consectetur varius risus eu hendrerit. Sed scelerisque euismod leo, quis accumsan justo venenatis eu. Ut risus lorem, aliquet id fermentum nec, auctor ut enim. Ut pretium elementum turpis vel dignissim.";


        $columns=mysql_query('SELECT * FROM `column` ORDER BY id');
        while($column=mysql_fetch_array($columns))
        {
            echo '<div class="column" id="column'.$column['id'].'" >';
            $items=mysql_query("SELECT * FROM widgets WHERE column_id='".$column['id']."' AND user='".$_COOKIE['id']."' ORDER BY sort_no");
            while($widget=mysql_fetch_array($items))
            {
                echo '
                <div class="dragbox" id="item'.$widget['id'].'">
                    <h2>'.$widget['title'].'</h2>
                        <div class="dragbox-content" ';
                if($widget['collapsed']==1)
                    echo 'style="display:none;" ';
                echo '>
                            '.$dummy.'
                        </div>
                </div>';
            }               
            echo '</div>';
        }

    }

    }


     $build_panel = new build(); 

并输入模板:

              $file = str_replace('{main_panel_window}', $build_panel, $file);

错误:第47行无法将类构建的对象转换为....

如何解决问题?

4 个答案:

答案 0 :(得分:0)

更改

 $file = str_replace('{main_panel_window}', $build_panel, $file);

 $file = str_replace('{main_panel_window}', $build_panel.panel(), $file);

答案 1 :(得分:0)

问题在于错误是什么:您在调用build时尝试使用str_replace对象,如字符串。

也许你需要a string conversion,或者你需要重新考虑为什么要将build对象传递给str_replace

如果你想让panel()返回一个字符串,那么让它直接返回一个字符串而不是echo,然后写:

$file = str_replace('{main_panel_window}', $build_panel.panel(), $file);

答案 2 :(得分:0)

我认为正确的代码是:

class build {

    function panel()
    {
        $dummy="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vestibulum velit ultricies orci pharetra elementum. In massa mauris, varius sed tempus a, iaculis sed erat. Ut sollicitudin tellus mollis arcu laoreet semper. Suspendisse ut felis odio. Aliquam auctor, tortor sit amet suscipit elementum, nunc ante dictum lectus, ac accumsan justo nunc sed velit. Sed sollicitudin varius tortor vitae varius. Aliquam interdum, nisl consectetur laoreet commodo, metus massa sagittis nisl, non venenatis lacus mi nec tortor. Ut malesuada auctor dolor, id pulvinar est malesuada sed. Aliquam sed posuere orci. Proin porttitor euismod condimentum. Integer suscipit nibh nec augue facilisis ut commodo nisi ornare. Nam sed mauris vitae justo convallis placerat. Curabitur viverra, ipsum id volutpat sollicitudin, mi nisi condimentum nulla, nec dapibus velit libero eget orci. Nam purus lectus, imperdiet pharetra pulvinar ac, sodales sit amet sem. Ut vel mollis ante. Vivamus consectetur varius risus eu hendrerit. Sed scelerisque euismod leo, quis accumsan justo venenatis eu. Ut risus lorem, aliquet id fermentum nec, auctor ut enim. Ut pretium elementum turpis vel dignissim.";
        $return="";

        $columns=mysql_query('SELECT * FROM `column` ORDER BY id');
        while($column=mysql_fetch_array($columns))
        {
            $return .= '<div class="column" id="column'.$column['id'].'" >';
            $items=mysql_query("SELECT * FROM widgets WHERE column_id='".$column['id']."' AND user='".$_COOKIE['id']."' ORDER BY sort_no");
            while($widget=mysql_fetch_array($items))
            {
                $return .= '
                <div class="dragbox" id="item'.$widget['id'].'">
                    <h2>'.$widget['title'].'</h2>
                        <div class="dragbox-content" ';
                if($widget['collapsed']==1)
                    $return .= 'style="display:none;" ';
                $return.= '>
                            '.$dummy.'
                        </div>
                </div>';
            }               
            $return .= '</div>';
        }
        // return the value
        return $return;
    }

}

并在您的模板文件中:

$build_panel = new build(); 
$file = str_replace('{main_panel_window}', $build_panel->panel(), $file);

答案 3 :(得分:0)

您不能str_replace()编写此代码的方式。我认为你打算对$build_panel->panel()的输出进行操作,但函数直接回声字符串而不是构建模板输出并将其作为字符串返回。

因此,要么在某些output buffering中使用wrap panel(),要么将其连接并返回一个字符串。