Forum


Replies: 3   Views: 2307
Get text properties from a variable
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 ISSL-blocked  · 25-06-2019 - 09:56

Hi there,

Our templates are user-generated, so contain many different styles. I understand that when using

$docx->replaceVariableByText('FULL_DESCRIPTION', 'My description');

that the original text properties are honoured - so it will retain bold, the font, etc. That's perfect.

However, when replacing with HTML or a WordFragment, this isn't the case and obviously these are more complicated.

I wondered if it was possible to get the text properties from a variable that's about to be replaced - something like using $docx->getDocxPathQueryInfo so that I could search for a variable like $FULL_DESCRIPTION$ and then be able to get its size, font, bold, etc properties so that I can reapply them myself to my HTML or to the WordFragment I'm creating?

Posted by admin  · 25-06-2019 - 11:35

Hello,

UPDATE: the latest release of phpdocx includes options to apply (and mix if needed) placeholder styles to new HTML and WordFragment contents using template methods.

Both replaceVariableByHTML and replaceVariableByWordFragment methods include the type option, that can be inline or block. If you use a block replacement (the default in both methods), then the container paragraph is removed so some styles can disappear (such as the preset paragraph styles). If the type option is set to inline the paragraph styles aren't removed but only inline contents are added.

There's no method to do a block replacement and apply automatically the previous paragraph styles, so the new styles must come from the HTML/WordFragment. Using getWordStyles you can get the styles from a DOCXPath query, but (as you say) you'd need to iterate the array output of the previous method to generate the HTML or WordFragment with the styles.

Regards.

Posted by ISSL-blocked  · 25-06-2019 - 11:59

That's fine, I can work that into our code - do you have an example of using getWordStyle (presumably with a custom query) to try to get the styles from a variable $FULL_DESCRIPTION$ as part of the text?

Many thanks,

Posted by admin  · 25-06-2019 - 15:22

Hello,

We recommend you to check the samples included in the package (DocxPath folder) and the documentation available on https://www.phpdocx.com/documentation/introduction/docxpath. In these samples, you can find a lot of queries, including using custom queries.

For example, the following sample included in the package:

$docx = new CreateDocxFromTemplate('../../files/DOCXPathTemplate.docx');

$referenceNode = array(
    'type' => 'paragraph',
    'contains' => 'level 2 heading',
);

$styles = $docx->getWordStyles($referenceNode);

print_r($styles);

returns the styles applied to the paragraph that contains level 2 heading. This method returns an array with all styles of the queried content ordered by type.

Regards.