{"id":19060424,"url":"https://github.com/wearenolte/loader","last_synced_at":"2025-04-24T06:52:48.960Z","repository":{"id":13419931,"uuid":"50357729","full_name":"wearenolte/loader","owner":"wearenolte","description":"Loader for files in wordpress making things easier instead of using a require","archived":false,"fork":false,"pushed_at":"2021-12-16T14:41:42.000Z","size":33,"stargazers_count":6,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-24T06:52:43.118Z","etag":null,"topics":["loader","partials","theme","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wearenolte.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-01-25T14:56:44.000Z","updated_at":"2022-10-06T12:10:44.000Z","dependencies_parsed_at":"2022-07-15T14:47:55.608Z","dependency_job_id":null,"html_url":"https://github.com/wearenolte/loader","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wearenolte%2Floader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wearenolte%2Floader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wearenolte%2Floader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wearenolte%2Floader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wearenolte","download_url":"https://codeload.github.com/wearenolte/loader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250580713,"owners_count":21453531,"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":["loader","partials","theme","wordpress"],"created_at":"2024-11-09T00:15:22.553Z","updated_at":"2025-04-24T06:52:48.943Z","avatar_url":"https://github.com/wearenolte.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Loader  \n\n[![Build Status](https://travis-ci.org/moxie-lean/loader.svg?branch=master)](https://travis-ci.org/moxie-lean/loader) \n\n\u003e Allows to load files from directories with a more sugared syntax, and\n\u003e allowing the use of params passed to the file.\n\n# Benefits \n\nBy using the Loader package, instead of the regular `get_template_part` or \nany other `WordPress` default function to load partials or files between templates\nyou have the follow benefits: \n\n- More clear sintax of what files and from where are loaded.\n- Allow to send variables between files loaded.\n- Multiple set of arguments to the partials. \n- Keep things DRY.\n\n# Requirements\n\nMake sure you have at least the following in order to use this library.\n\n- PHP 7.4 or PHP 8.0\n- [composer](https://getcomposer.org/):\n\n# Installation\n\n```bash\ncomposer require moxie-lean/loader\n```\n\n# Usage\n\nYou need to make sure the `autoload.php` file from composer is included so you can use the functions\nfrom other packages.\n\n```php\n// functions.php\ninclude_once( get_stylesheet_directory()  . '/vendor/autoload.php' );\n```\n\n```php\n\u003c?php\n// File: index.php\nuse Lean\\Load;\n\n$args = [\n  'title' =\u003e get_the_title(),\n  'url' =\u003e get_the_permalink(),\n  'target' =\u003e '_blank'\n];\nLoad::partials( 'single', $args );\n```\n\nThe function accepts at least two arguments:\n\n- `$file`, in the example above `single`. This is the filename wanted to load.\nThe extension is optional, in this case we want to load the file `single.php` from\nthe `partials` directory, you can create an alias for directories \n([see alias for more information](#register-an-alias)) to use a different name for that directory.\n\n- `...$args`, an associative array with the values that we wanted to pass to the \nloaded file, the array can have any number of elements as long as it's a \nvalid associative array. Those values are available on the loaded file via \nthe `$args` variable and can be used as follows:\n\nYou can send as many set of arguments as you want, at the end all\nsets are merged into a single one with `wp_parse_args` to create a single set. \n\n```php\n\u003c?php \n// File: partials/single.php \n// All loaded files have an $args variable that is used to store all the params\n// passed from where the Load function was used.\n?\u003e\n\u003ca href=\"\u003c?php echo esc_url( $args['url'] ); ?\u003e\" target=\"\u003c?php echo esc_attr( $args['target'] ); ?\u003e\"\u003e\n  \u003c?php echo esc_html( $args['title'] ); ?\u003e\n\u003c/a\u003e\n```\n\n### Multiple set of arguments.\n\n```php\n\u003c?php \nuse Lean\\Load;\n\n$set_1 = [\n  'a' =\u003e 1,\n  'b' =\u003e 5,\n  'c' =\u003e 3\n];\n$set_2 = [\n  'a' =\u003e 10,\n  'd' =\u003e 3\n];\n$set_3 = [\n  'd' =\u003e 10,\n  'c' =\u003e 2,\n  'r' =\u003e 3,\n];\n// You can have as many sets as you want.\nLoad::partials( 'single', $set_1, $set_2, $set_3 ) ?\u003e\n```\n\n## Tips\n\n### Set default values\n\nYou can easily set default values to always make sure you have the expected arguments\non the partial or to have values that migth be optional like: \n\n```php\n\u003c?php\n// File: partials/single.php\n\n// The following lines creates an array with default values. If those values \n// are not specified when the file is loaded this values are going to be used instead.\n$defaults = [\n  'url' =\u003e '',\n  'title' =\u003e '',\n  'target' =\u003e '_self',\n]\n// Update $args with the initial $args mixed with the $default values.\n$args = wp_parse_args( $args, $defaults );\n?\u003e\n\u003ca href=\"\u003c?php echo esc_url( $args['url'] ); ?\u003e\" target=\"\u003c?php echo esc_attr( $args['target'] ); ?\u003e\"\u003e\n  \u003c?php echo esc_html( $args['title'] ); ?\u003e\n\u003c/a\u003e\n```\n\n### Don't render if you don't have an expected value.\n\nIn some cases you are expecting a required value and if that value is not present\nyou don't want to render that specifc component, in those situations is better to \navoid the render of the component, in order to do that you can return from the template\nat any point to avoid the following lines to be executed, for example: \n\n```php\n\u003c?php\n// File: partials/single.php\n\n// The following lines creates an array with default values. If those values \n// are not specified when the file is loaded this values are going to be used instead.\n$defaults = [\n  'url' =\u003e '',\n  'title' =\u003e '',\n  'target' =\u003e '_self',\n]\n// Update $args with the initial $args mixed with the $default values.\n$args = wp_parse_args( $args, $defaults );\n\n// Don't render if the title or url are empty.\nif ( empty( $args['title'] || empty( $args['url'] ) ) ) {\n  return; \n}\n?\u003e\n\u003ca href=\"\u003c?php echo esc_url( $args['url'] ); ?\u003e\" target=\"\u003c?php echo esc_attr( $args['target'] ); ?\u003e\"\u003e\n  \u003c?php echo esc_html( $args['title'] ); ?\u003e\n\u003c/a\u003e\n```\n\n# Filters\n\nThere are a coupple of filters that you can use in order to extend the default \nfunctionalitty of the loader. You can place all the filters on `functions.php` of \nyour theme or any file of your plugin.\n\n## Register directories where to look for files.\n\nBy default the loader is going to look in the root of the theme but if you have a \nstructure of files such as: \n\n```\nindex.php\nfunctions.php\n|- views\n|-|- partials\n|-|-|- single.php\n|-|-|- button.php\n```\n\nTo load files from `views` directory you can use:\n\n```php\n\u003c?php \nuse Lean\\Load;\n\n$arguments = [];\nLoad::views( 'partials/single', $arguments ); \nLoad::views( 'partials/button', $arguments ); \n?\u003e\n```\n\nOr if you want to avoid typing `partials/` every time you can include a new directory\ninto the search path, with the `loader_directories` filter, such as:\n\n```php\nadd_filter( 'loader_directories', function( $directories ){\n  $directories[] = get_template_directory() . '/views';\n  return $directories;\n});\n```\n\nWhith this change you now can write something like:\n\n```php\n\u003c?php \nuse Lean\\Load;\n\n$arguments = [];\nLoad::partials( 'single', $arguments ); \nLoad::partials( 'button', $arguments ); \n?\u003e\n````\n\n\n## Register an alias\n\nAlias are used if you want to access a directory in a different name such as if you want\nto use `Load::blocks` instead of `Load::partials` you can rename the directory but to\navoid that you can easily just create an alias to call a directory in a different way,\nwith the `loader_alias` filter, as you can see in the following example:\n\n```php\nadd_filter('loader_alias', function( $alias ){\n  $alias['partials'] = 'blocks';\n  return $alias;\n});\n```\n\nYou only need to specify the `key` into the `$alias` variable that you want to\ncreate an alias and assign to `$alias[ key ]` the value with the alias that you \nwant to create.\n\nWhich give us a sintax like this: \n\n```php\n\u003c?php\nuse Lean\\Load;\n$arguments = [];\nLoad::blocks( 'single', $arguments );\n?\u003e\n```\n\n# Road Map\n\n- Work the same as `get_template_part` so it works in childs and parent theme.\n- Set default root as the current theme in order to be a little easy to set up.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwearenolte%2Floader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwearenolte%2Floader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwearenolte%2Floader/lists"}