Download the PHP package moxie-lean/loader without Composer
On this page you can find all versions of the php package moxie-lean/loader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download moxie-lean/loader
More information about moxie-lean/loader
Files in moxie-lean/loader
Package loader
Short Description Loader for files in wordpress making things easier instead of using a require
License MIT
Informations about the package loader
Loader
Allows to load files from directories with a more sugared syntax, and allowing the use of params passed to the file.
Benefits
By using the Loader package, instead of the regular get_template_part
or
any other WordPress
default function to load partials or files between templates
you have the follow benefits:
- More clear sintax of what files and from where are loaded.
- Allow to send variables between files loaded.
- Multiple set of arguments to the partials.
- Keep things DRY.
Requirements
Make sure you have at least the following in order to use this library.
- PHP 7.4 or PHP 8.0
- composer:
Installation
Usage
You need to make sure the autoload.php
file from composer is included so you can use the functions
from other packages.
The function accepts at least two arguments:
-
$file
, in the example abovesingle
. This is the filename wanted to load. The extension is optional, in this case we want to load the filesingle.php
from thepartials
directory, you can create an alias for directories (see alias for more information) to use a different name for that directory. ...$args
, an associative array with the values that we wanted to pass to the loaded file, the array can have any number of elements as long as it's a valid associative array. Those values are available on the loaded file via the$args
variable and can be used as follows:
You can send as many set of arguments as you want, at the end all
sets are merged into a single one with wp_parse_args
to create a single set.
Multiple set of arguments.
Tips
Set default values
You can easily set default values to always make sure you have the expected arguments on the partial or to have values that migth be optional like:
Don't render if you don't have an expected value.
In some cases you are expecting a required value and if that value is not present you don't want to render that specifc component, in those situations is better to avoid the render of the component, in order to do that you can return from the template at any point to avoid the following lines to be executed, for example:
Filters
There are a coupple of filters that you can use in order to extend the default
functionalitty of the loader. You can place all the filters on functions.php
of
your theme or any file of your plugin.
Register directories where to look for files.
By default the loader is going to look in the root of the theme but if you have a structure of files such as:
To load files from views
directory you can use:
Or if you want to avoid typing partials/
every time you can include a new directory
into the search path, with the loader_directories
filter, such as:
Whith this change you now can write something like:
`
Register an alias
Alias are used if you want to access a directory in a different name such as if you want
to use Load::blocks
instead of Load::partials
you can rename the directory but to
avoid that you can easily just create an alias to call a directory in a different way,
with the loader_alias
filter, as you can see in the following example:
You only need to specify the key
into the $alias
variable that you want to
create an alias and assign to $alias[ key ]
the value with the alias that you
want to create.
Which give us a sintax like this:
Road Map
- Work the same as
get_template_part
so it works in childs and parent theme. - Set default root as the current theme in order to be a little easy to set up.