Skip to content

Custom Rules

inno-juanal edited this page Sep 11, 2019 · 6 revisions

If you need to generate a lot of HTML or XML content, it's a good idea to create common expression rules for individual elements and groups of elements.

The implementation of the common expression rule would call MarkupGen_html_newElem (or MarkupGen_xml_newElem for XML) at least once.

Example of custom rules

Consider the following example:

MarkupGen_html_toText(
  MarkupGen_html_newElem("div",  {class: "info"}, 
   {
     MarkupGen_html_newElem("h3", null, "Meeting Notice"),
     MarkupGen_html_newElem("p", null, "Please meet the team at 2:00."),
     MarkupGen_html_newElem( "a",  { href: "#someURL" },
        "Click here to confirm your attendance"
    )   
   }
  )
)

This produces the following HTML code:

<div class="info">
   <h3>Meeting Notice</h3>
   <p>Please meet the team at 2:00.</p>
   <a href="#someURL">Click here to confirm your attendance</a>
</div>

Because tags such as <div>, <p>, and <a> are very common in HTML generation, it would be useful to create common rules for each of them, so they can be used in more than one place.

With such rules, the above code would look like this:

MarkupGen_html_toText(
  rule!HTML_tag_div(
    class: "info", 
    contents: {
      rule!HTML_tag_h3(contents: "Meeting Notice"),
      rule!HTML_tag_p(contents: "Please meet the team at 2:00."),
      rule!HTML_tag_a(
        href: "#someURL",
        contents: "Click here to confirm your attendance"
      )   
    }
  )
)

Creating an HTML element custom rule

To create a rule such as HTML_tag_a, you may do this:

  1. Create a new expression rule named HTML_tag_a
  2. Create two rule input entries:
  • href of type Text
  • contents of type Any Type
  1. Enter the following rule content
markupgen_html_newelem(
  "a",
  {href: ri!href},
  ri!contents
)

You may follow the same pattern for any other rules.

Considerations when creating element rules

  • For elements with optional attributes, such as the 'class' attribute of a 'div', you'll need to check to see if the value was provided to decide whether to include it in the dictionary. In order to create dictionaries dynamically, you may want to use Dictionary Utilities, another publicly-available custom plugin developed by Innodev.
  • The type of contents can be Any Type. If you wanted to make it more explicit, you could use the type Array of MarkupGen_HtmlPart. The only problem with this is that you will need to call MarkupGen_html_newTextPart for any text you pass (such as "Click here to confirm your attendance" in the example) in order to convert text to a MarkupGen_HtmlPart instance.

Clone this wiki locally