➕Mixins
Mixins are like functions and allow you to reduce repetitive content.
Naming
Mixins names should be written in camelCase
. Use UpperCamelCase
for exported mixins, more on that in the next chapter.
Parameters
Unlike functions, mixins have unordered parameters that must be set by name. When defining the mixin you may set defaults for parameters that are optional. Required parameters must always have a type, the type of optional parameters will be inferred from the default value.
Corgi may sometimes not be able to infer the type of an optional parameter by it's default expression. Don't worry, should that be the case, the corgi compiler will tell you.
In such cases it may be necessary for you to explicitly define the type, like for the bar
parameter below.
You can use any expression to set a parameter including ternary and chain expressions.
When using a chain expression, the parameter will only be set if the chain expression passes its checks. When using a chain expression for a required parameter you must provide a default.
If you have parameters that accept URLs or srcset values, you should set their type to woof.URL
or woof.Srcset
to leverage corgi's contextual escaping.
Consider the following example to illustrate:
This is probably not what you expected and can be solved by making href
's type woof.URL
:
Blocks
Blocks allow you to define placeholders for longer content.
Each block is named. If you only have a single block, it is conventional to call it _
.
You are not limited to just one block. If you want, you can use multiple of them.
Block Short Form
If you only want to fill the _
block, then you can use one of the block short forms. All of the below examples are equivalent.
Defaults and Checking If a Block Exists
Just like parameters, you can also define defaults for blocks.
Unlike parameters, there are no "required" blocks. If you don't define a default for a block, it is assumed that the default is simply nothing.
You can also use an if block
conditional to check if a block exists.
The &
-Operator
&
-OperatorYou can also use the &
operator inside the body of a mixin call to add attributes to elements with an &-placeholder:
Interpolation
Similar to expression interpolation, you can also interpolate mixins.
You can also fill the mixin's _
block, if you add [text]
in brackets, or an {expression}
in braces.
Mixins As Attribute Values
Mixins that only generate text (and no elements, or attributes) can be used as values for attributes.
The syntax is the same as for interpolated mixins, minus the leading #
.
Last updated