PHP file reader and Smarty
I wrote this code when I was coding for a Company doing web development. Basically what this is three parts:
So this is how it works.
You got this this killer template and css Working awesome and looking great. but you need to add your content now, but you want to keep your programmers out of the template code, So you now segmented your code using smarty and have proper variables for your coders and they wont fuck up the design. So heres a really simple example of the solution. You got 3 major parts, your php code, your file that you want to read in and your smarty template.
php about.php page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>My File</title> <link rel="stylesheet" href="css/main.css" media="screen" /> <?php print('<!--[if IE]><style type="text/css"> .thrColLiqHdr #sidebar2, .thrColLiqHdr #sidebar1 { padding-top: 30px; } .thrColLiqHdr #mainContent { zoom: 1; padding-top: 15px; } </style><![endif]-->');?> </head> <?php require 'libs/Smarty.class.php'; $smarty = new Smarty; $smarty->compile_check = true; $smarty->debugging = false; //Conent For the About page //Open About.txt $sFile = "staticPages/about.txt"; $fh = fopen($sFile, 'r'); $tehPage = fread($fh,filesize($sFile)); fclose($fh); $smarty->assign('pageTitle','About'); $smarty->assign('PageContent',$tehPage); //Render to Screen $smarty->display('about.tpl'); ?> [/code] about.txt file <pre lang="html"> <p>This is the base file set up for the about static pages.</p> <br /> <br /> <br /> <p>This is Text File that is read using php to make handling the static files easier. Users just need to edit this about.txt file and use proper html tags to render things correctly.</p>
and the smarty template called about.tpl:
{include file="header.tpl" title='About Us} {include file="content.tpl"} {include file="footer.tpl"}
now the header.tpl the footer.tpl doesn’t matter here right now. So the content.tpl file:
<div id="mainContent"> <h2>{$pageTitle}</h2> {$PageContent} <!-- end #mainContent --></div>
So what is happening here is when the php script reads in the about.txt file it stores it and hands it to the smarty using assign to then some variable. in this case its assigned to $PageConent. Using smarty for templates is awesome because you can totally keep the code part from the design code. That way Developers can be working on the dirty stuff and designers of the UI can do there CSS and HTML Stuff with out screwing each other over.
Leave a Reply