Apply custom class using mustache syntax

Hi,

I want to use {{name}} with custom implementation class for mail merge like <<[x.fmt(name,"LIKE THIS")]>> so i want similar implementation in mustache syntax. How it is possible?

Thank you in advance.

@jilp

Cause

To implement custom functionality in a mail merge using Mustache syntax, you need to create a custom class that can handle the specific formatting or processing you want to achieve.

Solution

You can achieve this by creating a custom Mustache template engine that processes your placeholders. This involves defining a method in your class that formats the data as needed when the placeholder is encountered.

Code

Here’s a simple example of how you might implement this in Java:

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

public class CustomFormatter {
    public String format(String name) {
        return "LIKE THIS: " + name;
    }

    public static void main(String[] args) {
        CustomFormatter formatter = new CustomFormatter();
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile("template.mustache");

        Map<String, Object> context = new HashMap<>();
        context.put("name", formatter.format("John Doe"));

        StringWriter writer = new StringWriter();
        mustache.execute(writer, context);
        System.out.println(writer.toString());
    }
}

In this example, the format method in the CustomFormatter class processes the name variable. You would need to create a Mustache template file (e.g., template.mustache) that includes {{name}}.

Next Steps

  1. Create your Mustache template file with the desired placeholders.
  2. Implement your custom formatting logic in a class similar to the example above.
  3. Test the implementation to ensure that the formatting works as expected.

If you have any further questions or need additional assistance, feel free to ask!

using .net C# and i want something in tag as i’m using it like

<<foreach [part in file.parts]>>
<<[part.name]>>
   This paragraph is about {{abc.id}}. He/She/They is a named.

<</foreach>> 

@jilp Mustache syntax is classic mail merge syntax. Unfortunately, there is no way to use it with LINQ Reporting Engine. Also, mail merge does not provide such flexibility in template syntax as LINQ Reporting syntax. You should use LINQ Reporting syntax to achieve what you need.
If you need to apply custom formatting upon executing mail merge, you can consider using IFieldMergingCallback.

Thank you for your reply.

But somehow i want that functionality so i there any way to achieve this? As i’m using <<[]>> tag but in foreach loop other variable outside of file.parts is not accessible so i have to use {{}} tag and parallely i also want to have this custom functionality support like {{x.fmt(name, “LIKE THIS”)}}

@jilp To achieve what you need you should specify data source name and use the following syntax:

<<foreach [p in data.persons]>>
Name: <<[p.name]>>
City: <<[data.city]>>
<</foreach>>
JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json");

Document doc = new Document(@"C:\Temp\in.docx");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "data");

doc.Save(@"C:\Temp\out.docx");

data.zip (255 Bytes)

in.docx (14.1 KB)

out.docx (11.2 KB)

Thank you for your reply.

This is working but how should i handle docparts like <<doc [DOCNAME] -build>> ?
I tried adding data before like <<doc [data.DOCNAME] -build>>.

@jilp DOCNAME is the same field as other. So you should access them the same way as other fields in your data source.

but it is not working

@jilp Please provide your sample template, data and code that will allow us to reproduce the problem.