# **模板處理**
您可以使用包含的搜索模式(宏)創建OOXML文檔模板,該模板可以替換為您希望的任何值。只能替換單行值。宏定義如下:${search-pattern}。要加載模板文件,請創建TemplateProcessor的新實例。
`$ templateProcessor \= new TemplateProcessor(' Template.docx ');`
## **替換值**
### **setValue(設定值)**
給定一個包含的模板
你好$ {firstname} $ {lastname} !
```
下面將取代${firstname}同John,并${lastname}用Doe。生成的文檔現在將包含Hello John Doe!
$ templateProcessor \- > setValue(' firstname ',' John ');
$ templateProcessor \- > setValue(' lastname ',' Doe ');
```
### **setValues(數組)方法**
PHPword 0.16版本中未加入此函數,更新至0.17-dev版本后可用。
您還可以通過將所有值傳遞到數組中來設置多個值。
```
$ templateProcessor \- > setValues(array(' firstname ' \=> ' John ',' lastname ' \=> ' Doe '));
```
### **setImageValue(圖片)**
***圖像的搜索模式模型可以是:***
```
· ${search-image-pattern}
· ${search-image-pattern:\[width\]:\[height\]:\[ratio\]}
· ${search-image-pattern:\[width\]x\[height\]}
· ${search-image-pattern:size=\[width\]x\[height\]}
· ${search-image-pattern:width=\[width\]:height=\[height\]:ratio=false}
```
***where:***
· \[width\]和\[height\]可以只是帶有小節的數字或數字,由Word支持(cm,mm,in,pt,pc,px,%,em,ex)
· \[比率\]只使用用于false,\-或f以關閉圖像的方面的寬高比。默認情況下,模板圖像大小用作“容器”大小。
例:
```
$ {CompanyLogo}
$ {UserLogo :50 :50 } $ {Name} \- $ {City} \- $ {Street}
$ templateProcessor \= new TemplateProcessor(' Template.docx ');
$ templateProcessor \- > setValue(' Name ',' John Doe ');
$ templateProcessor \- > setValue(array(' City ',' Street '),array(' Detroit ',' 12th Street '));
$ templateProcessor \- > setImageValue(' CompanyLogo ',' path / to / company / logo.png ');
$ templateProcessor \- > setImageValue(' UserLogo ',array(' path ' \=> ' path / to / logo.png ',' width ' \=> 100,' height ' \=> 100,' ratio ' \=> false));
```
## **塊替換**
### **1.cloneBlock**
給出一個包含See的模板Sample\_23\_TemplateBlock.php示例。
```
$ {block\_name}
Customer: $ {customer\_name}
Address: $ {customer\_address}
$ { / block\_name}
```
下面將復制之間的所有內容${block\_name},并${/block\_name}3次。
$ templateProcessor \- > cloneBlock(' block\_name ',3,true,true);
最后一個參數將重命名塊中定義的任何宏,并將#1,#2,#3 ...添加到宏名稱中。結果將是
```
Customer: $ {customer\_name #1 }
Address: $ {customer\_address #1 }
Customer: $ {customer\_name #2 }
Address: $ {customer\_address #2 }
Customer: $ {customer\_name #3 }
Address: $ {customer\_address #3 }
```
也可以傳遞一個帶有值的數組來替換marcros。如果傳遞帶有替換的數組,count則忽略該參數,它是計數的數組的大小。
```
$ replacements \= array(
array(' customer\_name ' => ' Batman ',' customer\_address ' => ' Gotham City '),array(' customer\_name ' => ' Superman ',' customer\_address ' => ' Metropolis '),); $ templateProcessor \- > cloneBlock(' block\_name ',
true,false,$ replacementments);
```
結果將是
```
Customer: Batman
Address: Gotham City
Customer: Superman
Address: Metropolis
```
### **2.replaceBlock**
給定一個包含的模板
`$ {block\_name}`
此塊內容將被替換
`$ { / block\_name}`
以下內容將替換``$ {block\_name}``和${/block\_name}傳遞的值之間的所有內容。
`$ templateProcessor \- > replaceBlock(' block\_name ','這是替換文本。');`
### **3.deleteBlock**
與上一個相同,但它刪除了塊
`$ templateProcessor \- > deleteBlock(' block\_name ');`
## 表格行
### **1.cloneRow**
克隆模板文檔中的表格行。請參閱Sample\_07\_TemplateCloneRow.php示例。
```
\+ ----------- + ---------------- +
| $ {userId} | $ {userName} |
| | ---------------- +
| | $ {userAddress} |
\+ ----------- + ---------------- +
$ templateProcessor \- > cloneRow(' userId ',2);
```
會導致
```
\+ ------------- + ------------------ +
| $ {userId #1 } | $ {userName #1 } |
| | ------------------ +
| | $ {userAddress #1 } |
\+ ------------- + ------------------ +
| $ {userId #2 } | $ {userName #2 } |
| | ------------------ +
| | $ {userAddress #2 } |
\+ ------------- + ------------------ +
```
### **2.cloneRowAndSetValues**
在$ search param標識的表行中查找一行,并將其克隆為$ values中的條目的次數。
```
\+ ----------- + ---------------- +
| $ {userId} | $ {userName} |
| | ---------------- +
| | $ {userAddress} |\+ ----------- + ---------------- +
$ values \= \[
\[ ' userId ' \=> 1,' userName ' \=> ' Batman ',' userAddress ' \=> ' Gotham City ' \],
\[ ' userId ' \=> 2,' userName ' \=> ' Superman ',' userAddress ' \=> ' Metropolis ' \],
\];
$ templateProcessor \- >cloneRowAndSetValues(' userId ',);
```
會導致
```
+---+-------------+
| 1 | Batman |
| |-------------+
| | Gotham City |
+---+-------------+
| 2 | Superman |
| |-------------+
| | Metropolis |
+---+-------------+
```
## 頁眉、頁腳樣式
### **1.applyXslStyleSheet**
應用傳遞給頁眉部分,頁腳部分和主要部分的XSL樣式表
```
$ xslDomDocument \= new \\ DOMDocument();
$ xslDomDocument \- > load(' / path / to / my /stylesheet.xsl ');
$ templateProcessor \- > applyXslStyleSheet($ xslDomDocument);
```
### **2.setComplexValue**
使用ComplexType傳遞$ {macro}。請參閱Sample\_40_TemplateSetComplexValue.php示例。
```
$ inline \= new TextRun();
$ inline \- > addText(' by a red italic text ',array(' italic ' \=> true,' color ' \=> ' red '));
$ templateProcessor \- > setComplexValue(' inline ',$ inline);
```
### **3.setComplexBlock**
使用ComplexType傳遞$ {macro}。請參閱Sample\_40_TemplateSetComplexValue.php示例。
```
$ table \= new Table(array(' borderSize ' \=> 12,' borderColor ' \=> ' green ',' width ' \=> 6000,' unit ' \=> TblWidth :: TWIP));
$ table \- > addRow();
$ table \- > addCell(150)\- > addText(' Cell A1 ');
$ table \- >150)\- > addText(' Cell A2 ');
$ table \- > addCell(150)\- > addText(' Cell A3 ');$ table \- > addRow();
$ table \- > addCell(150)\- > addText(' Cell B1 ');
$ table \- > addCell(150)\- > addText(' Cell B2 ');
$ table \- > addCell(150)\- > addText(' Cell B3 ');
$ templateProcessor \- > setComplexBlock( ' table ', $ table);
```