类函数使用变量加载HTML页面

时间:2015-04-03 17:05:54

标签: php html oop templating

Class Html_Pdf_Export {
    var $first_name;
    var $last_name;
    //alot of data variables

    //How I have it now
    function getHtml()
    {
        $html = "<!DOCTYPE html>
        1000 lines of code with data variables
        </html>";

        $this->html = $html;
        return $this->html;
    }

    function convertToPdf()
    {
         //function that converts $this->html to Pdf
    }

    //How I want the function to be but How do I pass all the data variables?
    function loadHtml()
    {
         $html_load_template = new Html_Load_Template('the_template_i_want_to_load_with_data_variables');
         $this->html = $html_load_template;
         return $this->html;
    }
}

我有一个将Html转换为PDF的类。我在该类中使用的html包含1000-1500行html代码,最终转换为PDF。为了减少膨胀,我决定将所有html分离到另一个名为Html_Load_Template的类。如何将Html_Pdf_Export的所有数据变量传递给Class Html_Load_Template?

谢谢

2 个答案:

答案 0 :(得分:0)

好吧,如果我理解正确你只需要将getHtml()函数转移到Html_Load_Template类(手动工作)。所以它看起来像:

Class Html_Pdf_Export {
    var $first_name;
    var $last_name;
    //alot of data variables

    function convertToPdf()
    {
        //function that converts $this->html to Pdf
    }

    //How I want the function to be but How do I pass all the data variables?
    function loadHtml()
    {
         $html_load_template = new Html_Load_Template('the_template_i_want_to_load_with_data_variables');
         $this->html = $html_load_template->getHtml();
         return $this->html;
    }
}

Class Html_Load_Template {
    public function getHtml()
    {
        $html = "<!DOCTYPE html>
        1000 lines of code with data variables
        </html>";

        $this->html = $html;
        return $this->html;
    }

    // other functions if needed
}

答案 1 :(得分:0)

我不太确定你的&#34; Html_Load_Template&#34;上课看起来像,但基本上我想你想外包&#34; getHtml&#34;部分到不同的文件,对吗?

因此,如果您将代码移动到Load-Class ...

function getHtml()
{
    $html = "<!DOCTYPE html>
    1000 lines of code with data variables
    </html>";
    $this->html = $html;
    return $this->html;
}

...如果你通过......获得数据,它将不起作用。

$this->html = $html_load_template->getHtml();