Comments (8)
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.
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 :
- If you use bullet points, those will be kept
- You can have rawtags inside a paragraph with text surrounding it.
from docxtemplater.
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.
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.
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.
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.
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 {{.
- 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?
- 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.
Thank you! I will definitely check with my team.
from docxtemplater.
Related Issues (20)
- Cannot resolve @Name placeholder HOT 2
- Chart Module: Unexpected Behavior with Null Value in Data HOT 3
- Differentiate between conditions and loops when getting tags HOT 1
- Conditional assignment doesn't work as expected HOT 1
- Page break propogates to all elements inside div HOT 5
- Numbering for conditional text HOT 3
- Question: Importing ExpressionParser in TypeScript. HOT 6
- When I write a tag in complex content, it doesn't work HOT 4
- HTMLModule Dependencies causing errors HOT 1
- paragraphLoop doesn't seem to work HOT 2
- ChartModule + ErrorLocationModule HOT 2
- Bun.sh HOT 1
- Trying to setup angular parser, but is failing with compilation error. HOT 8
- getSize 返回一个promsie ,word预览正常,但是导出word图片丢失 HOT 2
- Compile (tsx) issue with TxtTemplater HOT 1
- Ability to Insert IDs into XML Tags HOT 1
- Slides demo is broken HOT 1
- According to Microsoft Word, the generated file is corrupted. Cannot open it. HOT 12
- Duplicate content of a word file (in one page) HOT 1
- Error: New Delimiters cannot be parsed HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docxtemplater.