While using phpLiveDocx, it is conceivable that at some point, you will be required to populate a template, in which a number of text fields and block text fields have been inserted, but whose names you do not know.
For example, in the case that you are running a Linux server and need to find out the names of text fields in a DOCX template. Or in the case that you have thousands of templates, it is much more efficient to be programmatically interrogate the files than to manually open them one by one.
phpLiveDocx ships with the three information methods, which return exactly this information: getFieldNames(), getBlockFieldNames() and getBlockNames(). Consider the following PHP5 code, which illustrates their usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | $mailMerge = new Zend_Service_LiveDocx_MailMerge(); $mailMerge->setUsername('myUsername') ->setPassword('myPassword'); // getFieldNames() $templateName = 'template-block-fields.doc'; $mailMerge->setLocalTemplate($templateName); printf("\nField names in %s:\n", $templateName); $fieldNames = $mailMerge->getFieldNames(); foreach ($fieldNames as $fieldName) { printf("- %s\n", $fieldName); } // getBlockNames() and getBlockFieldNames(); printf("\nBlock names in %s:\n", $templateName); $blockNames = $mailMerge->getBlockNames(); foreach ($blockNames as $blockName) { printf("- %s\n", $blockName); } printf("\nBlock field names in %s:\n", $templateName); foreach ($blockNames as $blockName) { $blockFieldNames = $mailMerge->getBlockFieldNames($blockName); foreach ($blockFieldNames as $blockFieldName) { printf("- %s::%s\n", $blockName, $blockFieldName); } } print("\n"); unset($mailMerge); |
This sample is shipped with phpLiveDocx in /samples/mail-merge/template-info/.