{"id":33960081,"url":"https://github.com/blablacar/twig-stamp","last_synced_at":"2025-12-12T21:40:46.452Z","repository":{"id":62494847,"uuid":"58375421","full_name":"blablacar/twig-stamp","owner":"blablacar","description":"Put a placeholder in a base template, fill it from anywhere. ","archived":false,"fork":false,"pushed_at":"2018-03-29T14:54:21.000Z","size":21,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":44,"default_branch":"master","last_synced_at":"2025-11-22T23:11:46.859Z","etag":null,"topics":["owner-v3","v3"],"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/blablacar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-09T12:39:45.000Z","updated_at":"2022-01-11T02:00:25.000Z","dependencies_parsed_at":"2022-11-02T11:18:17.315Z","dependency_job_id":null,"html_url":"https://github.com/blablacar/twig-stamp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/blablacar/twig-stamp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blablacar%2Ftwig-stamp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blablacar%2Ftwig-stamp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blablacar%2Ftwig-stamp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blablacar%2Ftwig-stamp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blablacar","download_url":"https://codeload.github.com/blablacar/twig-stamp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blablacar%2Ftwig-stamp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27692444,"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":["owner-v3","v3"],"created_at":"2025-12-12T21:40:45.693Z","updated_at":"2025-12-12T21:40:46.446Z","avatar_url":"https://github.com/blablacar.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Twig Stamp\n\nPut a placeholder in a base template, fill it from anywhere.\n\n## Idea\n\nOne of our front mates needs to dump SVG sprites on our base layout, but don't want to dump all our icons: only the SVGs that are actually used\nin the whole page, on demand. The problem here is that our pages are complex, with lots of `{% include %}`, `{% render %}` and other features\nthat doesn't let us easily track what are the required SVG icons and dump them at the top of the base layout.\n\nThis Twig extension adds the ability to put a placeholder somewhere in a base layout, and to put something inside from any page of the application\nwhatever his scope and independence.\n\n## Example\n\nTo simplify, imagine that we want to create a page having a table of contents (toc) at the top, generated based on what's inside the page.\n\nThe main layout:\n\n```jinja\n{# demo.twig #}\n{% stamp 'toc' %}\n\n{# here is the placeholder where we want the table of contents dumped #}\n{% stamp_dump 'toc' %}\n\n{{ include('section1.twig') }}\n{# ... #}\n\n{% endstamp %}\n```\n\nOne section:\n\n```jinja\n{# section1.twig #}\n{# Here, we add a stamp that will be dumped in the placeholder later on #}\n\u003ch1\u003e{{ stamp_use('toc', 'Section 1') }}\u003c/h1\u003e\n\nLorem ipsum dolor sit amet, eu vel aliquam adversarium...\n```\n\nThe table of contents:\n\n```jinja\n{# toc.twig #}\n\u003ch1\u003eTable of contents\u003c/h1\u003e\n\n\u003cul\u003e\n    {% for title in list %}\n        \u003cli\u003e{{ title }}\u003c/li\u003e\n    {% endfor %}\n\u003c/ul\u003e\n```\n\nNow we need to create the logic:\n\n```php\n\u003c?php\n\nnamespace Demo\\TableOfContents;\n\nuse Blablacar\\Twig\\Api\\StampInterface;\n\nclass TableOfContentsStamp implements StampInterface\n{\n    protected $twig;\n    protected $list = [];\n\n    public function __construct(\\Twig_Environment $twig)\n    {\n        $this-\u003etwig = $twig;\n    }\n\n    /**\n     * Method called with {{ stamp_use('toc', 'Section 1') }}\n     */\n    public function useStamp()\n    {\n        list($title) = func_get_args();\n        $this-\u003elist[] = $title;\n\n        return $title;\n    }\n\n    /**\n     * Method called when reaching {% endstamp %}, that will dump content in the {% stamp_dump %} placeholder\n     */\n    public function dumpStamp()\n    {\n        return $this-\u003etwig-\u003erender('toc.twig', [\n            'list' =\u003e $this-\u003elist\n        ]);\n    }\n\n    public function getName()\n    {\n        return 'toc';\n    }\n}\n\n```\n\nWe need to register the Stamp in the extension:\n\n```php\nuse Blablacar\\Twig\\Extension\\StampExtension;\n// ...\n\n$extension = new StampExtension();\n$twig-\u003eaddExtension($extension);\n\n$stamp = new TableOfContentsStamp($twig);\n$extension-\u003eaddStamp($stamp);\n```\n\nBy running this sample, you'll get:\n\n```html\n\n\u003ch1\u003eTable of contents\u003c/h1\u003e\n\n\u003cul\u003e\n    \u003cli\u003eSection 1\u003c/li\u003e\n\u003c/ul\u003e\n\n\u003ch1\u003eSection 1\u003c/h1\u003e\n\nLorem ipsum dolor sit amet, eu vel aliquam adversarium...\n```\n\n## Installation\n\n```sh\ncomposer require blablacar/twig-stamp\n```\n\n## License\n\nThe MIT License (MIT)\n\nPlease read the [LICENSE](LICENSE) file for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblablacar%2Ftwig-stamp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblablacar%2Ftwig-stamp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblablacar%2Ftwig-stamp/lists"}