Created
May 27, 2012 17:11
-
-
Save alexkingorg/2815114 to your computer and use it in GitHub Desktop.
cfct_template_file() and Globals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Include a specific file based on directory and filename | |
* | |
* @param string $dir Directory the file will be in | |
* @param string $file Filename | |
* @param array $data pass in data to be extracted for use by the template | |
* | |
**/ | |
function cfct_template_file($dir, $file, $data = array()) { | |
// ** WHICH OF THESE LOOKS BEST? ** | |
// option #1 - bring in globals that appear most useful: | |
global $id, $post, $wp_query, $wpdb, $blog_id, $pagenow, $current_user, $p, $post_parent, $pagename, $page_id, $posts; | |
// option #2 - extract globals to make them all available (within current scope) | |
extract($GLOBALS); | |
// option #3 - bring in all globals | |
foreach ($GLOBALS as $k => $v) { | |
global $$k; | |
} | |
// ** END OPTIONS ** | |
$path = ''; | |
if (!empty($file)) { | |
$file = basename($file, '.php'); | |
/* Check for file in the child theme first | |
var name is deliberately funny. Avoids inadvertantly | |
overwriting path variable with extract() below. */ | |
$_cfct_filepath = STYLESHEETPATH.'/'.$dir.'/'.$file.'.php'; | |
if (!file_exists($_cfct_filepath)) { | |
$_cfct_filepath = CFCT_PATH.$dir.'/'.$file.'.php'; | |
} | |
} | |
if (file_exists($_cfct_filepath)) { | |
/* Extract $data as late as possible, so we don't accidentally overwrite | |
local function vars */ | |
extract($data); | |
include($_cfct_filepath); | |
} | |
else { | |
cfct_die('Error loading '.$file.' '.__LINE__); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment