Coder Social home page Coder Social logo

Comments (15)

shapiromatron avatar shapiromatron commented on May 14, 2024

Looks like I didn't RTFD- sorry

from docxtemplater.

edi9999 avatar edi9999 commented on May 14, 2024

Haha no problem. Great to see self closing issues

from docxtemplater.

zhouseahe avatar zhouseahe commented on May 14, 2024

@edi9999 , I think the feature is cool , now I need transfer json to object. haha

from docxtemplater.

gilesbutler avatar gilesbutler commented on May 14, 2024

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.

edi9999 avatar edi9999 commented on May 14, 2024

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.

gilesbutler avatar gilesbutler commented on May 14, 2024

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.

edi9999 avatar edi9999 commented on May 14, 2024

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.

gilesbutler avatar gilesbutler commented on May 14, 2024

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.

edi9999 avatar edi9999 commented on May 14, 2024

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.

edi9999 avatar edi9999 commented on May 14, 2024

Also you have to require the angular expression package , but i guess you have done that

from docxtemplater.

gilesbutler avatar gilesbutler commented on May 14, 2024

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.

edi9999 avatar edi9999 commented on May 14, 2024

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.

gilesbutler avatar gilesbutler commented on May 14, 2024

How do you subscribe to a milestone? I can't see anywhere to do that on the milestones page.

from docxtemplater.

edi9999 avatar edi9999 commented on May 14, 2024

Actually i don't know if it's possible. You can subscribe on all issues tough by watching the repository

from docxtemplater.

edi9999 avatar edi9999 commented on May 14, 2024

Hey @gilesbutler , version 1.0.0 is out (in beta), you can discuss it over there #92

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.