Comments (15)
Looks like I didn't RTFD- sorry
from docxtemplater.
Haha no problem. Great to see self closing issues
from docxtemplater.
@edi9999 , I think the feature is cool , now I need transfer json to object. haha
from docxtemplater.
Hey,
Sorry but I'm having the same problem and I can't seem to find the answer in the docs, maybe I'm just not reading it right. I'm trying to loop over a nested object using the following:
{#positions}
{#company}
{name}
{/company}
{title}
{/positions}
I also tried:
{#positions}
{company.name}
{title}
{/positions}
But that didn't work either. The strange thing for me is that {title}
didn't work for the first few attempts and then it just started working, I'm not too sure why though, I didn't change anything.
Anyway, I'd appreciate any pointers you can give me.
Thanks
from docxtemplater.
Can you please post your json data ?
{#company} {/company} is to loop over arrays, i don't think it works with objects now. You can extend that, it's in scopemanager.loopover I think. I accept PR for this
The company.name syntax works only if you enable the angular parser.
from docxtemplater.
Ah ok, that would be why then as company
is an object not an array.
I noticed everything is in CoffeeScript so I probably won't be able to do a PR but I'll take a look anyway.
I've enabled the angular parser but company.name
still doesn't work, is that because company
is an object or should the angular parser handle both objects an arrays?
[
{
company: {
industry: 'Internet',
name: 'Company 1'
},
title: 'Designer'
},
{
company: {
industry: 'Internet',
name: 'Company 2'
},
title: 'Developer'
}
]
Thanks for your help @edi9999
from docxtemplater.
The angular parser works with objects.
You can use the following parser to debug and see which values are undefined:
angularParser = function(tag) { var e, expr; try { expr = expressions.compile(tag); } catch (_error) { e = _error; console.log("parsing didn't work with " + tag); } return { get: function(scope) { if (scope == null) { console.log('warning: scope undefined'); } try { return expr(scope); } catch (_error) { e = _error; console.log("parsing didn't work with " + tag); return "undefined"; } } }; };
from docxtemplater.
Thanks @edi9999 I can't seem to get the parser working though. Is this the correct setup:
angularParser = function(tag) {
var e, expr;
try {
expr = expressions.compile(tag);
} catch (_error) {
e = _error;
console.log("parsing didn't work with " + tag);
}
return {
get: function(scope) {
if (scope == null) {
console.log('warning: scope undefined');
}
try {
return expr(scope);
} catch (_error) {
e = _error;
console.log("parsing didn't work with " + tag);
return "undefined";
}
}
};
};
var documentPath = "input.docx";
// Create the Docx template file
var doc = new DocxGen({parser:angularParser}).loadFromFile(documentPath);
// Set the templates variables
doc.setTags({
"firstName" : req.body.firstName,
"lastName" : req.body.lastName,
"positions" : req.body.positions
})
// Apply the variables (replace all occurences of {firstName} by req.body.firstName, etc)
doc.applyTags()
// Create a file name from the users first name and last name
var filename = req.body.firstName + '_' + req.body.lastName + '_resume.docx';
var doc = doc.output({
name: filename
});
I noticed in the docs when you use the angular parser there's an extra data argument being passed in first new DocxGen(data,{parser:angularParser})
but I'm using the load from file wrapper, could this be the problem?
from docxtemplater.
Yes that's the issue. As you can see in the samples http://javascript-ninja.fr/docxgenjs/examples/demo.html
new DocxGen().loadFromFile("tagExample.docx",options)
Is the way I recommend.
This is a bit weird and will be better designed in version 1.0
from docxtemplater.
Also you have to require the angular expression package , but i guess you have done that
from docxtemplater.
Excellent, new DocxGen().loadFromFile("tagExample.docx",options)
worked!!
Thanks @edi9999 I really appreciate your help with that.
If you need any help testing v1 then I'll be happy to give you hand.
from docxtemplater.
Great that it helped ! The 1.x branch already exists and you could already test it but is only developped up to 30%.
You can maybe subscribe to the milestone 1.0 0 if you want to get notified of the questions I ask myself
from docxtemplater.
How do you subscribe to a milestone? I can't see anywhere to do that on the milestones page.
from docxtemplater.
Actually i don't know if it's possible. You can subscribe on all issues tough by watching the repository
from docxtemplater.
Hey @gilesbutler , version 1.0.0 is out (in beta), you can discuss it over there #92
from docxtemplater.
Related Issues (20)
- The parser parses '()' incorrectly in case of using custom delimiters like this '$(', ')' HOT 6
- dropUnsupportedFileTypesModules called twice by constructor HOT 3
- 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
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.