Skip to content

Mixins (The Pit Crew)

In the pit lane, we don’t build a new tire wrench for every car. We use standard, reusable tools. In Pug, these are called Mixins. They function like JavaScript functions but for HTML structure. You define a template “tool” once and strictly reuse it.

A schematic of a "Pit Gun" (Mixin). Inputs: Arguments (Pressure, Torque). Output: A perfectly secured wheel nut (HTML Component).

Use the mixin keyword followed by the name and optional arguments into parentheses.

//- Definition
mixin petCard(pet)
.card
.card-header
h2= pet.name
.card-body
p Breed: #{pet.breed}
p Age: #{pet.age}

To “call” the mixin, use the + symbol. This is the implementation trigger.

//- Usage
+petCard({ name: "Drago", breed: "Doberman", age: 4 })
+petCard({ name: "Rex", breed: "German Shepherd", age: 2 })

Mixins can also accept a block of content, allowing you to wrap custom HTML inside a reusable container.

mixin modal(title)
.modal
h3= title
.modal-content
if block
block
else
p No content provided.
//- Usage
+modal("Security Alert")
p Unauthorized entry detected in Sector 7.
button.btn-lockdown Seal Doors

Pug Mixins and Layouts - GitHub Repo


Mixins handle small components. But what about the main frame?

Next Lap: Layouts, Extends, and Blocks (layout.pug).