The cloneContent method of Docxpresso allows you to clone the template contents.

Although this method is mainly used internally as an aid for the replace method it may sometimes become handy when there are parts of a template that owe to be replicated in order to accomodate the required contents.

The cloneContent method when used in combination with the replace method let you, for example, copy a section of your document, replicate it as many times as desired and later replace part of its contents by the requested values.

If you just need to replicate list elements or table rows you may let the replace method to take care automatically of all the required cloning.

The public API of the cloneContent method can be summarized as follows:

Signature

public cloneContent ([$options])

Parameters

  • $options (type: array). This array has the following available keys and values:
    • needle (type: string). The text to be searched to select the content to be clone or the bookmark name if the element option is set to be equal to bookmark.
    • element (type: string, default: paragraph).
      It sets the type of element to be cloned, possible values are:
      • paragraph: the paragraphs that contain the needle text are cloned,
      • list: the lists that contain the needle text are cloned,
      • list-item: the list items that contain the needle text are cloned,
      • table: the table that contains the needle text is cloned unless the text fall within a subtable (then only the subtable is cloned),
      • table-row: the same that for table but just for a table row,
      • image: the images which title include the needle text are cloned,
      • chart: the charts which title include the given text are cloned,
      • section: all section content of a section that contains the needle text is cloned,
      • heading: all the contents that fall under a given heading are cloned whenever the heading contains the needle text.
      • bookmark: all the bookmarked elements will be cloned. Warning: only elements at the same document depth are selected.
    • heading-level (type: integer, default: 1). It only applies if 'heading' is the chosen element value.
    • match (type: mixed, default: all). If an integer (defining its order of appareance) only that match will be cloned if 'all'(default value)all the matches will be cloned.
    • position (type: string, default: self). It specifies were the cloned nodes should be inserted. Possible values are self (just before the cloned element) or cursor (just before the current cursor position).
    • repeat (type: integer, default: 1). The number of times that the cloned element will be repeated.
    • target (type: string, default: document). Restricts the scope of search. The possible values are: document, header or footer.

The element option allows us to carefully choose the chunk of content that should be cloned from the template. If the heading option is chosen all content that will naturally fall within it in an ordinary table of contents will be cloned.

A few illustrative examples

Let us just clone some general content from this template: paragraphs (by content and position) and a table row:

<?php
/**
 * This sample script clones some general content from the template
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument(array('template' => 'template_clone_general.odt'));
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//clone a paragraph by content
$doc->cloneContent(array('needle' => 'cloned', 'element' => 'paragraph'));
//clone a paragraph by position, beware that a previous paragraph has already be cloned
//so the position has increased by one
$doc->cloneContent(array('needle' => '', 'element' => 'paragraph', 'match' => 5));
//clone a table row
$doc->cloneContent(array('needle' => 'one', 'element' => 'table-row'));
//include in the render method the path where you want your document to be saved
$doc->render('clone_general_content' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'clone_general_content' . $format . '">Download document</a>';  

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

Now we can just clone some bookmarked content from this template:

<?php
/**
 * This sample script clones some bookmarked content
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument(array('template' => 'template_clone_bookmark.odt')));
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//clone bookmarked text
$doc->cloneContent(array('needle' => 'html52pdf', 'element' => 'bookmark'));	
//include in the render method the path where you want your document to be saved
$doc->render('clone_bookmarked' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'clone_bookmarked' . $format . '">Download document</a>';  

DOWNLOAD:download pdfdownload docdownload docxdownload odtdownload rtf

We finish by cloning all the content that falls within a heading scope from this template:

<?php
/**
 * This sample script clones all the content associated with a heading
 */
require_once 'pathToDOCXPRESSO/CreateDocument.inc';
$doc = new Docxpresso\CreateDocument(array('template' => 'template_clone_heading.odt')));
$format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf
//clone heading
$doc->cloneContent(array('needle' => 'Clone', 'element' => 'heading', 'heading-level' => 1));
//include in the render method the path where you want your document to be saved
$doc->render('clone_heading' . $format); 
//echo a link to the generated document
echo 'You may download the generated document from the link below:<br/>';
echo '<a href="' . 'clone_heading' . $format . '">Download document</a>';  

DOWNLOAD: download pdfdownload docdownload docxdownload odtdownload rtf