diff --git a/content/collections/pages/building-a-tag.md b/content/collections/pages/building-a-tag.md index 93cba3302..a6564f3e3 100644 --- a/content/collections/pages/building-a-tag.md +++ b/content/collections/pages/building-a-tag.md @@ -202,6 +202,51 @@ You may choose to create aliases for your tag too. It will then be usable by its protected static $aliases = ['sample']; ``` +## Namespaced Tags + +Tags can be registered under a namespace to avoid handle collisions between addons. Without namespacing, if two addons register a tag with the same handle, the last one registered wins. + +### Enabling namespacing in an addon + +Set the `$tagNamespace` property on your addon's service provider. All tags registered by that addon will be callable under that namespace. + +```php +class ServiceProvider extends AddonServiceProvider +{ + protected $tagNamespace = 'my-addon'; +} +``` + +::tabs + +::tab antlers +```antlers +{{ my-addon::my_tag:method }} +``` +::tab blade +```blade + +``` +:: + +When using the [fluent tag syntax](/blade#using-fluent-tags-with-blade), include the namespace: + +```php +Statamic::tag('my-addon::my_tag:method')->fetch(); +``` + +### Manual registration with a namespace + +You can also register a single tag class under a namespace directly: + +```php +MyTag::register('my-addon'); +``` + +:::tip +Namespacing is opt-in. Addons without `$tagNamespace` continue to work exactly as before. Tags can still be called without the namespace if there is no collision. +::: + ## Parameters You may get the values of parameters through the `parameters` property. diff --git a/content/collections/pages/building-an-addon.md b/content/collections/pages/building-an-addon.md index 79b61e24f..93638535d 100644 --- a/content/collections/pages/building-an-addon.md +++ b/content/collections/pages/building-an-addon.md @@ -234,6 +234,15 @@ protected $commands = [ ]; ``` +To avoid tag handle collisions with other addons, you can register all your tags under a namespace by setting `$tagNamespace` on your service provider: + +``` php +protected $tagNamespace = 'my-addon'; +``` + +Tags will then be callable as `{{ my-addon::tag_name }}`. See [Namespaced Tags](/building-a-tag#namespaced-tags) for full details. + + ## Assets ### CSS and Javascript