Forum


Replies: 7   Views: 5080
[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  · 17-09-2012 - 08:37

Hi,

I am using the latest Version of PHPDOCX (v2.6) with a pro License. I would like to add some paragraphes using the replaceTemplateVariableByHTML function descibed here: 8http://www.phpdocx.com/documentation/api-documentation/replacevariablebyhtml-inserts-html-in-a-template. Following the example I have added the exact line like that:
[code]
$docx->replaceTemplateVariableByHTML('OBJECTIVES', 'inline', '<p style="font-family: verdana; font-size: 11px">C/ Matías Turrión 24, Madrid 28043 <b>Spain</b></p>', array('isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));
[/code]
which in may case result in the following error messages:
[quote]
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Empty string supplied as input in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1457

Warning: DOMXPath::query() [domxpath.query]: Undefined namespace prefix in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1462

Warning: DOMXPath::query() [domxpath.query]: Invalid expression in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1462

Warning: Invalid argument supplied for foreach() in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1464

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity, line: 2 in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1457

Warning: DOMXPath::query() [domxpath.query]: Undefined namespace prefix in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1462

Warning: DOMXPath::query() [domxpath.query]: Invalid expression in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1462

Warning: Invalid argument supplied for foreach() in /var/www/view_extensions/php_docx/v2.6_pro/classes/CreateTemplate.inc on line 1464
[/quote]

The error messages go away as soon as I remove the above line. Changing the html code to a simple "<p>Hello World</p>" doesn't help...

Any help would be appreciated.

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

Hello,

Please check you are adding a template using addTemplate method. You can view the example easy/replaceTemplateVariableByHTML.php

Regards.

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

Hi,

yes my implementation is totally in line with your example (even using the same replacement string). Howerver, only the first document in the loop is created as it should be. All subsequent files will be empty and the above errors are thrown.

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

Hello,

Please contact us: http://www.phpdocx.com/contact

Regards.

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]

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

Hello,

For each document you need to create a new CreateDocx object:

[code]private function createdocx($value) {
$docx = new CreateDocx();
$docx->addTemplate('template_files/test.docx');
if($this->style == 1) {
$docx->replaceTemplateVariableByHTML('ADDRESS', 'inline', $value, array('isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => false));
} else {
$docx->addTemplateVariable('ADDRESS', $value);
}
return $docx;
}[/code]

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

Thanks for the help. I thought I have tested that - howerver, you are right after removing the cache the files are generated without an error.