【问题标题】:Class function Loading HTML page with Variables类函数加载带有变量的 HTML 页面
【发布时间】:2015-04-03 17:05:54
【问题描述】:
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 行最终转换为 PDF 的 html 代码。为了让它不那么臃肿,我决定将所有的 html 分离到另一个名为 Html_Load_Template 的类中。如何将 Html_Pdf_Export 具有的所有数据变量传递给 Html_Load_Template 类?

谢谢

【问题讨论】:

    标签: php html oop templating


    【解决方案1】:

    好吧,如果我理解正确,您只需将 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
    }
    

    【讨论】:

      【解决方案2】:

      我不太确定您的“Html_Load_Template”类是什么样的,但基本上我猜您想将“getHtml”部分外包给不同的文件,对吧?

      因此,如果您将代码移至 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();
      

      【讨论】:

      • 哦,AdamM 也有同样的想法。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      相关资源
      最近更新 更多