Giter Club home page Giter Club logo

Comments (8)

edi9999 avatar edi9999 commented on August 15, 2024 1

Since {{ starts with {, docxtemplater will try to parse it and fail, since you can't have { inside a {.

You could use completely different delimiters, for example :

[[Token1]] and {{@wmlTokenToRepalce}}, and replace with custom delimiters with [[ and then with {{

from docxtemplater.

edi9999 avatar edi9999 commented on August 15, 2024 1

Both your issues are handled by the "word-run" module : https://docxtemplater.com/modules/word-run/
This is a paid module, but with it, you can have "raw runs", eg instead of replacing a whole paragraph, when you use the given tag, it replaces only the current run.

So this means that :

  1. If you use bullet points, those will be kept
  2. You can have rawtags inside a paragraph with text surrounding it.

from docxtemplater.

edi9999 avatar edi9999 commented on August 15, 2024

Hey,

this is a good proposal, letting the user insert his own XML without any limits (as opposed to the possibility of just modify the style by your own).

I prefer to use an expliciy syntax for rawXmlData, as for example {@myXmlData}, because it could lead to unexpected results.

When the template looks like this:

<w:r>
    <w:t>Your complex text i</w:t>
</w:r>
<w:r>
    <w:t>s {@rawXmlText}.</w:t>
</w:r>

They should be these steps:

  • First read the rawXmlText variable

    <w:r>
     <w:rPr>
        <w:highlight w:val="red"/>
     </w:rPr>
     <w:t>Some highlighted text</w:t>
    </w:r>
    
  • Determine what's the root element of the xml: in this case it is rootElement='w:r'

  • find the first parent element in the xml of the docx that is rootElement , in this case:

block=

<w:r>
    <w:t>s {@rawXmlText}.</w:t>
</w:r>
  • If they is static test surrounding {@rawXmlTest}, duplicate the block for the left part and the right part, if it is not empty
<w:r>
    <w:t>s </w:t>
</w:r>
<w:r>
    <w:t>{@rawXmlText}</w:t>
</w:r>
<w:r>
    <w:t>.</w:t>
</w:r>
  • Replace the inner dynamic block by the User XML:
<w:r>
    <w:t>s </w:t>
</w:r>
<w:r>
    <w:rPr>
       <w:highlight w:val="red"/>
    </w:rPr>
    <w:t>Some highlighted text</w:t>
</w:r>
<w:r>
    <w:t>.</w:t>
</w:r>

from docxtemplater.

edi9999 avatar edi9999 commented on August 15, 2024

Actually, the step I have described:

  • Determine what's the root element of the xml: in this case it is rootElement='w:r'

should't be automatic.

because if the User XML is for example a table, it doesn't me that you want to replace a parent that is a table.

So I think I will replace by default the nearest 'w:r' value, and you could pass as a first argument:

{@w:table table1} which element you want to change. (in this case element="w:table")

from docxtemplater.

edi9999 avatar edi9999 commented on August 15, 2024

I have done a first version of this for now. (since v0.5.0)

You just enter {@tag} and it replaces the tag by xml.

Demo Here: http://javascript-ninja.fr/docxgenjs/examples/demo.html#rawxml

from docxtemplater.

adharshvlocity avatar adharshvlocity commented on August 15, 2024

Hi,

Can this work if we mix and match the tokens?

Say for example: I have the below content in the Word Doc.

This is a sample Word doc. {{Token1}} is the user who is testing this doc.

The real formatted content is in this wml token {@wmlTokentoReplace}

I will be replacing the raw XML token {@wmlTokentoReplace} content first and then the custom delimiter token {{Token1}} which will be replaced by static content.

Please advice. I tried this but I see Template Word Document Error
Unopened tag: The tag beginning with "{{Token1" is unopened
Unopened tag: The tag beginning with "}" is unopened.
Code:
doc.loadZip(zip); // replace the tokens with real data in the document doc.setData(result); /*result contains the key value pair for the token {{@wmlTokentoReplace}} like wmlTokentoReplace: "<w:r> <w:t>s </w:t> </w:r> <w:r> <w:rPr> <w:highlight w:val="red"/> </w:rPr> <w:t>Some highlighted text</w:t> </w:r> <w:r> <w:t>.</w:t> </w:r>" */ console.log(doc); doc.render();

Stuck on this for a while now. Appreciate any suggestions here.

from docxtemplater.

adharshvlocity avatar adharshvlocity commented on August 15, 2024

Hi @edi9999 , Thank you for your prompt reply.

I was able to achieve the above objective by first replacing all {{ to [[, then extract all @TokenToReplaceWml and then replace back all [[ to {{.

  1. But now I am seeing another issue. If I have something like below in my document.
  • {@replaceWmlToken1}
  • {@replaceWmlToken2}

And my WML Content looks like this for both the above tokens:

<w:p w:rsidR="00337E81" w:rsidRDefault="008A0D84"> <w:bookmarkStart w:id="0" w:name="_GoBack"/> <w:r> <w:t xml:space="preserve">This </w:t> </w:r> <w:r w:rsidRPr="008A0D84"> <w:rPr> <w:b/> </w:rPr> <w:t>is formatted text</w:t> </w:r> <w:r> <w:t xml:space="preserve"> for the Contract </w:t> </w:r> </w:p> <w:p w:rsidR="008A0D84" w:rsidRDefault="008A0D84"> <w:r> <w:t xml:space="preserve">The colored font should Appear fine. </w:t> </w:r> </w:p> <w:p w:rsidR="008A0D84" w:rsidRPr="008A0D84" w:rsidRDefault="008A0D84"> <w:pPr> <w:rPr> <w:color w:val="00B0F0"/> </w:rPr> </w:pPr> <w:r w:rsidRPr="008A0D84"> <w:rPr> <w:color w:val="00B0F0"/> </w:rPr> <w:t>Bullet points also works fine :</w:t> </w:r> </w:p> <w:p w:rsidR="008A0D84" w:rsidRDefault="008A0D84" w:rsidP="008A0D84"> <w:pPr> <w:pStyle w:val="ListParagraph"/> <w:numPr> <w:ilvl w:val="0"/> <w:numId w:val="2"/> </w:numPr> </w:pPr> <w:r> <w:t>Bullet1</w:t> </w:r> </w:p> <w:p w:rsidR="008A0D84" w:rsidRDefault="008A0D84" w:rsidP="008A0D84"> <w:pPr> <w:pStyle w:val="ListParagraph"/> <w:numPr> <w:ilvl w:val="0"/> <w:numId w:val="2"/> </w:numPr> </w:pPr> <w:r> <w:t>Bullet2</w:t> </w:r> </w:p> <w:p w:rsidR="008A0D84" w:rsidRDefault="008A0D84" w:rsidP="008A0D84"> <w:pPr> <w:pStyle w:val="ListParagraph"/> <w:numPr> <w:ilvl w:val="0"/> <w:numId w:val="2"/> </w:numPr> </w:pPr> <w:r> <w:t>Bullet3</w:t> </w:r> </w:p>

then the resulting word document looses the bullet point while replacing rawxml tokens. Is there any way around this so the bullet points are retained after the rawxml token are replaced by it wml?

  1. Also. Is there any way to achieve this - Some Header: {@rawwmlToken}? I know you mentioned in your documentation that there rawxml tags needs to be in seprate paragraph but was wondering if there is a workaround.

Appperiate any help on this.

Thank you!

from docxtemplater.

adharshvlocity avatar adharshvlocity commented on August 15, 2024

Thank you! I will definitely check with my team.

from docxtemplater.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.