Proposal: Customizable Patient Dashboard Cards -- Twig Templates, Independent of LBF

I’d like feedback on an idea for making the patient dashboard easier to customize without editing PHP or LBF layouts.

The problem

Today the Demographics card is drawn entirely by the LBF layout engine. Over the years, I have heard many complaints about the LBF engine, which has become a bit antiquated.

LBF works well as a configurable data-entry form, but it’s an awkward way to control what a dashboard card shows. A practice that wants a compact demographics card, a different field order, or just the handful of fields its front desk uses has to change the LBF layout, which also changes the full edit form, or edit core templates, which breaks on upgrade.

I would like to add another option for templating forms, which can then be used to customize any card (not just demographics), in addition to be used in other parts of OpenEMR.

I still want to keep LBF layouts and just add another option.

The Idea: Two Kinds of Cards, Side by Side

Each dashboard card would use one of two renderers:

  • LBF card: exactly what we have today, unchanged.
  • Twig card: a template file that controls both the view and the edit layout, and doesn’t use LBF at all.

A site can use all LBF cards (the default, nothing changes), all Twig cards, or a mix. For example, the standard LBF Demographics card next to a compact Twig contact card:

primary:
  - { card: demographics, renderer: lbf }
  - { card: contact,      renderer: twig, template: contact.twig }

Templates go in a per-site folder (for example sites/<site>/templates/cards/), so they survive upgrades. Drop in a file and it becomes a card option.

What a template looks like

