Order of KirbyText/KirbyTags hooks
Available hooks
When you call the kirbyText()/kt()
field method, a lot of stuff happens to the raw text until the final result is rendered. During this process, four different hooks are called:
But in which order are these hooks called and what does this mean when we want to do something when these hooks are triggered?
Order of hooks
The hooks are called in the following order:
kirbytext:before
kirbytags:before
kirbytags:after
kirbytext:after
kirbytext:before
The kirbytext:before
hook is triggered before any parsing is applied to the raw text. At this state, you can therefore manipulate the raw text of the field.
kirbytags:before
The kirbytags:before
hook kicks in directly afterwards, i.e. before KirbyTags are parsed, but after modifications through a kirbytext:before
hook.
kirbytags:after
The kirbytags:after
hook kicks in after modifications through a kirbytags:before
hook and after all KirbyTags are parsed, but before Markdown and SmartyPants are parsed.
kirbytext:after
The final hook is the kirbytext:after
hook. Changes you make in this hook are applied to the rendered HTML after Markdown and SmartyPants are parsed. You could therefore use this hook to replace or modify HTML tags, e.g. replace all headings with anchor headings and stuff like that.
By choosing the right hook, you have full control over what modifications happen when and what further modifications are applied.
Related: