PHP - Php Templating
Okay so I just finished designing a template and I want to begin coding but I want to separate the logic from the template as per suggestions I've received. Now I have the separate files that are ready to be included, but how can I include it so that it updates globally. What I mean is this for example. In my header.php, I have included "style/navbar.php". Now from the root directory that's fine. The problem comes when let's say I'm working inside viewpost.php that is actually inside /style and that file includes header.php which includes style/navbar.php. So now viewpost is looking for style/style.php. How do I make php know where the include files are globally and update that directory for each query? I hope I explained this properly. Similar TutorialsHi there, ok I'm stuck... I need to show different headers for certain pages on my website, My template file below calls in all template parts with the 'layout' file being the file which includes a simple html header and menu. How would I go about this? Would I need to create and call on a seperate/second layout file eg. layout2 or can I determine which header shows on certain pages by adding some code to the layout file itself. Any help greatly greatly appreciated. Code: [Select] <?php class Templating extends Model{ function __constructor(){ } function show($center = 'homepage', $right = 'login_box', $left ='left/search_panel', $template = 'layout'){ $data = array( 'right_bar' => $right, 'left' => 'left/search_panel', 'center' => $center, 'left_logged'=>'left/user_panel' ); if ($this->session->userdata('logged_in') == true){ if ($this->session->userdata('admin')){ #$data['right_bar'] = 'right_panel/admin_panel'; $data['left_logged'] = 'right_panel/admin_panel'; }else{ #$data['right_bar'] = 'right_panel/user_panel'; $data['left_logged'] = 'left/user_panel'; //$data['left'] = 'left_panel/user_panel'; } }else{ #$data['right_bar'] = 'right_panel/login_box'; } $this->load->vars($data); $this->load->view($template); } function show_non_live(){ $data = array( 'left' => 'left/search_panel', 'center' => 'center/list_companies', 'left_logged'=>'left/user_panel' ); if ($this->session->userdata('logged_in') == true){ if ($this->session->userdata('admin')){ //$data['right_bar'] = 'right_panel/admin_panel'; $data['left_logged'] = 'right_panel/admin_panel'; }else{ //$data['right_bar'] = 'right_panel/user_panel'; $data['left_logged'] = 'left/user_panel'; } }else{ #$data['right_bar'] = 'right_panel/login_box'; } $template = 'layout'; $this->load->vars($data); $this->load->view($template); } } ?> I currently use HTML strings within my PHP code to display output. And while it might not be best practice, I find it non-restrictive and I can easily add loops, manipulate variables, etc. within my display. However in the interest of having a cleaner code I'm thinking of separating the HTML, without having to use a Template engine like Smarty. I don't care much about replacement patterns to be honest, I don't mind some PHP code withing the HTML, however my biggest issue is loops and having to modify a variable within each loop. Say I have the following example code (Similar to what I'm using right now): Code: [Select] function student_output() { //Retrieve students from array $students_arr = students_info(); $selected_student = $_GET['selected_student']; $output = '<div id="students_container">'; $output .= '<div class="items_list">'; $i = 1; //Loop through students foreach($students_arr as $key=>$value) { if ($selected_student == $key) { $output .= '<div id="student_name_'.$i.'" class="selected">'.$value['student_name'].'</div>'; $output .= '<div id="student_img_'.$i.'" class="selected"><img src="'.$value['student_image'].'" /></div>'; } else { $output .= '<div id="student_name_'.$i.'">'.$value['student_name'].'</div>'; } $i++; } return $output; } What's the best way to represent this in HTML cleanly without having too much PHP code within? I've been coding my script to PEAR standards, however was wondering what do you recommend for the naming conventions for my templating, I'm currently doing them as how constants would be (uppercase seperated with _ for another word) like: $xtpl->assign('LATEST_FLASH_TITLE', $latest_flash_title); What do you suggest? |