繁体   English   中英

如何在PHP函数中获取Twig模板引擎标题标签

[英]How To Get Twig Template Engine Header Tags in PHP Function

我正在尝试扩展Pico导航插件,以从页面使用Twig模板引擎标题标签的导航树中排除项目。

我的问题是如何从以下PHP函数中的.md文件中获取特定的标头标签,并将其过滤以排除在导航树中?

该插件当前实现了使用config.php文件中的以下设置从树中省略项目(页面和文件夹)的功能:

//  Exclude pages and/or folders from navigation header
$config['navigation']['hide_page'] = array('a Title', 'another Title');
$config['navigation']['hide_folder'] = array('a folder', 'another folder');

插件文件中的当前函数使用上面的config.php,如下所示:

private function at_exclude($page) {

    $exclude = $this->settings['navigation'];
    $url = substr($page['url'], strlen($this->settings['base_url'])+1);
    $url = (substr($url, -1) == '/') ? $url : $url.'/';

    foreach ($exclude['hide_page'] as $p) {

        $p = (substr($p, -1*strlen('index')) == 'index') ? substr($p, 0, -1*strlen('index')) : $p;
        $p = (substr($p, -1) == '/') ? $p : $p.'/';

        if ($url == $p) {
            return true;
        }
    }

    foreach ($exclude['hide_folder'] as $f) {

        $f = (substr($f, -1) == '/') ? $f : $f.'/';
        $is_index = ($f == '' || $f == '/') ? true : false;

        if (substr($url, 0, strlen($f)) == $f || $is_index) {
            return true;
        }
    }
    return false;
}

我需要添加使用Twig标头标签“类型”和“状态”从树中省略项目(或页面)的功能,就像在.md文件中一样:

/*
Title: Post01 In Cat01
Description: This post01 in cat01
Date: 2013-10-28
Category:
Type: post      // Options: page, post, event, hidden
Status: draft   // Options: published, draft, review
Author: Me
Template: 
*/
...
The MarkDown content . . .

因此,如果用户希望从“草稿”标签中删除“类型”标签和/或“草稿”中标记为“发布”的项目(请参见上方标头),那么他们将在我下面的数组中添加链接的标签添加到config.php中:

//  Exclude taged items:
$config['navigation']['hide_status'] = array('draft', 'maybe some other status tag');
$config['navigation']['hide_type'] = array('post', 'etc');

我还将以下内容添加到at_exclude()函数的底部:

private function at_exclude($page) {
. . .
    foreach ($exclude['hide_staus'] as $s) {

        $s = $headers['status'];

        if ($s == 'draft' || 'review') {
            return true;
        }
    }

    foreach ($exclude['hide_type'] as $t) {

        $t = $headers['type'];

        if ($t == 'post' || 'hidden') {
            return true;
    }

    return true;
}
. . .

这显然对我不起作用(因为我的PHP知识有限)。 对于我所缺少的,做错的或如何添加此功能的任何帮助将不胜感激。

我研究了(不是很漂亮)Pico代码,这些是我的发现。

首先,Pico不会读取您添加到内容标题中的每个自定义字段。 相反,它具有要解析的内部字段数组。 幸运的是,提供了一个名为before_read_file_meta的钩子来修改数组。 在at_navigation.php中,我们将添加:

/**
 * Hook to add custom file meta to the array of fields that Pico will read
 */
public function before_read_file_meta(&$headers)
{
    $headers['status'] = 'Status';
    $headers['type'] = 'Type';
}

这将导致Pico读取标题,但尚未将字段添加到页面数据中。 我们需要另一个钩子get_page_data。 在同一文件中:

/**
 * Hook to add the custom fields to the page data
 */
public function get_page_data(&$data, $page_meta)
{
    $data['status'] = isset($page_meta['status']) ? $page_meta['status'] : '';
    $data['type'] = isset($page_meta['type']) ? $page_meta['type'] : '';
}

现在,在at_exclude函数中,我们可以添加新逻辑。 (而不是循环,我们配置了要排除的状态和类型的数组,我们将检查与当前页面状态/类型是否匹配)

private function at_exclude($page)
{
    [...]
    if(in_array($page['status'], $exclude['status']))
    {
        return true;
    }

    if(in_array($page['type'], $exclude['type']))
    {
        return true;
    };

    return false;
}

现在,让我们自定义我们的config.php(我使用插件标准对配置进行了标准化):

$config['at_navigation']['exclude']['status'] = array('draft', 'review');
$config['at_navigation']['exclude']['type'] = array('post');

全部做完!
但是,如果您刚刚起步,建议您使用更成熟和最新的平面文件cms。 除非您坚持使用PHP5.3

我决定简化该功能以从config.php中忽略它,因为最终用户确实不需要设置它。 这样,通过省略其他文件的所有检查,at_exclude()函数在后端变得更加简单和快捷:

at_exclude {

  . . .

    $pt = $page['type'];
    $ps = $page['status'];

    $home = ($pt == "home");
    $post = ($pt == "post");
    $event = ($pt == "event");
    $hide = ($pt == "hide");

    $draft = ($ps == "draft");
    $review = ($ps == "review");

    $type = $home || $post || $event || $hide;
    $status = $draft || $review;

    if ( $type || $status ) {
        return true;
    };

    return false;
}

显然,它需要进行一些整理,但是您了解了情况。 日Thnx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM