简体   繁体   English

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

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

I am trying to extend a Pico navigation plugin to exclude items from the navigation tree where the page's utilizes Twig template engine header tags. 我正在尝试扩展Pico导航插件,以从页面使用Twig模板引擎标题标签的导航树中排除项目。

My question is how do I get specific header tags from the .md files in the below PHP function and filter them to be excluded in the navigation tree? 我的问题是如何从以下PHP函数中的.md文件中获取特定的标头标签,并将其过滤以排除在导航树中?

The plugin currently implements the ability to omit items (pages and folders) from the tree with the following settings in a config.php file: 该插件当前实现了使用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');

The current function in the plugs' file uses the above config.php as follows: 插件文件中的当前函数使用上面的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;
}

I need to add the ability of omitting items (or pages) from the tree using the Twig header tags 'Type' and 'Status' like so in the .md files: 我需要添加使用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 . . .

So if a user wants to remove items tagged with "post" in the 'type' tag and/or "draft" from the 'draft' tag (see header above), they would then add the linked tags in the array below that I added into the config.php: 因此,如果用户希望从“草稿”标签中删除“类型”标签和/或“草稿”中标记为“发布”的项目(请参见上方标头),那么他们将在我下面的数组中添加链接的标签添加到config.php中:

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

I also added the following to the bottom of the at_exclude() function: 我还将以下内容添加到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;
}
. . .

This is obviously not working for me (because my PHP knowledge is limited). 这显然对我不起作用(因为我的PHP知识有限)。 Any help with what I am missing, doing wrong or how I can add this functionality will be greatly appreciated. 对于我所缺少的,做错的或如何添加此功能的任何帮助将不胜感激。

I dived into the (not so beautiful) Pico code and those are my findings. 我研究了(不是很漂亮)Pico代码,这些是我的发现。

First of all, Pico doesn't read every custom field you add to the content header. 首先,Pico不会读取您添加到内容标题中的每个自定义字段。 Instead, it has an internal array of fields to parse. 相反,它具有要解析的内部字段数组。 Luckily, an hook called before_read_file_meta is provided to modify the array. 幸运的是,提供了一个名为before_read_file_meta的钩子来修改数组。 In at_navigation.php we'll add: 在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';
}

This will result in Pico reading the headers, but it won't add the fields to the page data yet. 这将导致Pico读取标题,但尚未将字段添加到页面数据中。 We need another hook, get_page_data. 我们需要另一个钩子get_page_data。 In the same file: 在同一文件中:

/**
 * 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'] : '';
}

Now, in the at_exclude function, we can add the new logic. 现在,在at_exclude函数中,我们可以添加新逻辑。 (Instead of cycling, We configure an array of status and types we want to exclude, and we'll check if there is a match with the current page status/type) (而不是循环,我们配置了要排除的状态和类型的数组,我们将检查与当前页面状态/类型是否匹配)

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;
}

Now let's customize our config.php (I standardized the configuration with the plugin standards): 现在,让我们自定义我们的config.php(我使用插件标准对配置进行了标准化):

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

All done! 全部做完!
But if you are just starting out, I'd advise you to use a more mature and recent flat file cms. 但是,如果您刚刚起步,建议您使用更成熟和最新的平面文件cms。 Unless you are stuck with PHP5.3 除非您坚持使用PHP5.3

I decided to simplify the function to omit it from the config.php since it really isn't needed to be set by the end-user. 我决定简化该功能以从config.php中忽略它,因为最终用户确实不需要设置它。 By doing so, the at_exclude() function is much simpler and quicker on the back-end by omitting all the checks via other files: 这样,通过省略其他文件的所有检查,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;
}

Obviously it needs some tidying up but you get the picture. 显然,它需要进行一些整理,但是您了解了情况。 Thnx 日Thnx

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

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