2015-09-08 96 views
1

我正在處理一個PHP類,在這裏我用數據替換變量到HTML電子郵件模板文件。它通過將數據替換爲「{{first_name}}」這樣的字符串。通過str_replace()顯示html模板中的數組數據

這樣,我可以用一個客戶的正確數據替換像first_name,last_name,email等變量。這對單值很好,但現在我有一個問題。

在這封電子郵件中,我展示了客戶訂購的產品。這是一個陣列產品,每個產品都有自己的規格(請看下面的示例數組)。

問題: 有沒有人有一個想法,我可以如何實現用產品數組的循環替換{{variable}}?

產品陣列例如:

$products = array(
    array( 
     'name' => 'Product 1', 
     'price' => 10.00, 
     'qty' => 1 
    ), 
    array(
     'name' => 'Product 2', 
     'price' => 12.55, 
     'qty' => 1 
    ), 
    array(
     'name' => 'Product 3', 
     'price' => 22.10, 
     'qty' => 3 
    ) 
); 

我的類別:

class ConfirmationEmail { 

    protected $_openingTag = '{{'; 
    protected $_closingTag = '}}'; 
    protected $_emailValues; 
    protected $_template; 

    /** 
    * Email Template Parser Class. 
    * @param string $templatePath HTML template string OR File path to a Email Template file. 
    */ 
    public function __construct($templatePath) { 
     $this->_setTemplate($templatePath); 
    } 

    /** 
    * Set Template File or String. 
    * @param string $templatePath HTML template string OR File path to a Email Template file. 
    */ 
    protected function _setTemplate($templatePath) { 
     $this->_template = file_get_contents($templatePath); 
    } 

    /** 
    * Set Variable name and values one by one or at once with an array. 
    * @param string $varName Variable name that will be replaced in the Template. 
    * @param string $varValue The value for a variable/key. 
    */ 
    public function setVar($varName, $varValue) { 

     if(! empty($varName) && ! empty($varValue)) { 
      $this->_emailValues[$varName] = $varValue; 
     } 

    } 

    /** 
    * Set Variable name and values with an array. 
    * @param array $varArray Array of key=> values. 
    */ 
    public function setVars(array $varArray) { 
     if(is_array($varArray)) { 
      foreach($varArray as $key => $value) { 
       $this->_emailValues[$key] = $value; 
      } 
     } 
    } 

    /** 
    * Returns the Parsed Email Template. 
    * @return string HTML with any matching variables {{varName}} replaced with there values. 
    */ 
    public function output() { 

     $html = $this->_template; 

     foreach($this->_emailValues as $key => $value) { 
      if(! empty($value)) { 
       $html = str_replace($this->_openingTag . $key . $this->_closingTag, $value, $html); 
      } 
     } 

     return $html; 

    } 
} 

在動作:

$template_path = 'path-to-template/email-templates/confirmation.php'; 
$emailHtml = new ConfirmationEmail($template_path); 

$emailHtml->setVars(array(
    'first_name'   => 'Jack', 
    'last_name'   => 'Daniels', 
    'street'    => 'First street', 
    'number'    => '22', 
    // Other data 
)); 

// Outputs the HTML 
echo $emailHtml->output(); 

Ps。如果需要,我可以向您顯示HTML電子郵件模板。這是一個包含內聯樣式和需要替換數據的地方{{variables}}的很多表格的html結構。

回答

0

取決於您想要如何顯示它們。一種選擇可能是:

/** 
* Convert arrays to strings, ignore strings 
* @param mixed $var - variable to be instered 
*/ 
public function filterVars($var){ 
    if(is_array($var)){ 
     $str = "<table>"; 
     foreach($var as $k=>$v) $str .= "<tr><td>$k</td><td>$v</td></tr>"; 
     $str .= "</table>"; 
     $var = $str; 
    } 
    return $var; 
} 

/** 
* Set Variable name and values with an array. 
* @param array $varArray Array of key=> values. 
*/ 
public function setVars(array $varArray) { 
    if(is_array($varArray)) { 
     foreach($varArray as $key => $value) { 
      $val = $this->filterVars($value); 
      $this->_emailValues[$key] = $val; 
     } 
    } 
} 
+0

感謝您的回答!這也是我的想法。但這是乾淨的做法。或者這是一點點黑客行爲? – Robbert

+0

底線是,你必須將數組轉換爲字符串。理想情況下,你可以在數組訪問這個函數之前這樣做,這將是一種乾淨的方式,但就表格而言,這確實是最好的選擇。我就是做這個的。 –

+0

好吧,如果我選擇將數組轉換爲字符串之前的選項。你的意思是json對它進行編碼嗎?或者我需要創建一個完整的其他功能? – Robbert