PHPFAN10 Posted April 22, 2011 Share Posted April 22, 2011 Hi, Hope this is the correct board to post this in. Currently when building websites (projects of my own) i tend to do develop around a site template as shown below, i was taught this by a friend and don't really no of any better way. Take this example: <?php // Include config.php require_once("".$_SERVER['DOCUMENT_ROOT']."/lib/config.php"); // top.inc.php require_once($top_inc); ?> <!-- Meta start --> <title>PAGE TITLE HERE</title> <meta name="description" content="PAGE DESCRIPTION HERE" /> <meta name="keywords" content="PAGE KEYWORDS HERE" /> <!-- Meta end --> <?php // main.inc.php require_once($sidebar_inc); ?> <!-- CONTENT HERE --> my php and html code for particular page here <!-- CONTENT FINISH --> <?php // footer.inc.php require_once($footer_inc); ?> Problem with this type of site template for me is i hate it really really hate it. Why? For example if i wanted to created a sign up page i would simply start by adding my form between the <!-- content here --> and <!-- content finish --> and above the html i would place my php code to process the sign up webpage. To show you what i mean take a look: <?php // Include config.php require_once("".$_SERVER['DOCUMENT_ROOT']."/lib/config.php"); // top.inc.php require_once($top_inc); ?> <!-- Meta start --> <title>PAGE TITLE HERE</title> <meta name="description" content="PAGE DESCRIPTION HERE" /> <meta name="keywords" content="PAGE KEYWORDS HERE" /> <!-- Meta end --> <?php // main.inc.php require_once($sidebar_inc); ?> <!-- CONTENT HERE --> <?php // check if form been submitted if(isset($_POST['submit'])){ // validate, sanitize date etc $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; // blah blah blah and the rest etc } ?> <h1>Signup to <?php echo $websitename; ?></h1> <form method="post" action="<?php echo basename(__file__); ?>" id="signup-form"> <label for="name">Name</label> <input type="text" name="name" id="name" value="<?php if(isset($name)) { echo $name; } ?>" /> <label for="email">Email</label> <input type="text" name="email" id="email" value="<?php if(isset($email)) { echo $email; } ?>" /> <label for="name">Password</label> <input type="text" name="password" id="password" value="<?php if(isset($password)) { echo $password; } ?>" /> </form> <!-- CONTENT FINISH --> <?php // footer.inc.php require_once($footer_inc); ?> Problem is i am despising the way i currently do things. Reason is i am mixing php with html and vice versa and although it looks pretty clean in example above when you have several hundred lines of code mixed php, html it is a nightmare to read through, ok for a small site but i like to build upon existing projects so something like the above is just a no no for me. In my examples above my config.php file contains $variables like for site name, website address, email addresses etc and has some includes() in the config.php file that includes() some other files like db.inc.php (function), paths.inc.php file, functions.inc.php file and so on and so on. I would like to ask all developers out there how do you do it? i really need to change the way i do things, i don't know OOP so that's out of the window for now as i am still learning the non OOP stuff and don't want to over do it. I am just finding this type of layout/template (what ever you would like to call it) very frustrating. I know there are things like smarty out there but have already looked at smarty, codeignitor etc and find them to be overwhelming and feels like a a programming language of there own that i would need to learn. Thanks for any advice, tips etc. PHPFAN Quote Link to comment https://forums.phpfreaks.com/topic/234454-dynamic-web-page-templatelayout-php/ Share on other sites More sharing options...
lastkarrde Posted April 22, 2011 Share Posted April 22, 2011 I only ever use Twig nowadays. Its a full featured templating language with a very basic syntax that only takes 5 minutes to learn. It has template inheritance which makes things like what your trying to do a breeze. For example you would just define a basic template with many blocks like : base.html <html> <head> <title>{% block title %}{% endblock %}</title> </head> <!-- define menu/sitewide features here --> {% block content %}{% endblock %} <!-- define footer/sidewide features here --> </html> All you would need to do to render any page on your website is: aboutus.html {% extends 'base.html %} {% block title %}About Us{% endblock %} {% block content %} About us content blah blah {% endblock %} Twig automatically (and somewhat magically) takes your about page and replaces the appropriate blocks into the base.html template. It then compiles all of this into straight PHP and caches the PHP code. So when you enable caching (a Twig configuration setting), it will render the straight PHP code making it as fast (if not faster) than a PHP templating solution. Useful links: Twig syntax & features Twig PHP API Quote Link to comment https://forums.phpfreaks.com/topic/234454-dynamic-web-page-templatelayout-php/#findComment-1205055 Share on other sites More sharing options...
PHPFAN10 Posted April 22, 2011 Author Share Posted April 22, 2011 Hello, Thanks for the reply, i will take a look at Twig, sounds great and the sort of thing i am after Thanks. anyone else have any other examples or alternatives to Twig? , no disprespect to poster above obviously, just want to see what's out there. Thanks PHPFAN Quote Link to comment https://forums.phpfreaks.com/topic/234454-dynamic-web-page-templatelayout-php/#findComment-1205095 Share on other sites More sharing options...
lastkarrde Posted April 23, 2011 Share Posted April 23, 2011 PHPTAL, Smarty and PHPTI are all worth a look at. Although I do recommend Twig for performance, stability (its fully tested) and ease of use . Quote Link to comment https://forums.phpfreaks.com/topic/234454-dynamic-web-page-templatelayout-php/#findComment-1205103 Share on other sites More sharing options...
Philip Posted April 23, 2011 Share Posted April 23, 2011 You can also do it like this, separating the templates completely from the application logic: Application logic: <?php // Include config.php require_once("".$_SERVER['DOCUMENT_ROOT']."/lib/config.php"); $_PAGE = array( 'title' => 'Page Title Here', 'description' => 'Meta description', 'keywords' => 'Meta keywords', 'website_name' => 'My website', 'content_template' => 'register.inc', 'current_loc' => basename(__file__), ); // Process your stuff... // check if form been submitted if(isset($_POST['submit'])){ // validate, sanitize date etc $_CONTENT['name'] = $_POST['name']; $_CONTENT['email'] = $_POST['email']; $_CONTENT['password'] = $_POST['password']; // blah blah blah and the rest etc } require('main.inc'); ?> A main (minus the content) template: <?php // main.inc template file // top.inc.php require_once($top_inc); ?> <!-- Meta start --> <title>PAGE TITLE HERE</title> <meta name="description" content="PAGE DESCRIPTION HERE" /> <meta name="keywords" content="PAGE KEYWORDS HERE" /> <!-- Meta end --> <?php // main.inc.php require_once($sidebar_inc); require_once($_PAGE['content_template']); // footer.inc.php require_once($footer_inc); // end main.inc template file ?> Registration template page <?php // register.inc template file ?> <h1>Signup to <?php echo $_PAGE['website_name']; ?></h1> <form method="post" action="<?php echo $_PAGE['current_loc']; ?>" id="signup-form"> <label for="name">Name</label> <input type="text" name="name" id="name" value="<?php if(isset($_CONTENT['name'])) { echo $_CONTENT['name']; } ?>" /> <label for="email">Email</label> <input type="text" name="email" id="email" value="<?php if(isset($_CONTENT['email'])) { echo $_CONTENT['email']; } ?>" /> <label for="name">Password</label> <input type="text" name="password" id="password" value="<?php if(isset($_CONTENT['password'])) { echo $_CONTENT['password']; } ?>" /> </form> <?php // end register.inc template file ?> Quote Link to comment https://forums.phpfreaks.com/topic/234454-dynamic-web-page-templatelayout-php/#findComment-1205224 Share on other sites More sharing options...
PHPFAN10 Posted April 23, 2011 Author Share Posted April 23, 2011 Hi King Phillip, That looks quite alrite i must say. Did not think of that. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/234454-dynamic-web-page-templatelayout-php/#findComment-1205286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.