【问题标题】:Extending Timber延长木材
【发布时间】:2017-10-09 22:16:14
【问题描述】:

我是第一次使用 Timber,我发现很难将我的纯 PHP 思维转换为这种模板模型。

目前我感到困惑的是使用输入字段的日期,而不是发布日期。

所以我有一个来自 ACF 的转发器字段。它具有三个子字段:release_datedocument_filedocument_title。我的发布日期格式是YYYY-MM-DD

您可能已经猜到了,此转发器字段以 PDF 列表的形式输出。

到目前为止一切都很好,但我想要一些高级功能 - 即默认情况下按 release_date 显示 PDF。我还希望能够按年份过滤,即 - 如果您点击“2015”,它只会显示该年的文档。

我确切地知道如何在直接的 WordPress 中做到这一点,但我很困惑在 Timber 上制作它。我一直在尝试使用自定义过滤器来做到这一点,但我觉得我真正想要的是自定义类?

另外,当我安装 Timber 时,它没有附带启动主题,所以我搜索并从 GitHub 下载了一个。我感觉这是一个旧版本,因为文件结构和语法似乎与文档不匹配。

从这里下载:https://github.com/timber/starter-theme

但是例如启动主题functions.php中的这段代码:

function add_to_twig( $twig ) {
    /* this is where you can add your own functions to twig */
    $twig->addExtension( new Twig_Extension_StringLoader() );
    $twig->addFilter('split_date', new Twig_SimpleFilter('split_date', array($this, 'split_date')));
    return $twig;
}

与“添加到 Twig”下的 https://github.com/timber/timber/wiki/Extending-Timber 中的语法不太匹配。

【问题讨论】:

  • 添加filter 的语法有点不同,但仍然正确。使用array($this, 'split_date)` 等于告诉twig:添加一个新过滤器split_date 并为其分配基于$this 类的函数。文档中的示例是使用全局函数而不是类方法
  • 好的,很酷,所以它不会被弃用。但是,这是一种合理的方式来实现我想要实现的目标吗?我希望能够拆分日期(因此split_date)并分别使用日、年和月。
  • 如果您的发布日期是一个有效的日期字符串,我会选择:<a href="route/to/file?year={{ release_date | date('Y') }}">{{ release_date | date('Y-m-d') }}</a>
  • 由于第一次看到footer.twig,我自己刚刚发现了这一点。这正是我正在寻找的,干杯。这是非常好的和简单的,虽然我仍然对文档有点困惑。我在任何地方的示例中都没有看到这种语法。
  • 你可以在herehere的文档中看到为twig添加过滤器的不同方法

标签: wordpress twig advanced-custom-fields timber


【解决方案1】:

这里有一些辅助函数可以让我轻松扩展木材。

function add_context_var( $key, $var ) {
    add_filter( 'timber_context', function ( $context ) use ( $key, $var ) {
        $context[ $key ] = $var;

        return $context;
    } );
}

function add_context_func( $key, $callback ) {
    add_filter( 'timber/twig', function ( $twig ) use ( $key, $callback ) {
        $twig->addFunction( new \Twig_SimpleFunction( $key, $callback ) );

        return $twig;
    } );
}

function add_to_context( $key, $val ) {
    if ( is_callable( $val ) ) {
        add_context_func( $key, $val );
    } else {
        add_context_var( $key, $val );
    }
}

function add_to_context_filter( $key, $callback ) {
    add_filter( 'get_twig', function ( $twig ) use ( $key, $callback ) {
        $twig->addExtension( new Twig_Extension_StringLoader() );
        $twig->addFilter( new Twig_SimpleFilter( $key, $callback ) );

        return $twig;
    } );
}

//php file
add_to_context("blue", "this key is blue")
add_to_context("red", function($extra = ""){
  return "this key is red and it has $extra";
})
add_to_context_filter( "relative_link", function ( $content ) {
    return str_replace( "http://", "//", $content );;
} );

//twig file
color: {{ blue }}
color: {{ red("a function parameter") }}
github: {{ "http://github.com/"|relative_link }}

//output html
color: this key is blue
color: this key is red and it has function parameter
github: //github.com/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 2022-10-02
    相关资源
    最近更新 更多