moxie-lean/loader 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

moxie-lean/loader

Composer 安装命令:

composer require moxie-lean/loader

包简介

Loader for files in wordpress making things easier instead of using a require

README 文档

README

Build Status

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.

Installation

composer require moxie-lean/loader

Usage

You need to make sure the autoload.php file from composer is included so you can use the functions from other packages.

// functions.php
include_once( get_stylesheet_directory()  . '/vendor/autoload.php' );
<?php
// File: index.php
use Lean\Load;

$args = [
  'title' => get_the_title(),
  'url' => get_the_permalink(),
  'target' => '_blank'
];
Load::partials( 'single', $args );

The function accepts at least two arguments:

  • $file, in the example above single. This is the filename wanted to load. The extension is optional, in this case we want to load the file single.php from the partials 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.

<?php 
// File: partials/single.php 
// All loaded files have an $args variable that is used to store all the params
// passed from where the Load function was used.
?>
<a href="<?php echo esc_url( $args['url'] ); ?>" target="<?php echo esc_attr( $args['target'] ); ?>">
  <?php echo esc_html( $args['title'] ); ?>
</a>

Multiple set of arguments.

<?php 
use Lean\Load;

$set_1 = [
  'a' => 1,
  'b' => 5,
  'c' => 3
];
$set_2 = [
  'a' => 10,
  'd' => 3
];
$set_3 = [
  'd' => 10,
  'c' => 2,
  'r' => 3,
];
// You can have as many sets as you want.
Load::partials( 'single', $set_1, $set_2, $set_3 ) ?>

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:

<?php
// File: partials/single.php

// The following lines creates an array with default values. If those values 
// are not specified when the file is loaded this values are going to be used instead.
$defaults = [
  'url' => '',
  'title' => '',
  'target' => '_self',
]
// Update $args with the initial $args mixed with the $default values.
$args = wp_parse_args( $args, $defaults );
?>
<a href="<?php echo esc_url( $args['url'] ); ?>" target="<?php echo esc_attr( $args['target'] ); ?>">
  <?php echo esc_html( $args['title'] ); ?>
</a>

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:

<?php
// File: partials/single.php

// The following lines creates an array with default values. If those values 
// are not specified when the file is loaded this values are going to be used instead.
$defaults = [
  'url' => '',
  'title' => '',
  'target' => '_self',
]
// Update $args with the initial $args mixed with the $default values.
$args = wp_parse_args( $args, $defaults );

// Don't render if the title or url are empty.
if ( empty( $args['title'] || empty( $args['url'] ) ) ) {
  return; 
}
?>
<a href="<?php echo esc_url( $args['url'] ); ?>" target="<?php echo esc_attr( $args['target'] ); ?>">
  <?php echo esc_html( $args['title'] ); ?>
</a>

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:

index.php
functions.php
|- views
|-|- partials
|-|-|- single.php
|-|-|- button.php

To load files from views directory you can use:

<?php 
use Lean\Load;

$arguments = [];
Load::views( 'partials/single', $arguments ); 
Load::views( 'partials/button', $arguments ); 
?>

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:

add_filter( 'loader_directories', function( $directories ){
  $directories[] = get_template_directory() . '/views';
  return $directories;
});

Whith this change you now can write something like:

<?php 
use Lean\Load;

$arguments = [];
Load::partials( 'single', $arguments ); 
Load::partials( 'button', $arguments ); 
?>

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:

add_filter('loader_alias', function( $alias ){
  $alias['partials'] = 'blocks';
  return $alias;
});

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:

<?php
use Lean\Load;
$arguments = [];
Load::blocks( 'single', $arguments );
?>

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.

moxie-lean/loader 适用场景与选型建议

moxie-lean/loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37.84k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2016 年 04 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 moxie-lean/loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 moxie-lean/loader 我们能提供哪些服务?
定制开发 / 二次开发

基于 moxie-lean/loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 37.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 6
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-26