{#--- title: Contact (compact) ---#}
{% if mode == 'view' %}
  <dl class="oe-card-grid">
    <dt>{{ t('Cell') }}</dt>     <dd>{{ patient.phone_cell|phone }}</dd>
    <dt>{{ t('Email') }}</dt>    <dd>{{ patient.email }}</dd>
    <dt>{{ t('Language') }}</dt> <dd>{{ patient.language|list_label }}</dd>
  </dl>
{% else %}
  <form method="post" action="{{ save_url }}">
    {{ csrf_field() }}
    {{ input('phone_cell') }} {{ input('email') }} {{ input('language') }}
    <button class="btn btn-primary">{{ t('Save') }}</button>
  </form>
{% endif %}

Basic use is HTML with blanks to fill in. Conditions, loops, formatting helpers (dates, phone numbers, SSN masking), translation and shared partials are there for anyone who wants more.

How it works without LBF

  • Field definitions in code. A typed field list in src/ describes each patient_data field: label, type (text, date, phone, email, select, checkbox…), dropdown list, required, maximum length and permission. Sites can add or override fields in a small YAML file. Dropdown choices still come from the standard list_options lists.
  • Reading goes through the existing service layer (PatientService and related services), the same code the REST/FHIR API uses.
  • Inputs are drawn by a small set of Twig widgets based on each field’s type. Sites can restyle them.
  • Saving goes to a normal PHP endpoint. It checks the CSRF token and write permission, accepts only the fields the template actually displayed, validates with the existing PatientValidator, and saves with PatientService::update(), again the same path as the API.

Because Twig cards read and write through the service layer, building them also exercises and improves the code the API depends on.

No new dependencies, and safe for user-supplied files

Templates run in Twig’s built-in sandbox mode. Twig is already in OpenEMR, and sandbox mode is designed for untrusted templates. Templates can only use an allow-listed set of tags, filters and data. They can’t run PHP, query the database or read files, and output is always escaped. Templates never write anything; all saving happens in trusted PHP on submit. A broken template shows an error on that card only (details logged for admins), never a broken dashboard.

Things I already know need care

  • The LBF save (updatePatientData()) and the service-layer save (PatientService::update()) may differ in history tracking, audit logging and events. They should be compared first, and the service fixed where needed, so both paths behave the same.
  • LBF layout settings (required, hidden, skip conditions) won’t apply to Twig cards. That’s intentional, but it must be documented.
  • When an LBF card and a Twig card show the same field, saving one should refresh the other, and a stale edit form shouldn’t silently overwrite newer data.
  • Address, phone and relationship fields have special handling today. They’d start out read-only in Twig cards.

Proposed phases

  1. Field definitions, view-only Twig cards, and dashboard configuration mixing LBF and Twig cards.
  2. Edit mode for simple patient_data fields through PatientService and PatientValidator.
  3. Employer, contact and other cards through their own services.
  4. An admin screen to manage cards and fields without editing files.

I’d like to start as a custom module using the existing dashboard card hooks (SectionEvent / CardModel), so no core changes are needed to try it. If it proves useful, the base pieces could move into core.

Questions

  • Would your practice use this? Which cards would you most want to customize?
  • Per-site template folder, admin screen, or both?
  • Is Twig with the sandbox simple enough for the people who would write these templates?
  • Maintainers: any concerns with the approach, especially using PatientService / PatientValidator for saves, before I start a prototype?

Thanks for any feedback.

David Eschelbacher

Update: mockups, findings, and why I think this needs to be in core

A few follow-ups since my original post: what it could look like, what I found in the code, a second use for the same idea, and a concrete plan. All patients, phone numbers and amounts in the mockups are made up, and nothing is built yet. The mockups use OpenEMR’s current light theme and Bootstrap 4, so nothing new is needed on the styling side.

1. What a Twig card looks like next to an LBF card

  • Top left is the Demographics card as it is today (renderer: lbf). It stays exactly as it is, and stays the default.
  • Top right is a Twig card (renderer: twig) that a practice laid out for a quick look: language and interpreter, cell phone and SMS consent, email, portal status, PCP, pharmacy, last and next visit, insurance eligibility and balance. It pulls from several services (patient, insurance, appointments, billing), and anything the user isn’t allowed to see is removed before the template gets it.
  • Bottom left is the same card in edit mode after a failed save. Only the fields the card displayed can be saved; address, insurance and balance are read-only here. More on saving in section 3.
  • Bottom right is everything the practice edits: one template file and one line in the dashboard config. LBF, Twig and built-in cards can sit side by side.

LBF is great for data entry and for practices that already have their layouts set up; this just gives a second option for cards that are mostly about display.

2. This can’t be done as a module

My original post suggested starting as a module. After looking at the code, that won’t work for the main goal. In demographics.php, core dispatches SectionEvent('primary') and then adds its own cards (Demographics, Billing, Insurance) after listeners have run. SectionEvent only has addCard(); it can’t remove or replace a card. So a module could add a Twig card next to the LBF Demographics card, but could never replace it or let a site choose LBF or Twig per card. That requires a change to how core builds the dashboard.

3. Saving: the LBF save vs PatientService::update()

Templates never write anything. A Twig card’s save goes through fixed PHP code: CSRF and patients/demo write check, accept only the fields the card displayed, validate with PatientValidator, save with PatientService::update() (the same path as the REST API).

Comparing that with the LBF save, the two are closer than I expected. updatePatientData() (the LBF save) already calls PatientService::databaseUpdate(), so both share the same SQL, audit logging and BeforePatientUpdatedEvent / PatientUpdatedEvent. The API’s update() method validated with PatientValidator (the LBF path doesn’t), but it wrote its own update instead of calling databaseUpdate(), which left three gaps:

  • it didn’t set updated_by, so patient edits made through the REST/FHIR API didn’t record who made them
  • it skipped the portal-username fix-up that databaseUpdate() does
  • its PatientUpdatedEvent payload was shaped differently (the “after” data was a list of records, which breaks listeners such as the telehealth module’s patient registration)

That’s an API bug whether or not this proposal goes anywhere, so I created a PR to fix it: PR #15459 makes update() validate and then save through databaseUpdate(), so the API and the demographics form now behave the same. With that fix, a Twig card can save through update() and get validation plus everything the LBF save does.

Also, demographics_save.php fires PatientUpdatedEventAux with the raw form data (Weno uses this for pharmacies), and saves addresses, phone numbers and relationships through their own services. Twig cards would leave those fields read-only at first.

4. Twig sandbox security

Twig’s sandbox had a number of bypass CVEs in 2024–2026, all fixed by 3.27; OpenEMR is on 3.30.0. To keep the risk low:

  • a separate Twig environment and cache for user templates, sandbox always on, strict mode enabled
  • templates get only plain data, no service objects or methods
  • a small allow-list: no raw, no callable filters
  • data filtered by permission before rendering
  • templates as files on disk only at first

5. A second use: the day’s schedule, outside the calendar

This isn’t only about dashboard cards. As part of the Visit List calendar work (calendar changes proposal), I’m planning a Daily Schedule window: one provider, one date, every patient in order, with what the front desk and the provider need at a glance. Here it is for a regular clinic day:

Visit type and reason, insurance eligibility, copay and balance, alerts (allergies, overdue labs, referral or prior auth needed, intake forms), status with room and wait time, and the next appointment. Open slots have a Book button, and status and reason can be changed right there, then saved together with Update.

The Template dropdown is the point: a front desk wants insurance and balances, a nurse wants rooming and alerts, a provider wants the reason and history. Each is a different template over the same data, and the PDF button renders the same template, so the screen and the printout match.

And the same page for a practice that does home visits:

Visit order, stop numbers (two patients at one address share a stop), address and phone for routing, gate codes and other patient notes, practice-specific flags, status, follow-up interval and next appointment. There would also be a Total Schedule window covering a date range and several providers (for example, sorted by ZIP for routing home visits).

Every practice wants these pages laid out differently, so it’s the same problem as the cards, and it works the same way:

  • templates in sites/<site>/templates/schedule/, with a shipped default
  • the controller loads the data in a few batched queries, with permissions and provider/facility scope already applied, and passes it as plain data
  • the template can lay out, sort, group and format that data, but can’t fetch anything else or save
  • practice-specific fields (flags, patient notes) come from fields the admin picks, so core never hard-codes them
  • sensitive fields (SSN, insurance IDs) appear only if the admin turns them on for that template
  • editing (status, appointment comment, visit order, follow-up interval) goes through one fixed controller that checks permissions and saves through the services

With two features needing the same thing, I think the sandboxed renderer belongs in core as shared infrastructure, not inside either feature.

6. Proposed plan: small core PRs, each useful on its own

  1. Fix PatientService::update() (updated_by, portal fix-up, consistent event payload). A bug fix for the API regardless of this proposal. Done: PR #15459, waiting for review.
  2. Allow dashboard cards to be replaced or removed through SectionEvent, with core cards added before the dispatch. Useful for modules too; no change for users.
  3. A shared sandboxed template renderer in src/Common/Twig/, with tests. Used by both the dashboard cards and the schedule windows; letters and portal document templates could use it later.
  4. A field list and read-only patient data object in src/Patient/, with tests.
  5. A template-based card type (TemplateCard extends CardModel) that loads templates from a per-site folder, off by default behind a global setting, with one example template. The LBF Demographics card stays the default.
  6. Dashboard configuration to choose LBF or Twig per card.
  7. Edit mode for simple patient_data fields through the fixed PatientService::update().

Nothing changes for existing sites unless an admin turns it on, and PRs 1 and 2 don’t involve Twig at all.

7. Questions

  • Practices: would you use cards or a schedule page like these? What would you put on yours?
  • Maintainers: before I put real time into PRs 3–7: if this were built as a series of small core PRs like the above, off by default and with LBF cards unchanged, would you be open to accepting it into core, including a shared sandboxed template renderer used by both the dashboard cards and the schedule windows? PR 1 is already up; I’m happy to do PR 2 next if that looks reasonable on its own.

Thanks.

— David

Hi David, I like this a lot from the implementer side. Practices often ask us for a compact demographics card, and today the only answers are changing the LBF layout, which also changes the edit form, or patching core templates, which breaks on upgrade. A few suggestions:

  1. Keep practice templates under sites/<site>/ (or a custom directory set in Globals) so upgrades never overwrite them, and use the core template only as a fallback.
  2. If a custom template fails to render, fall back to the LBF card and log the error instead of breaking the dashboard.
  3. Enforce ACLs in the data layer, not in the template, so a card can’t show billing or sensitive fields to a role that shouldn’t see them.
  4. Let practices choose the card per user group, because front desk and providers usually want different fields.

Happy to test the PR against a few real-world layouts once it’s up.