Is it possible to create aliases for pug mixins? I have a library of generic mixins for a project, but then have another mixin that when I call it I want to modify the behavior of some of the generic mixins within the body of the complex mixin. So, in code that I know doesn't work, I'd like to do something like:
//- Generic mixins
mixin input(inputObj)
//- does some things
//- Complex mixin
mixin complex
mixin _input = input
mixin input
//- does some slightly different things from the base input mixin.
//- and then calls the base input mixin via it's newly defined alias
_input
//- Invoked like so:
//- Invokes the base input
+input({name:'some name'})
//- invoke the complex section
+complex
//- Invoke the input defined in the complex section instead of the base mixin
+input({name:'name for complex mixin'})
Found the answer on how to do this. As a basic example of what I'm trying to do:
//- A basic mixin that just does some registering of the inputs that have been created using it.
mixin input(inputObj)
- inputObj.type = inputObj.type || 'text';
- registeredInputs.push(inputObj.name);
input&attributes(inputObj)
//- A mixin that defines a unique section where we want to log each form element that is created and adds a span before the created form element
mixin override
- pug_mixins._input = pug_mixins.input;
mixin overInput(inputObj)
- console.log('override input',inputObj);
span
|#{inputObj.name}
+_input(inputObj)&attributes(attributes)
- pug_mixins.input = pug_mixins.overInput;
block
-pug_mixins.input = pug_mixins._input;
//- Use the various forms of the mixins
+input({name:'before_override',type:'text'})
+override
+input({name:'in_override',type:'number'})
+input({name:'after_override',type:'hidden'})
The pug_mixins
object is created by the pug parser as part of parsing the pug language and we can use that to manipulate what mixin is actually called by a given mixin call.
Along with console logging the inputObj for the input created within the override, it creates the following html (prettified for easier demonstration):
<input name="attr_before_override" type="text" title="@{before_override}"/>
<span>in override</span>
<input name="attr_in_override" type="number" title="@{in_override}"/>
<input name="attr_after_override" type="hidden" title="@{after_override}"/>
Using this method, I can now create shadow versions of generic mixins in the project to add additional functionality when within certain other mixins. This will be useful for logging certain data and registering created data.