{"id":18263940,"url":"https://github.com/studiometa/twig-toolkit","last_synced_at":"2025-04-04T20:31:26.844Z","repository":{"id":42433810,"uuid":"310005622","full_name":"studiometa/twig-toolkit","owner":"studiometa","description":"🔧 A set of useful extension and components for Twig","archived":false,"fork":false,"pushed_at":"2025-01-27T08:55:24.000Z","size":1408,"stargazers_count":9,"open_issues_count":3,"forks_count":0,"subscribers_count":11,"default_branch":"develop","last_synced_at":"2025-03-18T06:30:00.153Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/studiometa.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-04T13:07:26.000Z","updated_at":"2025-01-27T08:10:07.000Z","dependencies_parsed_at":"2024-04-18T15:30:31.233Z","dependency_job_id":"900f2565-71b2-4d99-9c75-e10f4fb03234","html_url":"https://github.com/studiometa/twig-toolkit","commit_stats":{"total_commits":58,"total_committers":2,"mean_commits":29.0,"dds":0.08620689655172409,"last_synced_commit":"bfecf171f01ccbd426c6e8be3b16e9d088cefc95"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studiometa%2Ftwig-toolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studiometa%2Ftwig-toolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studiometa%2Ftwig-toolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studiometa%2Ftwig-toolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/studiometa","download_url":"https://codeload.github.com/studiometa/twig-toolkit/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247246405,"owners_count":20907792,"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","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":[],"created_at":"2024-11-05T11:13:12.350Z","updated_at":"2025-04-04T20:31:26.839Z","avatar_url":"https://github.com/studiometa.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# studiometa/twig-toolkit\n\n[![Packagist Version](https://img.shields.io/github/v/release/studiometa/twig-toolkit?style=flat\u0026colorB=3e63dd\u0026colorA=414853\u0026label=packagist)](https://packagist.org/packages/studiometa/twig-toolkit)\n[![Downloads](https://img.shields.io/packagist/dm/studiometa/twig-toolkit?style=flat\u0026colorB=3e63dd\u0026colorA=414853)](https://packagist.org/packages/studiometa/twig-toolkit)\n[![Dependency Status](https://img.shields.io/librariesio/github/studiometa/twig-toolkit?style=flat\u0026colorB=3e63dd\u0026colorA=414853)](https://packagist.org/packages/studiometa/twig-toolkit)\n![Codecov](https://img.shields.io/codecov/c/github/studiometa/twig-toolkit?style=flat\u0026colorB=3e63dd\u0026colorA=414853)\n[![License MIT](https://img.shields.io/packagist/l/studiometa/twig-toolkit?style=flat\u0026colorB=3e63dd\u0026colorA=414853)](https://github.com/studiometa/twig-toolkit/blob/master/LICENSE)\n\n\u003e A set of useful extension and components for Twig.\n\n## Installation\n\n```bash\ncomposer require studiometa/twig-toolkit\n```\n\n## Usage\n\nAdd the `Studiometa\\TwigToolkit\\Extension` to your Twig instance:\n\n```php\n$loader = new \\Twig\\Loader\\FilesystemLoader();\n$twig = new \\Twig\\Environment($loader);\n$twig-\u003eaddExtension(new \\Studiometa\\TwigToolkit\\Extension());\n```\n\n## Reference\n\n### Functions\n\n#### `{{ html_classes(\u003cclasses\u003e) }}`\n\nA function to manage classes more easily.\n\n**Params**\n\n- `classes` (`String | Array | Object`)\n\n**Examples**\n\n```twig\n{# The following examples will render the same HTML #}\n\u003cdiv class=\"{{ html_classes('foo bar') }}\"\u003e\u003c/div\u003e\n\u003cdiv class=\"{{ html_classes(['foo', 'bar']) }}\"\u003e\u003c/div\u003e\n\u003cdiv class=\"{{ html_classes({ foo: true, bar: true, baz: false }) }}\"\u003e\u003c/div\u003e\n\u003cdiv class=\"{{ html_classes(['foo', { bar: true, baz: false }]) }}\"\u003e\u003c/div\u003e\n\n{# HTML #}\n\u003cdiv class=\"foo bar\"\u003e\u003c/div\u003e\n```\n\n#### `{{ html_styles(\u003cstyles\u003e) }}`\n\nA function to manage style attributes more easily.\n\n**Params**\n\n- `styles` (`Object`)\n\n**Examples**\n\n```twig\n\u003cdiv style=\"{{ html_styles({ display: 'none', margin_top: '10px' }) }}\"\u003e\u003c/div\u003e\n\u003cdiv style=\"display: none; margin-top: 10px\"\u003e\u003c/div\u003e\n\n\u003cdiv style=\"{{ html_styles({ display: false, opacity: 0 }) }}\"\u003e\u003c/div\u003e\n\u003cdiv style=\"opacity: 0;\"\u003e\u003c/div\u003e\n```\n\n#### `{{ html_attributes(\u003cattrs\u003e) }}`\n\nA function to render HTML attributes more easily with the following features:\n\n- The `class` attribute will automatically be processed by the `class` method described above\n- Array and objects will be converted to JSON\n- Attributes keys will be converted from any format to kebab-case\n- Values will be escaped to prevent XSS attacks\n\n**Params**\n\n- `attrs` (`Object`): The attributes to render\n\n**Examples**\n\n```twig\n\u003cdiv {{ html_attributes({ id: 'one', data_options: { label: 'close' }, required: true }) }}\u003e\u003c/div\u003e\n\n{# HTML #}\n\u003cdiv id=\"one\" data-options=\"{\\\"label\\\":\\\"close\\\"}\" required\u003e\u003c/div\u003e\n```\n\n#### `{{ merge_html_attributes(attr, default_attr, required_attr) }}`\n\nMerge HTML attributes smartly, useful to define default and required attributes at the component level and allow users to add custom ones.\n\nThis filter can also be used as a function.\n\n**Params**\n\n- `attr` (`Object`): The user provided attributes\n- `default` (`Object`): The default attributes\n- `required` (`Object`): The required attributes\n\n**Examples**\n\nYou can define default and required attributes in a component's template:\n\n```twig\n{#\n/**\n * @file\n * component\n *\n * @param array $attr\n *   Custom attributes to apply to the root element.\n */\n#}\n\n{% set default_attributes = { class: 'bar' } %}\n{% set required_attributes = { data_component: 'Component' } %}\n\n{# Merge all attributes #}\n{% set attributes = merge_html_attributes(attr, default_attributes, required_attributes)}\n\n\u003cdiv {{ html_attributes(attributes) }}\u003e\u003c/div\u003e\n{# or #}\n{% html_element 'div' with attributes %}\n```\n\nAnd then include your component with custom attributes:\n\n```twig\n{% include 'component.twig' with {\n  attr: {\n    class: 'mb-10',\n    aria_hidden: 'true'\n  }\n} %}\n```\n\nYou can take advantage of [named arguments](http://twig.symfony.com/doc/3.x/templates.html#named-arguments) to avoid passing a value for the default attributes argument:\n\n```twig\n{% set required_attributes = { id: 'block' } %}\n{% set merged_attributes = merge_html_attributes(attr, required=required_attribute) %}\n```\n\n#### `{{ twig_toolkit_url(string) }}`\n\nWrapper for the `Spatie\\Url\\Url` class to easily manipulate URLs. See the [`spatie/url` documentation](https://github.com/spatie/url) for details on its API.\n\n**Params**\n\n- `url` (`string`): The URL to parse for manipulation\n\n**Examples**\n\n```twig\n{# Change host #}\n{% set url = twig_toolkit_url(url).withHost('cdn.fqdn.com') %}\n\n{# Change/add query parameters #}\n{% set url = twig_toolkit_url(url).withQueryParameter('key', 'value') %}\n```\n\n### Tags\n\n#### `{% html_element '\u003ctag\u003e' with attrs %}`\n\nRender an HTML element with the given attributes. Useful to avoid setting dynamic HTML element tags with the `\u003c{{ tag }}\u003e...\u003c/{{ tag }}\u003e` pattern.\n\n**Params**\n\n- `tag` (`String`): The name of the tag\n- `attrs` (`Object`): An object describing the element's attribues\n\n**Examples**\n\n```twig\n{# Twig #}\n{% html_element 'h1' with { class: 'block' } %}\n  Hello world\n{% end_html_element %}\n\n{# HTML #}\n\u003ch1 class=\"block\"\u003e\n  Hello world\n\u003c/h1\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstudiometa%2Ftwig-toolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstudiometa%2Ftwig-toolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstudiometa%2Ftwig-toolkit/lists"}