Include an Existing Fragment

Now we're going to introduce the most important part of the OpenMRS UI Framework: "fragments". Basically, we want to make every effort to break down our UI into fragments of pages. This has two key benefits:

  1. it lets us build a library of fragments that we can reuse all over the UI (and modules!) making application development much faster
  2. it allows us to let administrators change the way pages are layed out, and even compose their own pages, just by including fragments

First, we're going to include a fragment that's already written. Change the code of your helloWorld.gsp to

${ ui.includeFragment("uiframework", "helloUser") }

Now reload the page, and you should see a simple welcome message.

You're probably wondering what "ui" is. Since Groovy templates do not support tag libraries, we have collected a bunch of useful functionality in the "ui" object. (Your module can provide a utility function like this too.)

ui.includeFragment tells the UI framework to evaluate the given fragment and return its HTML as a String. Since we want to insert this into the page, we use the dollar-curly-brace construct to wrap the Groovy code.

The first argument to ui.includeFragment is a "fragment provider", which 99% of the time is the module id of the module the fragment is defined in. The second argument is the fragment name.

The uiframework module only contains this single helloUser fragment for test purposes. The uilibrary module includes standard helpful fragments.