Forum


Replies: 7   Views: 5250
[docx v2.6] replacetemplatevariablebyhtml issue
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by js  · 11-04-2013 - 12:13

Hi,

for the rest of the world - I have created a "proof of error" class which aims to generate 4 documents in a loop.

[code]
<?php

require_once("classes/CreateDocx.inc");

class test {

private $test_loop;
private $docx_cache = false;
private $style = 1;

/**
*
* Construcutor
* @param int $style allowed value is 1 or actually something else :-). 1 will use thereplaceTemplateVariableByHTML function and something else will use addTemplateVariable function
*/
function test($style = 1) {
$this->style = $style;
$this->test_loop[] = "<p>my test 1</p>";
$this->test_loop[] = "<p>my test 2</p>";
$this->test_loop[] = "<p>my test 3</p>";
$this->test_loop[] = "<p>my test 4</p>";
}

/**
*
* Loop through the array of html values
*/
public function loop() {
foreach ($this->test_loop as $key => $value) {
$docx = $this->createdocx($value);
$docx->createDocx('template_files/test_'.$key);
echo "<a href=\"template_files/test_".$key.".docx\">klick</a><br />";
}
}

/**
*
* Create the actual docx files based on the template - only load the template once
* in order to save i/o
* @param string $value random html string which will replace the ADDRESS var in the template
*/
private function createdocx($value) {
if($this->docx_cache == false) {
$docx = new CreateDocx();
$docx->addTemplate('template_files/test.docx');
$this->docx_cache = $docx;
} else {
$docx = $this->docx_cache;
}
if($this->style == 1) {
$docx->replaceTemplateVariableByHTML('ADDRESS', 'inline', $value, array('isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));
} else {
$docx->addTemplateVariable('ADDRESS', $value);
}
return $docx;
}

}

if(isset($_GET['style'])) {
$style = $_GET['style'];
} else {
$style = 1;
}

$test = new test($style);

$test->loop();

?>
[/code]