While working on a project I found myself needing to dynamically include a snippet based on the URL. Doing this would allow me to keep a majority of my PHP code outside of my template, thus allowing me to easily redesign the layout when I needed to. Below was the result:
<?php
$query_string = substr($_SERVER[‘QUERY_STRING’], 2);
$match = preg_match(‘!fans/(.+?)/shows!’, $query_string, $value);
$doc_root = $_SERVER[‘DOCUMENT_ROOT’].‘/’;
$theme_path = ‘path/to/theme/’;
$filename = ’snippet-’.str_replace(‘/’, ‘-’, $value[0]).‘.tpl.php’;
$file = $doc_root.$base_path.$theme_path.$filename;
if (file_exists($file)) {
include($filename);
} else {
print $content;
}
$query_string = substr($_SERVER[‘QUERY_STRING’], 2);
$match = preg_match(‘!fans/(.+?)/shows!’, $query_string, $value);
$doc_root = $_SERVER[‘DOCUMENT_ROOT’].‘/’;
$theme_path = ‘path/to/theme/’;
$filename = ’snippet-’.str_replace(‘/’, ‘-’, $value[0]).‘.tpl.php’;
$file = $doc_root.$base_path.$theme_path.$filename;
if (file_exists($file)) {
include($filename);
} else {
print $content;
}
0 Responses to “File include based on URL”
Leave a Reply