{"id":33952692,"url":"https://github.com/tehwave/laravel-shortcodes","last_synced_at":"2025-12-12T19:45:06.505Z","repository":{"id":36673545,"uuid":"229155772","full_name":"tehwave/laravel-shortcodes","owner":"tehwave","description":"Simple, elegant WordPress-like Shortcodes the Laravel way","archived":false,"fork":false,"pushed_at":"2025-10-13T18:01:18.000Z","size":116,"stargazers_count":12,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-10-21T23:02:00.215Z","etag":null,"topics":["laravel","package","php","shortcodes","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tehwave.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-19T23:36:41.000Z","updated_at":"2025-08-31T11:20:02.000Z","dependencies_parsed_at":"2025-03-17T21:30:15.195Z","dependency_job_id":"5a220e02-6a18-4791-9a20-258e17281a81","html_url":"https://github.com/tehwave/laravel-shortcodes","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/tehwave/laravel-shortcodes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tehwave%2Flaravel-shortcodes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tehwave%2Flaravel-shortcodes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tehwave%2Flaravel-shortcodes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tehwave%2Flaravel-shortcodes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tehwave","download_url":"https://codeload.github.com/tehwave/laravel-shortcodes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tehwave%2Flaravel-shortcodes/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27689953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-12T02:00:06.775Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","package","php","shortcodes","wordpress"],"created_at":"2025-12-12T19:45:05.965Z","updated_at":"2025-12-12T19:45:06.492Z","avatar_url":"https://github.com/tehwave.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](https://banners.beyondco.de/Laravel%20Shortcodes.jpeg?theme=light\u0026packageManager=composer+require\u0026packageName=tehwave%2Flaravel-shortcodes\u0026pattern=wiggle\u0026style=style_1\u0026description=Simple%2C+elegant+WordPress-like+Shortcodes+the+Laravel+way\u0026md=1\u0026showWatermark=1\u0026fontSize=100px\u0026images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg)\n\n# Laravel Shortcodes\n\nSimple, elegant WordPress-like Shortcodes the Laravel way.\n\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)\n![Build Status](https://github.com/tehwave/laravel-shortcodes/workflows/tests/badge.svg)\n\n## Requirements\n\nThe package has been developed and tested to work with the latest supported versions of PHP and Laravel as well as the following minimum requirements:\n\n- Laravel 11\n- PHP 8.2\n\n### Version Compatibility\n\n| Laravel | PHP | Branch |\n|---|---|---|\n|  11+ | 8.2+ | [master](https://github.com/tehwave/laravel-shortcodes/tree/master) |\n|  10 and below | 8.1 and below | [1.x](https://github.com/tehwave/laravel-shortcodes/tree/1.x) |\n\n## Installation\n\nInstall the package via Composer.\n\n```bash\ncomposer require tehwave/laravel-shortcodes\n```\n\n## Usage\n\n`Laravel Shortcodes` work much like WordPress' [Shortcode API](https://codex.wordpress.org/Shortcode_API).\n\n```php\n\u003c?php\n\nuse tehwave\\Shortcodes\\Shortcode;\n\n$compiledContent = Shortcode::compile('[uppercase]Laravel Shortcodes[/uppercase]');\n\n// LARAVEL SHORTCODES\n```\n\n### Creating Shortcodes\n\nRun the following command to place a fresh `Shortcode` class in your new `app/Shortcodes` directory.\n\n```bash\nphp artisan make:shortcode ItalicizeText\n```\n\n#### Output\n\nEach `Shortcode` class contains a `handle` method, that you may use to output into the compiling content.\n\nWithin the `handle` method, you may access the `attributes` and `body` properties.\n\n\u003e [!NOTE]  \n\u003e All values in the `attributes` array are cast to `string` type when parsed unless specifically cast to a type via the `$casts` property.\n\n```php\n\u003c?php\n\nnamespace App\\Shortcodes;\n\nuse tehwave\\Shortcodes\\Shortcode;\n\nclass ItalicizeText extends Shortcode\n{\n    /**\n     * The code to run when the Shortcode is being compiled.\n     *\n     * You may return a string from here, that will then\n     * be inserted into the content being compiled.\n     *\n     * @return string|null\n     */\n    public function handle(): ?string\n    {\n        if (isset($this-\u003eattributes['escape_html']) \u0026\u0026 $this-\u003eattributes['escape_html'] === 'true')) {\n            return sprintf('\u003ci\u003e%s\u003c/i\u003e', htmlspecialchars($this-\u003ebody));\n        }\n\n        return sprintf('\u003ci\u003e%s\u003c/i\u003e', $this-\u003ebody);\n    }\n}\n```\n\n#### Naming\n\nThe shortcode's tag is derived from the class name to snake_case.\n\nYou may specify a custom tag using the `tag` property or by overwriting the `getTag` method.\n\nShortcode tags must be alpha-numeric characters and may include underscores.\n\n```php\n\u003c?php\n\nnamespace App\\Shortcodes;\n\nuse tehwave\\Shortcodes\\Shortcode;\n\nclass ItalicizeText extends Shortcode\n{\n    /**\n     * The tag to match in content.\n     *\n     * @var string\n     */\n    protected $tag = 'italics';\n}\n```\n\n### Compiling Shortcodes\n\nRun a string through the compiler to parse all shortcodes.\n\n```php\n\u003c?php\n\nuse tehwave\\Shortcodes\\Shortcode;\n\n$compiledContent = Shortcode::compile('[italics escape_html=\"true\"]\u003cb\u003eHello World\u003c/b\u003e[/italics]');\n\n// \u003ci\u003e\u0026lt;b\u0026gt;Hello World\u0026lt;/b\u0026gt;\u003c/i\u003e\n```\n\nYou may specify a list of instantiated `Shortcode` classes to limit what shortcodes are parsed.\n\n```php\n\u003c?php\n\nuse tehwave\\Shortcodes\\Shortcode;\n\n$shortcodes = collect([\n    new ItalicizeText,\n]);\n\n$compiledContent = Shortcode::compile('[uppercase]Hello World[/uppercase]', $shortcodes);\n\n// [uppercase]Hello World[/uppercase]\n```\n\n### Using Casts\n\n`Laravel Shortcodes` supports casting attributes to various data types. This can be useful when you need to ensure that the attributes passed to your shortcodes are of a specific type.\n\n#### Available Casts\n\n- `boolean`\n- `integer`\n- `float`\n- `string`\n- `array`\n- `collection`\n- `object`\n- `json`\n- `encrypted`\n- `hashed`\n- `date` (casts to `Carbon\\Carbon` instance)\n\n#### Example\n\nTo use casts, you need to create a shortcode class and specify the casts in the `$casts` property.\n\n```php\n\u003c?php\n\nnamespace App\\Shortcodes;\n\nuse tehwave\\Shortcodes\\Shortcode;\nuse Carbon\\Carbon;\n\nclass ExampleShortcode extends Shortcode\n{\n    /**\n     * The attributes that should be cast.\n     *\n     * @var array\n     */\n    protected $casts = [\n        'is_active' =\u003e 'boolean',\n        'count' =\u003e 'integer',\n        'price' =\u003e 'float',\n        'name' =\u003e 'string',\n        'tags' =\u003e 'array',\n        'options' =\u003e 'collection',\n        'metadata' =\u003e 'object',\n        'config' =\u003e 'json',\n        'published_at' =\u003e 'date',\n    ];\n\n    /**\n     * The code to run when the Shortcode is being compiled.\n     *\n     * @return string|null\n     */\n    public function handle(): ?string\n    {\n        $publishedAt = $this-\u003eattributes['published_at'] instanceof Carbon\n            ? $this-\u003eattributes['published_at']-\u003etoFormattedDateString()\n            : 'N/A';\n\n        $tags = implode(', ', $this-\u003eattributes['tags']);\n\n        $options = $this-\u003eattributes['options']-\u003eimplode(', ');\n\n        return sprintf(\n            'Active: %s, Count: %d, Price: %.2f, Name: %s, Published At: %s, Tags: %s, Options: %s',\n            $this-\u003eattributes['is_active'] === true ? 'Yes' : 'No',\n            $this-\u003eattributes['count'],\n            $this-\u003eattributes['price'],\n            $this-\u003eattributes['name'],\n            $publishedAt,\n            $tags,\n            $options\n        );\n    }\n}\n```\n\nWhen you compile content with this shortcode, the attributes will be automatically cast to the specified types.\n\n```php\n\u003c?php\n\nuse tehwave\\Shortcodes\\Shortcode;\n\n$compiledContent = Shortcode::compile('[example is_active=\"1\" count=\"10\" price=\"99.99\" name=\"Sample\" published_at=\"2023-06-29\" tags=\\'[\"tag1\",\"tag2\",\"tag3\"]\\' options=\\'[\"option1\",\"option2\"]\\']');\n\n// Active: Yes, Count: 10, Price: 99.99, Name: Sample, Published At: Jun 29, 2023, Tags: tag1, tag2, tag3, Options: option1, option2\n```\n\n### Accessing Attributes\n\nYou can retrieve the attributes as direct properties of the shortcode instance.\n\n```php\n\u003c?php\n\nnamespace App\\Shortcodes;\n\nuse tehwave\\Shortcodes\\Shortcode;\n\nclass ExampleShortcode extends Shortcode\n{\n    protected $casts = [\n        'is_active' =\u003e 'boolean',\n        'count' =\u003e 'integer',\n    ];\n\n    public function handle(): ?string\n    {\n        // Access attributes as properties\n        $isActive = $this-\u003eis_active;\n        $count = $this-\u003ecount;\n\n        return sprintf('Active: %s, Count: %d', $isActive === true ? 'Yes' : 'No', $count);\n    }\n}\n```\n\n### Example\n\nI developed `Laravel Shortcodes` for use with user provided content on [gm48.net](https://gm48.net).\n\nThe content is parsed using a Markdown converter called Parsedown, and because users can't be trusted, the content has to be escaped.\n\nUnfortunately, this escapes the attribute syntax with double quotes, but singular quotes can still be used as well as just omitting any quotes.\n\n\u003e [!NOTE]  \n\u003e Quotes are required for any attribute values that contain whitespace.\n\nLet's take a look at the following content with some basic `Row`, `Column`and `Image` shortcodes.\n\n```\n# Controls:\n\n[row]\n    [column]\n        [image align=left src=http://i.imgur.com/6CNoFYx.png alt='Move player character']\n    [/column]\n    [column]\n        [image align=center src=http://i.imgur.com/8nwaVo0.png alt=Jump]\n    [/column]\n    [column]\n        [image align=right src=http://i.imgur.com/QsbkkuZ.png alt='Go down through platforms']\n    [/column]\n[/row]\n```\n\nWhen running the content through the following code:\n\n```php\n$parsedDescription = (new Parsedown())\n    -\u003esetSafeMode(true)\n    -\u003esetUrlsLinked(false)\n    -\u003etext($this-\u003edescription);\n\n$compiledDescription = Shortcode::compile($parsedDescription);\n```\n\nWe can expect to see the following output:\n\n```html\n\u003ch1\u003eControls:\u003c/h1\u003e\n\u003cp\u003e\u003c/p\u003e\n\u003cdiv class=\"container-fluid\"\u003e\n    \u003cdiv class=\"row\"\u003e\n        \u003cdiv class=\"col\"\u003e\n            \u003cimg src=\"http://i.imgur.com/6CNoFYx.png\" class=\"mr-auto\" alt=\"Move player character\"\u003e\n        \u003c/div\u003e\n        \u003cdiv class=\"col\"\u003e\n            \u003cimg src=\"http://i.imgur.com/8nwaVo0.png\" class=\"mx-auto\" alt=\"Jump\"\u003e\n        \u003c/div\u003e\n        \u003cdiv class=\"col\"\u003e\n            \u003cimg src=\"http://i.imgur.com/QsbkkuZ.png\" class=\"ml-auto\" alt=\"Go down through platforms\"\u003e\n        \u003c/div\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n```\n\nYou should still escape any user input within your shortcodes' `handle`.\n\n## Tests\n\nRun the following command to test the package.\n\n```bash\ncomposer test\n```\n\n## Security\n\nFor any security related issues, send a mail to [peterchrjoergensen+shortcodes@gmail.com](mailto:peterchrjoergensen+shortcodes@gmail.com) instead of using the issue tracker.\n\n## Changelog\n\nSee [CHANGELOG](CHANGELOG.md) for details on what has changed.\n\n## Upgrade Guide\n\nSee [UPGRADING.md](UPGRADING.md) for details on how to upgrade.\n\n## Contributions\n\nSee [CONTRIBUTING](CONTRIBUTING.md) for details on how to contribute.\n\n## Credits\n\n- [Peter Jørgensen](https://github.com/tehwave)\n- [All Contributors](../../contributors)\n\nInspired by https://github.com/webwizo/laravel-shortcodes and https://github.com/spatie/laravel-blade-x\n\n## About\n\nI work as a Web Developer in Denmark on Laravel and WordPress websites.\n\nFollow me [@tehwave](https://twitter.com/tehwave) on Twitter!\n\n## License\n\n[MIT License](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftehwave%2Flaravel-shortcodes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftehwave%2Flaravel-shortcodes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftehwave%2Flaravel-shortcodes/lists"}