[英]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.