-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clone block issue #1855
Comments
Hi, you should take a look at my ticket because I have the same problem : #1836 I've just posted a rewrite of the function which bypasses the issue for my cases. |
phpword when character length add more clone block not working after this loop adding content its not worked any one solved this please help |
hi got issue resolved please replace below code
|
In case you guys don't want to modify the original file, just write an extension `use PhpOffice\PhpWord\TemplateProcessor;
}` And use by `
}` |
thanks
…On Wed, Jun 17, 2020 at 10:11 AM silver247 ***@***.***> wrote:
Hi, you should take a look at my ticket because I have the same problem :
#1836 <#1836>
I've just posted a rewrite of the function which bypasses the issue for my
cases.
hi got issue resolved
please replace below code
in template preprocessor
/**
- Clone a block.
-
- @param <https://github.com/param> string $blockname
- @param <https://github.com/param> int $clones How many time the
block should be cloned
- @param <https://github.com/param> bool $replace
- @param <https://github.com/param> bool $indexVariables If true, any
variables inside the block will be indexed (postfixed with #1
<#1>, #2
<#2>, ...)
- @param <https://github.com/param> array $variableReplacements Array
containing replacements for macros found inside the block to clone
-
- @return <https://github.com/return> string|null
*/
public function cloneBlock($blockname, $clones = 1, $replace = true,
$indexVariables = false, $variableReplacements = null)
{
$xmlBlock = null;
$matches = array();
list($matches[1],$matches[2],$matches[3]) = $this->getBlocks($blockname);
if (isset($matches[2])&&$matches[2]) {
$xmlBlock = $matches[2];
if ($indexVariables) {
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
} elseif ($variableReplacements !== null && is_array($variableReplacements)) {
$cloned = $this->replaceClonedVariables($variableReplacements, $xmlBlock);
} else {
$cloned = array();
for ($i = 1; $i <= $clones; $i++) {
$cloned[] = $xmlBlock;
}
}
if ($replace) {
$this->tempDocumentMainPart = str_replace(
$matches[1] . $matches[2] . $matches[3],
implode('', $cloned),
$this->tempDocumentMainPart
);
}
}
return $xmlBlock;
}
/**
* Get part of block for cloneBlock
*
* @param string $blockName Block name to clone
*
* @return array
*/
private function getBlocks($blockName){
$dataXML = $this->tempDocumentMainPart;
if(stripos($dataXML,'{'.$blockName.'}') && stripos($dataXML,'{/'.$blockName.'}')){
$startBlock1 = strrpos(substr($dataXML,0,stripos($dataXML,'{'.$blockName.'}')), '<w:p ');
$lengthBlock1 = (stripos(substr($dataXML,$startBlock1),'p>')+2);
$block1 = substr($dataXML,$startBlock1,$lengthBlock1);
$startBlock3 = strrpos(substr($dataXML,0,stripos($dataXML,'{/'.$blockName.'}')), '<w:p ');
$lengthBlock3 = (stripos(substr($dataXML,$startBlock3),'p>')+2);
$block3 = substr($dataXML,$startBlock3,$lengthBlock3);
$block2 = substr($dataXML,$startBlock1+$lengthBlock1,$startBlock3-($startBlock1+$lengthBlock1));
return array($block1, $block2, $block3);
}
return array(0,0,0);
}
In case you guys don't want to modify the original file, just write an
extension
`use PhpOffice\PhpWord\TemplateProcessor;
class TemplateProcesserExtension extends TemplateProcessor {
//put your code here
public function __construct($documentTemplate) {
parent::__construct($documentTemplate);
}
public function cloneBlockNew($blockname, $clones = 1, $replace = true, $indexVariables = false, $variableReplacements = null) {
$xmlBlock = null;
$matches = array();
list($matches[1], $matches[2], $matches[3]) = $this->getBlocks($blockname);
if (isset($matches[2]) && $matches[2]) {
$xmlBlock = $matches[2];
if ($indexVariables) {
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
} elseif ($variableReplacements !== null && is_array($variableReplacements)) {
$cloned = $this->replaceClonedVariables($variableReplacements, $xmlBlock);
} else {
$cloned = array();
for ($i = 1; $i <= $clones; $i++) {
$cloned[] = $xmlBlock;
}
}
if ($replace) {
$this->tempDocumentMainPart = str_replace(
$matches[1] . $matches[2] . $matches[3],
implode('', $cloned),
$this->tempDocumentMainPart
);
}
}
return $xmlBlock;
}
/**
* Get part of block for cloneBlock
*
* @param string $blockName Block name to clone
*
* @return array
*/
private function getBlocks($blockName) {
$dataXML = $this->tempDocumentMainPart;
if (stripos($dataXML, '{' . $blockName . '}') && stripos($dataXML, '{/' . $blockName . '}')) {
$startBlock1 = strrpos(substr($dataXML, 0, stripos($dataXML, '{' . $blockName . '}')), '<w:p ');
$lengthBlock1 = (stripos(substr($dataXML, $startBlock1), 'p>') + 2);
$block1 = substr($dataXML, $startBlock1, $lengthBlock1);
$startBlock3 = strrpos(substr($dataXML, 0, stripos($dataXML, '{/' . $blockName . '}')), '<w:p ');
$lengthBlock3 = (stripos(substr($dataXML, $startBlock3), 'p>') + 2);
$block3 = substr($dataXML, $startBlock3, $lengthBlock3);
$block2 = substr($dataXML, $startBlock1 + $lengthBlock1, $startBlock3 - ($startBlock1 + $lengthBlock1));
return array($block1, $block2, $block3);
}
return array(0, 0, 0);
}
}`
And use by
`
use TemplateProcesserExtension;
public function generateDocumentFromTemplate($document) {
$templateProcessor = new TemplateProcesserExtension('path to template');
$templateProcessor->setValue('variable name', 'some value'); //Original PHPWord Method
$block_replacements = $replacement_data;
$templateProcessor->cloneBlockNew('block_name', 0, true, false, $block_replacements); //Extension Method
}`
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1855 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEJ2N3ZJUVEURN2R3BI7ROLRXBCONANCNFSM4MQDQZPA>
.
|
But it's not complete. It's 2023 and this is still an issue. |
clone block not working when adding more content after the clone block please help
${test}
${v1}
${/test}
after adding text its not working
its depending on character
The text was updated successfully, but these errors were encountered: