Sunday 15 April 2012

How To Get Twig Template Engine Header Tags in PHP Function -



How To Get Twig Template Engine Header Tags in PHP Function -

i trying extend pico navigation plugin exclude items navigation tree page's utilizes twig template engine header tags.

my question how specific header tags .md files in below php function , filter them excluded in navigation tree?

the plugin implements ability omit items (pages , folders) tree next settings in config.php file:

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

the current function in plugs' file uses above config.php follows:

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'] $p) { $p = (substr($p, -1*strlen('index')) == 'index') ? substr($p, 0, -1*strlen('index')) : $p; $p = (substr($p, -1) == '/') ? $p : $p.'/'; if ($url == $p) { homecoming true; } } foreach ($exclude['hide_folder'] $f) { $f = (substr($f, -1) == '/') ? $f : $f.'/'; $is_index = ($f == '' || $f == '/') ? true : false; if (substr($url, 0, strlen($f)) == $f || $is_index) { homecoming true; } } homecoming false; }

i need add together ability of omitting items (or pages) tree using twig header tags 'type' , 'status' in .md files:

/* title: post01 in cat01 description: post01 in cat01 date: 2013-10-28 category: type: post // options: page, post, event, hidden status: draft // options: published, draft, review author: me template: */ ... markdown content . . .

so if user wants remove items tagged "post" in 'type' tag and/or "draft" 'draft' tag (see header above), add together linked tags in array below added config.php:

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

i added next bottom of at_exclude() function:

private function at_exclude($page) { . . . foreach ($exclude['hide_staus'] $s) { $s = $headers['status']; if ($s == 'draft' || 'review') { homecoming true; } } foreach ($exclude['hide_type'] $t) { $t = $headers['type']; if ($t == 'post' || 'hidden') { homecoming true; } homecoming true; } . . .

this not working me (because php knowledge limited). help missing, doing wrong or how can add together functionality appreciated.

i dived (not beautiful) pico code , findings.

first of all, pico doesn't read every custom field add together content header. instead, has internal array of fields parse. luckily, hook called before_read_file_meta provided modify array. in at_navigation.php we'll add:

/** * hook add together custom file meta array of fields pico read */ public function before_read_file_meta(&$headers) { $headers['status'] = 'status'; $headers['type'] = 'type'; }

this result in pico reading headers, won't add together fields page info yet. need hook, get_page_data. in same file:

/** * hook add together custom fields page info */ 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 at_exclude function, can add together new logic. (instead of cycling, configure array of status , types want exclude, , we'll check if there match current page status/type)

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

now let's customize our config.php (i standardized configuration plugin standards):

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

all done! if starting out, i'd advise utilize more mature , recent flat file cms. unless stuck php5.3

php twig

No comments:

Post a Comment