Coder Social home page Coder Social logo

Comments (14)

edi9999 avatar edi9999 commented on May 28, 2024 1

If you want to test if the array is empty, you can use the inversed loop tags:

{#users}
User: {name}
{/users}

{^users}
No Users ...
{/users}

Will output "No Users ..." if the users array is empty

from docxtemplater.

edi9999 avatar edi9999 commented on May 28, 2024

Hey,

the way to do if else is to use table templating:

 {#ifFormatting}
     //table 1
 {/ifFormatting}

 {#elseFormatting}
    // table 2
 {/elseFormatting}

with the following data:

 {
     "ifFormatting":[],  //This will not show what's inside ifFormatting
     "elseFormatting":[{tableData:""}] //This will show what's inside elseFormatting once
 }

from docxtemplater.

edi9999 avatar edi9999 commented on May 28, 2024

I will maybe add the possibility to use "ifFormating":true instead, without changing the working scope, so that would be the same as what Mustache does

from docxtemplater.

edi9999 avatar edi9999 commented on May 28, 2024

Actually I have added this functionality:

If the docx template is

{#ifFormatting}
{/ifFormatting}

and ifFormatting===true, then what is inside the scope will be shown and the scope won't be changed.

from docxtemplater.

cmd030 avatar cmd030 commented on May 28, 2024

how can i use array.length in if condition.
{#array.length}
{some_actions_with_array}
{/array.length}
seems to be not working

problem when i use {-w:p smth}{smth} when the array is empty

from docxtemplater.

cmd030 avatar cmd030 commented on May 28, 2024

got u, thanks)

from docxtemplater.

crivera avatar crivera commented on May 28, 2024

hi

can you also do something like this

{#myVar == 'value1'}
// pring if myVar is value1
{/}

Thanks

from docxtemplater.

edi9999 avatar edi9999 commented on May 28, 2024

Yes, search in the docs for angular parser

from docxtemplater.

adadi-arc avatar adadi-arc commented on May 28, 2024

i need if else condition like this

{#dogName} German {/} else not found

from docxtemplater.

edi9999 avatar edi9999 commented on May 28, 2024

Hello @adadi-arc ,

there are two possibilities :

  1. Use {#dogName} and then {^dogName}, like this :
{#dogName}German{/}{^dogName}not found{/}

or you can also use the angular expression parser and a ternary :

  1. {dogName ? " German " : "not found"}

This second solution needs the following code :

const expressionParser = require("docxtemplater/expressions.js");
const doc = new Docxtemplater(zip, {
    paragraphLoop: true,
    linebreaks: true,
  parser: expressionParser,
});
doc.render({
    first_name: "John",
    last_name: "Doe",
    phone: "0652455478",
    description: "New Website",
});

from docxtemplater.

adadi-arc avatar adadi-arc commented on May 28, 2024

I have a question. Suppose there's a paragraph on a new page displayed depending on a condition. I want it to show the text when the condition is true; otherwise, it should generate a blank page. However, I don't want it to generate a blank page if the condition is false

from docxtemplater.

edi9999 avatar edi9999 commented on May 28, 2024

I think this could be solved by using the following trick :

https://docxtemplater.com/docs/faq/#adding-page-break-from-a-tag

from docxtemplater.

adadi-arc avatar adadi-arc commented on May 28, 2024

I'm encountering this error when using page break.
the tag \ pagebreak\ is not inside a paragraph, putting raw tags inside an inline loop is disallowed. ,

Can we connect on WhatsApp to discuss it?

from docxtemplater.

edi9999 avatar edi9999 commented on May 28, 2024

Please make sure first to upgrade to the latest docxtemplater version, 3.45.0

If you write your document like this :

{#cond}
{@pageBreak}
{text}
{/}

And use the following in your code :

const doc = new Docxtemplater(zip, {
    paragraphLoop: true,
    linebreaks: true,
});
doc.render({
    cond: true,
    pageBreak: '<w:p><w:r><w:br w:type="page"/></w:r></w:p>',
    text: "Hello world",
});

(The paragraphLoop: true option is important, it will automatically remove empty paragraphs inside those types of loops).

It is not possible to add a {@Pagebreak} inside a given paragraph, for example :

Foo{@pageBreak}

is invalid, as well as :

{#cond}{@pageBreak}{/}

You have to write :

{#cond}
{@pageBreak}
{/}

If that still doesn't work and you have applied everything above, please send your template.

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.