php - Merging multiple HTML files into one and return output -
i trying find cleanest way merge multiple html files 1 html file. way can change parts of html or show them on pages. file list followed:
- page.tpl (header, footer, head info)
- sidebar.tpl (contains sidebar , sidebar blocks)
- nav.tpl(contains navigation links in nested ul)
the page.tpl file looks this:
<!doctype html> <html> <head> <title>page title</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="author" content="brandon" /> <meta name="robots" content="noindex,nofollow" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <?php print $stylesheets; ?> <?php print $scripts; ?> </head> <body> <section id="wrapper"> <header>header title</header> <nav><?print $nav; ?></nav> <section><?php print $content; ?></section> <aside> <?php print $sidebar; ?><aside> <footer>© 2011 brandon license: gplv2</footer> </section> </body> </html>
the main function have include is:
function theme($tpl, $vars = array()) { extract($vars); ob_start(); require($tpl); $template = ob_get_contents(); ob_end_clean(); return $template; }
$tpl set page.tpl file.
i tried $vars['nav'] = file_get_contents('nav.tpl');
above theme function give data work with. if remove $tpl variable , require()
function, see ul nav list when add page.tpl file in error:
warning: extract() expects parameter 1 array, null given
this works(shows ul nav list):
$vars['nav'] = file_get_contents('nav.tpl'); function theme($vars = array()) { extract($vars); ob_start(); $template = ob_get_contents(); ob_end_clean(); return $template; }
this doesn't:
$vars['nav'] = file_get_contents('nav.html'); theme('page.html', $vars) //page.html set correct directory. function theme($tpl, $vars = array()) { extract($vars); ob_start(); require($tpl); $template = ob_get_contents(); ob_end_clean(); return $template; }
any on getting work correctly appreciated.
update: current index.php file:
<?php define('root_dir', getcwd()); require_once(root_dir . '/core/includes/boot.inc'); boot_start(boot_full); // based off of drupal's drupal_bootstrap(). set's ini_set's, database //and starts sessions. works fine , haven't coded //theme/template code it. thing boot_start() theme //load .inc file has theme() function. .inc gets included // otherwise have gotten "call unknown function" error. $vars['nav'] = file_get_contents(root_dir . '/core/templates/nav.tpl'); theme('./core/templates/page.tpl', $vars);
i don't quite understand why getting error extract()
. when add $vars['nav']
without including 'include($tpl)', extract works fine. isn't until try include page.tpl file.
the page.tpl file should loaded on every page request outputs anything. think need theme($vars) instead of theme($tpl, $vars = array())
is there way can include page.tpl without passing theme(), while passing $vars $vars['nav'] overrides <?php print $nav; ?>
tag in page.tpl? thanks.
solved: man, can't believe took me long fix this. since theme() returned , not echo'ed data, had assign $theme = theme('page.tpl', $vars);
echo $theme;
besides few php notices, works.
i make file individual parts. , include them.
<?php include('relative/link.php'); ?>
if want edit content in section use variables.
header.php
echo $foo;
index.php
$foo='bar'; include('header.php');
when include file grabs contents , injects in current file , process it.
Comments
Post a Comment