Skip to content

ako/MxMustache

Repository files navigation

MxMustache, templating for mendix

Use Mustache templates in Mendix Microflows. Includes support for Markdown to Html conversion. Based on JMustache with extensions for formatting. For formatting it uses PegDown.

Usage

This module contains 2 java actions:

  • FillTemplate - fills a JMustache template using data from Mendix Object.
  • SendEmail - to send emails, e.g., based on text generated with mustache template.

FillTemplate

FillTemplate has the following parameters:

  • Template - string containing a mustache template
  • Data - Mendix object containing the data you want to use in your template. You can use associations to include related objects. See examples below.
  • RunMarkdown - run the result of the template through markdown processor to generate html. Assumes the template contains markdown. See examples below.
  • NoObjectLevels - how many levels of associations need to be included from the root object.

SendEmail

SendEmail has the following parameters:

  • To - To address
  • From - From address
  • ReplyTo - Reply to address
  • Subject - Subject of email
  • Contents - contents of the email
  • Attachment - String to be sent as attachment with the email
  • AttachmentMimeType - Mime type of the attachment, e.g. 'text/html'.
  • AttachmentFilename - Filename that you want to be used for the attachment.
  • AttachmentDocument - Mendix FileDocument object to be attached to the email.
  • SmtpHost - Host name of the smtp server to be used
  • SmtpPort - Port of the smtp server to be used
  • SmtpUsername - Username for connecting with the smtp server
  • SmtpPassword - Password for connecting with the smtp server
  • UseSsl - Use ssl for connecting to the smtp server?

If you want to use gmail smtp server, specify host smtp.gmail.com, port 465, ssl is true. You also need to turn on less secure apps for the gmail account, otherwise you'll run into AuthenticationFailException.

Examples

Basic single object

Uses a single object of instance Car. Template refers to the attributes in the entity. Example can be found in the tests module:

Brand: {{Brand}}
Model: {{Model}}
HasTurbo: {{HasTurbo}}
CarId: {{CarId}}
Price: {{Price}}
Doors: {{Doors}}
Color: {{Color}}
DateIntroduction: {{DateIntroduction}}

Please check ModelShare for an example how to use this in a microflow: microflow to generate a string based on the template with MxMustache.

One to many association

Uses a CarList object with associations to 10 Cars. Result is a CSV string.

Brand,Model,HasTurbo,CarId,Price,Doors,Color,DateIntroduction
{{#Car_CarList}}
{{Brand}},{{Model}},{{HasTurbo}},{{CarId}},{{Price}},{{Doors}},{{Color}},{{DateIntroduction}}
{{/Car_CarList}}

You can find an example microflow on Mendix model share: Test csv template multiple objects.

One to many association using markdown to create an Html table

This template uses markdown to create an html table. Includes JMustache extensions to specify formatting: money, dd-MM-yyyy:

| Brand | Model | HasTurbo | CarId | Price | Doors | Color | DateIntroduction |
|:----- |:----- |:-------- | -----:| -----:| -----:|:----- |:---------------- |
{{#Car_CarList}}
| {{Brand}} | {{Model}} | {{HasTurbo}} | {{CarId}} | {{Price | money}} | {{Doors}} | {{Color}} | {{DateIntroduction | dd-MM-yyyy}} |
{{/Car_CarList}}

The resulting html snippet can be used with the document generator to generate a pdf.

You can find an example microflow on Mendix model share: Test markdown template multiple objects. You can find an example with pdf generation here: Test markdown pdf template multiple objects.

Generate JSON document with one to many association

You can use templating to generate JSON strings:

[
{{#Car_CarList}}
{ "Brand" : "{{Brand}}",
  "Model" : "{{Model}}",
  "HasTurbo" : {{HasTurbo}},
  "CarId" : {{CarId}},
  "Price" : {{Price}},
  "Doors" : {{Doors}},
  "Color" : "{{Color}}",
  "DateIntroduction" : "{{DateIntroduction | yyyy-MM-dd''T''HH:mm:ss.SSSZ}}"
},
{{/Car_CarList}}
]

You can find an example microflow on Mendix model share: Test json template multiple objects

Generate XML document with one to many association

You can also generate XML in a similar fashion:

<Cars>
{{#Car_CarList}}
  <Car>
   <Brand>{{Brand}}</Brand>
   <Model>{{Model}}</Model>
   <HasTurbo>{{HasTurbo}}</HasTurbo>
   <CarId>{{CarId}}</CarId>
   <Price>{{Price}}</Price>
   <Doors>{{Doors}}</Doors>
   <Color>{{Color}}</Color>
   <DateIntroduction>{{DateIntroduction | yyyy-MM-dd''T''HH:mm:ss.SSSZ}}</DateIntroduction>
  </Car>
{{/Car_CarList}}
</Cars>

You can find an example microflow on Mendix model share: Test xml template multiple objects

History

  • 0.1
    • Initial release
  • 0.2
    • New action to fill template using json string
    • Migrated project to Mendix 7.6

About

Mendix mustache templating

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published