{"id":26418661,"url":"https://github.com/devknown/twig-import-function","last_synced_at":"2025-03-18T01:54:19.446Z","repository":{"id":62551383,"uuid":"509624303","full_name":"devknown/twig-import-function","owner":"devknown","description":"Call (almost) any PHP function from your Twig (v3) templates.","archived":false,"fork":false,"pushed_at":"2022-07-02T02:01:25.000Z","size":15,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-15T17:48:43.343Z","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/devknown.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":"2022-07-02T00:56:22.000Z","updated_at":"2024-09-18T14:36:07.000Z","dependencies_parsed_at":"2022-11-03T02:15:50.089Z","dependency_job_id":null,"html_url":"https://github.com/devknown/twig-import-function","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devknown%2Ftwig-import-function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devknown%2Ftwig-import-function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devknown%2Ftwig-import-function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devknown%2Ftwig-import-function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devknown","download_url":"https://codeload.github.com/devknown/twig-import-function/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244141586,"owners_count":20404835,"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":"2025-03-18T01:54:18.816Z","updated_at":"2025-03-18T01:54:19.431Z","avatar_url":"https://github.com/devknown.png","language":"PHP","readme":"# Twig v3 Import Function \n\n[![Build Status](https://github.com/devknown/twig-import-function/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/devknown/twig-import-function/actions?query=branch%3Amain)\n[![License](https://poser.pugx.org/devknown/twig-import-function/license.svg)](https://packagist.org/packages/devknown/twig-import-function)\n\nCall (almost) any PHP function from your Twig (v3) templates.\n\n## Requirements\n\nPHP 7.3 and later.\nTwig 3.x\n\n## Composer\n\nYou can install the bindings via [Composer](http://getcomposer.org/). Run the following command:\n\n```bash\ncomposer require devknown/twig-import-function\n```\n\nTo use the bindings, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):\n\n```php\nrequire_once('vendor/autoload.php');\n```\n\n## Manual Installation\n\nIf you do not wish to use Composer, you can download and include the `ImportFunctionExtension.php` file.\n\n```php\nrequire_once('./src/Devknown/Twig/Extension/ImportFunctionExtension.php');\n```\n\n## Getting Started\n\nSimple usage looks like:\n\n```php\n// 1. Setup twig 3.x\n$loader = new \\Twig\\Loader\\FilesystemLoader('/path/to/template');\n$twig = new \\Twig\\Environment($loader);\n\n// 2. Load extension\n$extension = new \\Devknown\\Twig\\Extension\\ImportFunctionExtension();\n\n// 3. Import a function\n$extension-\u003eimport('uniqid');\n\n// -\u003e Or multiple functions\nfunction my_custom_function($name) {\n    echo \"Your name is: \" . $name;\n}\n$extension-\u003eimport(['uniqid', 'file_exists', 'my_custom_function']);\n// Note: in this example, the 'my_custom_function..' must be accessible globally \n\n// 4. Add extension\n$twig-\u003eaddExtension($extension);\n\n// 5. Render as normal\necho $twig-\u003erender('home.html', ['the' =\u003e 'variables', 'go' =\u003e 'here']);\n```\n\nthe template file `home.html` (/path/to/template/home.html) would look like:\n```html\n\u003cdiv\u003e\n    Hi, I am unique: \u003cb\u003e{{ uniqid() }}\u003c/b\u003e.\n    \u003cbr/\u003e \n    {{ my_custom_function('Devknown') }}\n\u003c/div\u003e\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevknown%2Ftwig-import-function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevknown%2Ftwig-import-function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevknown%2Ftwig-import-function/lists"}