Phong Duong

🔎Search

Override mixin attributes in Pug

28-07-2020 | #pug

When you create a mixin in Pug, you usually use a default attribute value for all cases generally. Pug has an ability allowing you to override and change your default attributes. With &attributes, you can pass new attributes for specific cases and override default attributes

mixin inputField(type, id)
  input(id=id, type=type, required)&attributes(attributes)

+inputField("text", "name")
/*
<input id="name" type="text" required />
*/

// Override required attribute
+inputField("number", "age")(required=false)
/*
<input id="age" type="number" />
*/

If you prefer learning by watching video, you can watch this tutorial

https://youtu.be/ws2Ff_ESXic

Share: TwitterFacebookLinkedInHacker News