PHPackages                             bjuppa/metatagbag - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bjuppa/metatagbag

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bjuppa/metatagbag
=================

A convenient collection for HTML meta tags

v2.0.1(1mo ago)04.4k↓50%[6 issues](https://github.com/bjuppa/metatagbag/issues)1MITPHPPHP ^8.2CI passing

Since Jul 28Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/bjuppa/metatagbag)[ Packagist](https://packagist.org/packages/bjuppa/metatagbag)[ RSS](/packages/bjuppa-metatagbag/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (21)Used By (1)

Meta Tag Bag
============

[](#meta-tag-bag)

A PHP class for collecting and manipulating [HTML meta tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta)before echoing in the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head). Works well with Laravel, and without.

Inspired by [Laravel's `MessageBag`](https://laravel.com/api/master/Illuminate/Support/MessageBag.html).

```
composer require bjuppa/metatagbag
```

Table of contents
-----------------

[](#table-of-contents)

- [Creating a `MetaTagBag`](#creating-a-metatagbag)
- [Input Formats](#input-formats)
- [HTML Output](#html-output)
- [Adding Tags](#adding-tags)
- [Removing Tags](#removing-tags)
- [Filtering Tags](#filtering-tags)
- [Inspecting a `MetaTagBag`](#inspecting-a-metatagbag)
- [Sorting Tags](#sorting-tags)
- [Optional Manipulation](#optional-manipulation)
- [Converting to json](#converting-to-json)
- [Alternative packages](#alternative-packages)

Creating a `MetaTagBag`
-----------------------

[](#creating-a-metatagbag)

```
use Bjuppa\MetaTagBag\MetaTagBag;

$bag = new MetaTagBag(
  ['name' => 'description', 'content' => 'A description'],
  ['name' => 'keywords', 'content' => 'key,words']
);

// ...or using a static creator:

$bag = MetaTagBag::make(
  ['name' => 'description', 'content' => 'A description'],
  ['name' => 'keywords', 'content' => 'key,words']
);
```

Input Formats
-------------

[](#input-formats)

All methods that operate on some kind of list of meta tags will accept almost any type of map-like (key-value) input, optionally nested in some kind of list.

### Tags can be in separate arguments

[](#tags-can-be-in-separate-arguments)

The most terse syntax can be seen in the creation examples above, where multiple tags are supplied, each as its own argument to the method.

### Lists of tags are flattened

[](#lists-of-tags-are-flattened)

If some kind of nested list is encountered, it will be flattened so that any item lacking a "string" key will become its own tag in the resulting *one-dimensional* list of tags.

```
$bag = new MetaTagBag(
  [
    ['name' => 'description', 'content' => 'A description'],
    ['name' => 'keywords', 'content' => 'key,words'],
    [
      ['name' => 'nested', 'content' => 'This will end up in the top-level with the other tags'],
    ]
  ]
);
```

### Json strings are deserialized

[](#json-strings-are-deserialized)

If a string is encountered within a supplied list, attempts will be made to deserialize it from json.

```
MetaTagBag::make('[{"name":"description","content":"A description"},{"name":"keywords","content":["key","words"]}]');
```

### Objects are converted to arrays

[](#objects-are-converted-to-arrays)

If an object is encountered within a supplied list, it will be converted to an array, and merged into the flattened list. Implementations of [Laravel's `Arrayable`](https://laravel.com/api/master/Illuminate/Contracts/Support/Arrayable.html), like [Laravel's `Collection`](https://laravel.com/api/master/Illuminate/Support/Collection.html)and other `MetaTagBag`s will work just fine. Implementations of [`Bjuppa\MetaTagBag\Contracts\MetaTagProvider`](https://github.com/bjuppa/metatagbag/blob/master/src/Contracts/MetaTagProvider.php)will pull out that instance's `MetaTagBag`.

```
MetaTagBag::make(new MetaTagBag(['name' => 'description', 'content' => 'A description']));
```

HTML Output
-----------

[](#html-output)

The `MetaTagBag` should usually be rendered first within the `` element, before any other elements like ``. This is because it may contain a `charset` meta tag that should come before any other content.

```
// Return a string of HTML tags from the bag's contents
$bag->toHtml();
```

`MetaTagBag` implements [Laravel's `Htmlable` contract](https://laravel.com/api/master/Illuminate/Contracts/Support/Htmlable.html)so in a [Blade template](https://laravel.com/docs/blade) you can echo the tags by putting any instance within curly braces:

```

{{ Bjuppa\MetaTagBag\MetaTagBag::make(['name' => 'description', 'content' => 'A description']) }}
Page title

```

Casting a `MetaTagBag` to `string` will also bring out the HTML representation:

```
echo $bag; //Implicit string casting
$html = (string) $bag; //Explicit string casting
```

### Output of comma-separated list attributes

[](#output-of-comma-separated-list-attributes)

For HTML, any array attribute will be imploded into a comma-separated list. This can for example be used with a `name="keywords"` meta tag, where the keywords in the `content` attribute can be treated as a list until the time of rendering.

Adding Tags
-----------

[](#adding-tags)

The `add(...$tags)` method will modify the `MetaTagBag` instance, adding any tags supplied without checking for duplicates.

The `merge(...$tags)` method will also modify the `MetaTagBag` instance, but will overwrite any existing tags having the same `name`, `http-equiv`, `itemprop`, or `property` attributes.

### Merging array attributes

[](#merging-array-attributes)

If a tag to be merged has an array as its `content` attribute, that array will be merged with the `content` of any existing matching tag in the bag. This can for example be used with `name="keywords"` meta tags, where one may want to add keywords, rather than overwriting them.

Removing Tags
-------------

[](#removing-tags)

The `forget(...$attributes)` method will remove all matching tags from the `MetaTagBag` instance.

Filtering Tags
--------------

[](#filtering-tags)

The `match(...$attributes)` method can be used to filter out matching tags into a new `MetaTagBag`.

The `unique()` method returns a new `MetaTagBag` where all duplicate tags have been removed (keeping the last).

In addition, if `unique(...$attributes)` is called with arguments, matching tags will only appear once in the returned `MetaTagBag`(also keeping the last).

Inspecting a `MetaTagBag`
-------------------------

[](#inspecting-a-metatagbag)

The methods `count(...$attributes)` and `has(...$attributes)` can be used to count matching tags or check if any matching tags exist in a bag. Of course, `count()` can be called without arguments to return the total number of tags in the bag, while calling `has()` without arguments will always return `false`.

The `content($attributes)` method will pull out the *value* of the `content` attribute of the last matching tag. It's a wrapper around `getLastMatchingAttributeValue($attributeToGet, $attributesToMatch)`that does the same for any attribute.

Sorting Tags
------------

[](#sorting-tags)

The `sort()` method called without arguments will return a new `MetaTagBag` instance where `charset`and `http-equiv="X-UA-Compatible"` tags are placed first.

If a callback is given, it will be used just like [PHP's `uasort` parameters](https://secure.php.net/manual/en/function.uasort.php#refsect1-function.uasort-parameters).

Optional Manipulation
---------------------

[](#optional-manipulation)

The `pipe(callable $callback)` method passes the `MetaTagBag` to the given callback and returns the result. For example it can be used to fluently check if a `MetaTagBag` contains some tag, and if so add or remove some other tag.

Converting to json
------------------

[](#converting-to-json)

`MetaTagBag` is [`JsonSerializable`](http://php.net/manual/en/class.jsonserializable.php)so instances can be supplied directly to [PHP's `json_encode()`](http://php.net/manual/en/function.json-encode.php) function. Also, because `MetaTagBag` implements [Laravel's `Jsonable` contract](https://laravel.com/api/master/Illuminate/Contracts/Support/Jsonable.html), there's also the `toJson()` method.

Alternative packages
--------------------

[](#alternative-packages)

For a more opinionated end-to-end solution for meta tags in Laravel apps you should check out [butschster/LaravelMetaTags](https://github.com/butschster/LaravelMetaTags).

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance90

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 98.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~174 days

Recently: every ~548 days

Total

17

Last Release

49d ago

Major Versions

v0.5 → v1.02018-08-13

v1.1.3 → v2.0.02026-02-27

PHP version history (3 changes)v0.1PHP ^7.1.0

v1.1.3PHP ^7.1.0 || ^8.0

v2.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/c7d75e38113b7a075f11d2d715ab47987cb6f37d6758d111d65858aaf44fe4a5?d=identicon)[bjuppa](/maintainers/bjuppa)

---

Top Contributors

[![bjuppa](https://avatars.githubusercontent.com/u/5339269?v=4)](https://github.com/bjuppa "bjuppa (141 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (2 commits)")

---

Tags

hacktoberfesthtmlmetadataphplaravelcollectionmeta tagbag

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bjuppa-metatagbag/health.svg)

```
[![Health](https://phpackages.com/badges/bjuppa-metatagbag/health.svg)](https://phpackages.com/packages/bjuppa-metatagbag)
```

###  Alternatives

[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[tonysm/importmap-laravel

Use ESM with importmap to manage modern JavaScript in Laravel without transpiling or bundling.

148399.8k1](/packages/tonysm-importmap-laravel)[bensampo/laravel-embed

Painless responsive embeds for videos, slideshows and more.

142146.8k](/packages/bensampo-laravel-embed)[dragon-code/pretty-routes

Pretty Routes for Laravel

10058.7k4](/packages/dragon-code-pretty-routes)[laracraft-tech/laravel-useful-additions

A collection of useful Laravel additions!

58109.4k](/packages/laracraft-tech-laravel-useful-additions)[watson/nameable

Format names of users into full, familiar and abbreviated forms

299.7k](/packages/watson-nameable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
