mvrk/icenberg

用于封装高级自定义字段灵活内容标记的库

v0.2.2 2024-08-15 07:44 UTC

README

这是什么?

这需要 ACF Pro,主要是用于 Maverick 的内部使用,尽管它很容易在 ACF 的灵活内容字段中实现。

Icenberg 旨在整理 ACF 灵活内容块模板,这些模板通常涉及大量的重复和逻辑与展示交织在一起,真正符合 WordPress 风格。

使用 Icenberg 的方法,我们可以以更干净的面向对象的方式渲染任何 acf 字段,包括 BEM 类和设置,同时仍然允许我们在必要时使用旧的方式。

它主要用于灵活内容字段,因为它假设存在“行”,但理论上也可以在非灵活组和重复器中使用。

注意:buttonssettings方法依赖于 Maverick 特定的设置,我们将在未来使这些方法更通用。

入门

通过 composer 安装

composer require mvrk/icenberg

确保在 functions.php 中设置了自动加载 - 例如

$composer_path = $_SERVER['DOCUMENT_ROOT'] . '/../vendor/';

if (file_exists($composer_path)) {

    require_once $composer_path . 'autoload.php';
}

确保您已安装 ACF Pro。此库还支持ACF Gravity forms插件。

以下所有内容都在 ACF 的 the_row() 内部进行 - 即

if (have_rows('content_blocks', $id)) :

    while (have_rows('content_blocks', $id)) : the_row();

        get_template_part('inc/blocks/block_template');

    endwhile;

endif;

使用 ACF 的the_row_layout()初始化

use MVRK\Icenberg\Icenberg;

$icenberg = new Icenberg(get_row_layout());

初始化后,您就可以开始构建您的块了。

Icenberg 方法

get_element($field_name, $tag = 'div')

返回一个格式化的字符串,作为一个 div 包裹着所有需要的 div,并应用任何特殊考虑。接受字段名作为参数,并可选地接受一个用于最顶层元素的标签。如果没有设置标签,它将使用 'div'。

$field_name = $icenberg->get_element('field_name');

echo $field_name;

the_element($field_name, $tag = 'div')

与上面相同,但它立即输出。

$icenberg->the_element('field_name');

Icenberg 足够聪明,知道字段类型,因此您不需要区分,只需传递字段名即可。

enclose()

Enclose 是一个实用工具,用于在容器 div 中包裹多个 icenberg 字段,而不必使用任何 ?>。您只需传递一个类名(不要使用前缀,因为 icenberg 将应用这些前缀)。所以很干净!

例如,在一个 'Cta' 块中,其中 cta_heading 是一个文本字段,cta_content 是一个 wysiwyg 字段

$icenberg->enclose('text', [
    $icenberg->get_element('cta_heading')
    $icenberg->get_element('cta_content'),
]);

将生成

<div class="block block--cta">
    <div class="section__inner">
        <div class="wrapper block--cta__wrapper">
            <div class="block--cta__text">
                <div class="block--cta__cta-heading">
                    I'm a heading, look at me!
                </div>
                <div class="block--cta__cta-content">
                    <p>
                        sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

您还可以将任何其他您喜欢的封装内容作为数组的一部分传递,只要它可以存储为变量(例如,插入一个get_template_part()在这里不会工作,因为它实际上会打印内容)。

$random_text = "<span>I am some random text, isn't it wonderful?</span>";

$icenberg->enclose('text', [
    $icenberg->get_element('cta_heading'),
    $random_text
]);

当然,生活从不简单,您可能需要更复杂的布局,但 icenberg 不介意。您想将其插入 html 也可以。

<div class="testimonial__corner-illustration_green_reversed">
    <?php if($motif_variation_5_purple === 'orange') :
    $icenberg->the_element('motif_blurple');
    endif; ?>
</div>

settings($field_name, $additional_classes)

传递一个设置的字段组和一个可选的手动设置类数组,它将它们作为 CSS 修饰类附加。如果您在组中包含一个名为 'unique_id' 的文本字段,icenberg 将将其作为 id 附加。

enclose()中使用设置的示例

$classes = ['banana', 'orange'];

$block_settings = get_sub_field($block_settings);

$settings = $icenberg->settings($block_settings, $classes);

$icenberg->enclose ($settings, [
    $icenberg->get_element('cheese_board'),
    $icenberg->get_element('flame_thrower')
])

或使用常规的 php/html

<div <?php echo $settings; ?>>
    ...whatever you want
</div>

这将打印出类似的内容

"class='orange orange_padding_top_300 orange_skin_purple' id='cheese_board'"

根据您组中的设置。

条件语句

field($field_name)

您还可以使用icenberg来评估字段,使用以下方法结合 field() 方法。 field() 接收字段名称作为参数,并返回用于方法链的icenberg实例。

$icenberg->field($field_name)

is($value)

如果字段的值等于 is() 的参数,则返回true。在使用这些方法之前,您不需要检查字段是否存在,因为它们会为您检查并如果它们不存在则返回 false

 if ($icenberg->field('font_colour')->is('red')) :
    $icenberg->the_element('font_colour');
else :
    echo 'oh no';
endif;

lessThan($value)greaterThan($value)

无需解释,两者都接受一个整数作为参数。警告:如果您在非数值字段上使用它,将返回false。

if ($icenberg->field('range_test')->lessThan(51)) :
    $class = 'text_' . $icenberg->field('range_test')->field;
    $icenberg->enclose($class, [
        $icenberg->get_element('cta_content'),
        $icenberg->get_element('cta_image'),
    ]);
endif;

Maverick 特定

get_buttons($field_name)the_buttons($field_name)

返回一个具有广泛样式选择的格式化按钮组 - 非常符合Maverick风格。期望我们常用的按钮组格式。

支持的字段

当前支持的字段

  • 相册
  • 分组
  • 图片
  • 链接
  • 数字
  • OEmbed
  • 范围
  • 重复
  • 选择
  • 文本
  • 文本区域
  • 所见即所得

第三方字段

  • 表单
  • 色卡

特殊字段

  • 按钮
  • 设置