The system behind every document eSerbisyo produces: a find-and-replace engine that reads a .docx template, injects live data, and outputs a signed, QR-stamped PDF.
At its core the template engine is a very sophisticated “fill in the blanks” system. A barangay staff member creates a normal Word document (a clearance, a certificate, whatever) and drops in placeholder tags where the variable data should go. When a request is approved, the engine reads those tags and swaps each one for the real value: the resident’s name, the date, the address, and so on.
Think of it like a mail-merge you might have used in a word processor, except it happens automatically, at scale, without anyone opening the file manually. The staff member who designed the template never needs to touch code (they just type{{fullName}} where the name should appear and the engine handles the rest.
To Whom It May Concern:
This is to certify that {{fullName}}, born on {{dateOfBirth}}, and
currently residing at {{address}}, is a bonafide resident of Barangay
{{barangayName}}, City of {{city}}.
This certification is issued upon the request of the above-named
individual for whatever legal purpose it may serve.
Issued this {{dateIssued}} at Barangay {{barangayName}}.
_________________________
{{captainName}}
Barangay CaptainThe template above is a plain .docx file. The placeholders inside double curly braces are the only “code” in the entire document. Every other piece of formatting (fonts, margins, letterhead, signatures) stays exactly as the designer set it up.
A .docx file is not a single opaque blob. It is a ZIP archive containing a collection of XML files. The text you see in Word lives inside word/document.xml, along with every formatting instruction, image reference, and style. Fonts, headers, footers, and even the spell-check dictionary each get their own XML file inside the archive.
The template engine exploits this structure directly. When a template is uploaded, it is unzipped in memory. The engine parsesword/document.xml with an XML parser, walks every text node, and collects every run of text that matches the{{...}} pattern. Because it operates at the XML level rather than at the display level, it can handle placeholders that span multiple Word “runs” (Word sometimes splits a single word into several runs due to formatting changes). The engine reassembles these fragments before replacing them.
This also means the engine is format-agnostic. Bold, italics, tables, and images are all preserved exactly, because the engine never rewrites formatting. It only modifies the text content of the nodes that contain a placeholder.
Each placeholder tag corresponds to a field in the request form. Some fields are filled automatically by the system (for example, the resident’s name and address come from the profile they already created during registration). Other fields are filled by the staff member when they encode a walk-in request, or by the resident when they submit an online request.
The mapping is defined in the document type’s configuration. Here is a simplified version of what that looks like:
{
"documentType": "Barangay Clearance",
"templateFile": "templates/barangay-clearance.docx",
"placeholders": {
"fullName": { "source": "resident", "field": "fullName" },
"dateOfBirth": { "source": "resident", "field": "dateOfBirth" },
"address": { "source": "resident", "field": "address" },
"barangayName": { "source": "system", "field": "barangayName" },
"city": { "source": "system", "field": "city" },
"captainName": { "source": "system", "field": "captainName" },
"dateIssued": { "source": "auto", "field": "currentDate" },
"purpose": { "source": "request", "field": "purpose" }
}
}The source key tells the engine where to pull the value from:
Auto-filled tags like {{dateIssued}} and{{barangayName}} are invisible to the end user (they never see a form field for them). User-filled tags like{{purpose}} appear as editable fields in the request form. This split is what makes the system both flexible and safe: staff only need to type what is unique to each request.
Once all placeholders are resolved, the engine has a completed XML document sitting in memory. But a Word XML document is not what residents or staff need (they need a universally readable PDF). The pipeline that produces the final file has four stages:
word/document.xml is loaded, and every placeholder node is identified).The same pipeline runs for every document type. Whether it is a barangay clearance, a certificate of residency, or a business permit, the engine follows the exact same four steps. Changing a template does not require a code change (just upload the new .docx and the engine adapts automatically).
The template engine is not just a technical convenience. It solves real, daily pain points in a barangay office:
Barangay admins manage templates through the admin panel. The workflow is designed to be as simple as possible so that non-technical staff can handle it end to end:
{{...}} tag in the uploaded document and presents a mapping form. The admin assigns each placeholder to a source (resident profile, system setting, auto-generated value, or request field).Version history is maintained automatically. If an admin uploads a new template, the old one is archived. This means the system can still re-generate a PDF for a past request using the original template it was filed under, even if a newer version exists.
Template changes are live immediately. There is no need to restart the server, rebuild anything, or involve a developer. The admin uploads the file, maps the tags, and publishes (the entire process takes less than five minutes).