{"id":18904198,"url":"https://github.com/kingkool68/sprig","last_synced_at":"2025-05-12T14:59:21.682Z","repository":{"id":34047035,"uuid":"136067989","full_name":"kingkool68/sprig","owner":"kingkool68","description":"Bare bones Twig templating support for WordPress","archived":false,"fork":false,"pushed_at":"2024-12-03T16:57:05.000Z","size":48,"stargazers_count":48,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-06T11:47:46.338Z","etag":null,"topics":["template-engine","theme-development","twig","twig-template-engine","twig-templates","wordpress"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kingkool68.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":"2018-06-04T18:41:12.000Z","updated_at":"2025-01-13T23:33:39.000Z","dependencies_parsed_at":"2022-08-08T00:00:07.921Z","dependency_job_id":null,"html_url":"https://github.com/kingkool68/sprig","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingkool68%2Fsprig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingkool68%2Fsprig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingkool68%2Fsprig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kingkool68%2Fsprig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kingkool68","download_url":"https://codeload.github.com/kingkool68/sprig/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238059009,"owners_count":19409607,"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":["template-engine","theme-development","twig","twig-template-engine","twig-templates","wordpress"],"created_at":"2024-11-08T09:07:42.981Z","updated_at":"2025-02-10T05:10:34.189Z","avatar_url":"https://github.com/kingkool68.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sprig\n_Bare bones Twig templating support for WordPress_\n\nSprig brings the Twig templating engine to WordPress. Install the plugin and get started separating your HTML from PHP.\n\nOther Twig plugins like [Timber](https://www.upstatement.com/timber/) try to bring lots of WordPress functionality into Twig. Sprig believes in separating PHP from HTML as much as possible. PHP is for gathering and massaging data and Sprig/Twig templates are for rendering HTML using the data passed to it. Keep things simple and only pass data to a template that the template needs to render.\n\n## Installation\n\n1. Clone this repo or download a zip\n2. Add this to the plugin directory of your WordPress site in `/wp-content/plugins/`\n3. Run `composer install` to download the [Twig](https://twig.symfony.com/) template engine dependency\n\n## How to Use\n\nCreate a directroy in your theme called `views` or `twig` to hold your Twig template files. Render your templates using the following methods passing an array of data to be used by the template.\n\n - `Sprig::render()` will render a Twig template using an array of data and return it as a string\n - `Sprig::out()` will `echo` a rendered template\n - `Sprig::do_action()` will capture the output of a WordPress action and return a string\n\n## Example\n\n**example.php**\n\n```php\n\u003c?php\n$context = array(\n  'title' =\u003e 'Sprig is awesome!',\n  'url'   =\u003e 'https://github.com/kingkool68/sprig/',\n);\n\n// Render the template and return a string\n$thing = Sprig::render( 'example.twig', $context );\n\n// Echo out the rendered template\nSprig::out( 'example.twig', $context );\n```\n\n**example.twig**\n\n```twig\n\u003cp\u003e\n\t\u003ca href=\"{{ url|esc_url }}\"\u003e{{ title }}\u003c/a\u003e\n\u003c/p\u003e\n```\n\n**Output**\n\n```html\n\u003cp\u003e\n\t\u003ca href=\"https://github.com/kingkool68/sprig/\"\u003eSprig is awesome!\u003c/a\u003e\n\u003c/p\u003e\n```\n\n## Demo Theme\n\nI've put together a [simple demo theme](https://github.com/kingkool68/sprig-demo-theme) to show how Sprig can be used within a WordPress theme.\n\n## Twig Filters\n\nTwig filters allow a string to be manipulated in a Twig template. Most of the default filters in Sprig are WordPress' [escaping functions](https://developer.wordpress.org/themes/theme-security/data-sanitization-escaping/#escaping-securing-output) for securing output as late as possible.\n\n - [`esc_attr`](https://developer.wordpress.org/reference/functions/esc_attr/)\n - [`esc_html`](https://developer.wordpress.org/reference/functions/esc_html/)\n - [`esc_url`](https://developer.wordpress.org/reference/functions/esc_url/)\n - [`esc_js`](https://developer.wordpress.org/reference/functions/esc_js/)\n - [`esc_textarea`](https://developer.wordpress.org/reference/functions/esc_textarea/)\n - [`tag_escape`](https://developer.wordpress.org/reference/functions/tag_escape/)\n - [`sanitize_email`](https://developer.wordpress.org/reference/functions/sanitize_email/)\n - [`sanitize_html_class`](https://developer.wordpress.org/reference/functions/sanitize_html_class/)\n - [`antispambot`](https://developer.wordpress.org/reference/functions/antispambot/)\n - [`wptexturize`](https://developer.wordpress.org/reference/functions/wptexturize/)\n - [`absint`](https://developer.wordpress.org/reference/functions/absint/)\n\nAdditional Twig filters can be added via the WordPress filter `sprig/twig/filters` and adding a callable function with a key to the array. This filter should be called before the `init` action so Twig filters can be set up properly in time.\n\nExample for adding the [`sanitize_title()`](https://developer.wordpress.org/reference/functions/sanitize_title/) function as a Twig filter:\n\n```\nfunction filter_sprig_twig_filters( $filters = array() ) {\n\t$filters['sanitize_title'] = 'sanitize_title';\n\treturn $filters;\n}\nadd_filter( 'sprig/twig/filters', 'filter_sprig_twig_filters' );\n```\n\n## Twig Functions\n\nTwig functions let you call PHP functions from within Twig templates. Sprig enables a handful of WordPress functions used in the base Twig template and WordPress' [`checked`](https://developer.wordpress.org/reference/functions/checked/)/[`selected`](https://developer.wordpress.org/reference/functions/selected/)/[`disabled`](https://developer.wordpress.org/reference/functions/disabled/) form helpers.\n\n - [`checked()`](https://developer.wordpress.org/reference/functions/checked/)\n - [`selected()`](https://developer.wordpress.org/reference/functions/selected/)\n - [`disabled()`](https://developer.wordpress.org/reference/functions/disabled/)\n - [`wp_head()`](https://developer.wordpress.org/reference/functions/wp_head/)\n - [`wp_footer()`](https://developer.wordpress.org/reference/functions/wp_footer/)\n - [`body_class()`](https://developer.wordpress.org/reference/functions/body_class/)\n - [`get_header()`](https://developer.wordpress.org/reference/functions/get_header/)\n - [`get_footer()`](https://developer.wordpress.org/reference/functions/get_footer/)\n - [`wp_title()`](https://developer.wordpress.org/reference/functions/wp_title/)\n\nAdditional Twig functions can be added via the WordPress filter `sprig/twig/functions` and adding a callable function with a key to the array. This filter should be called before the `init` action so Twig functions can be set up properly in time.\n\nExample for adding the [`wp_nonce_field()`](https://developer.wordpress.org/reference/functions/wp_nonce_field/) function as a Twig function:\n\n```\nfunction filter_sprig_twig_functions( $functions = array() ) {\n\t$functions['wp_nonce_field'] = 'wp_nonce_field';\n\treturn $functions;\n}\nadd_filter( 'sprig/twig/functions', 'filter_sprig_twig_functions' );\n```\n\n## Extending Sprig\n\nSprig offers various WordPress filters to customize its behavior.\n\n - `sprig/twig` for modifying Twig itself\n - `sprig/twig/filters` for adding or removing available Twig filters\n - `sprig/twig/functions` for adding or removing available Twig functions\n - `sprig/roots` for modifying which directories Twig should look for twig templates in\n - `sprig/theme_dirs` for modifying the name of directories to look for Twig templates in (Example: if you want to change the directory from `views` or `twig` to `templates`)\n - `sprig/twig_loader` for modifying the Twig loader environment (see \u003chttps://twig.symfony.com/doc/2.x/api.html\u003e)\n\n ## PHP Compatibility\n\nPHP compatibility is dependent on Twig's PHP prerequisites. Use the appropriate branch of Sprig to meet your minimum PHP requirements.\n\n - [Twig 3.x](https://twig.symfony.com/doc/3.x/intro.html) needs at least PHP 7.2.0 to run, use the Sprig `master` branch.\n - [Twig 2.x](https://twig.symfony.com/doc/2.x/intro.html) needs at least PHP 7.0.0 to run, use the Sprig `2.x` branch.\n - [Twig 1.x](https://twig.symfony.com/doc/1.x/intro.html) needs at least PHP 5.5.0 to run , use the Sprig `1.x` branch.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingkool68%2Fsprig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkingkool68%2Fsprig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkingkool68%2Fsprig/lists"}