Creating Views in PHP is Best Practice

I am working on a site with two other developers. I am responsible for creating the submissions.

Data is available in the object, and I have getters for reading data, then create XHTML pages.

What is the best practice for this without using a template engine?

Many thanks.

+8
design php xhtml templates separation-of-concerns
source share
3 answers

If you do not want to use the template engine, you can use the capabilities of the basic PHP template.

Actually, you should just write HTML, and whenever you need to output the value of a variable, open the PHP part with <?php and close it with ?> . I will assume for examples that $data is your data object.

For example:

 <div id="fos"><?php echo $data->getWhatever(); ?></div> 

Note that all PHP management structures (e.g. if , foreach , while , etc.) also have syntax that can be used for templates. You can see them on your PHP manual pages.

For example:

 <div id="fos2"> <?php if ($data->getAnother() > 0) : ?> <span>X</span> <?php else : ?> <span>Y</span> <?php endif; ?> </div> 

If you know that the use of short tags will be enabled on the server, for simplicity you can also use them (not recommended in XML and XHTML). With short tags, you can simply open your PHP part with <? and close it with ?> . In addition, <?=$var?> Is a shorthand for repeating something.

First example with short tags:

 <div id="fos"><?=$data->getWhatever()?></div> 

You need to know where you use line breaks and spaces. The browser will receive the same text that you write (except for parts of PHP). What I mean:

Writing this code:

 <?php echo '<img src="x.jpg" alt="" />'; echo '<img src="y.jpg" alt="" />'; ?> 

not equivalent to this:

 <img src="x.jpg" alt="" /> <img src="y.jpg" alt="" /> 

Because in the second you have the actual \n between the img elements, which will be translated by the browser as a space character and displayed as the actual space between the images, if they are embedded.

+11
source share

To read data, use a separate file:

 <?php if ($foo == False) { $bar = 1; } else { $bar = 0; } ?> 

Then specify the resulting state in the HTML file:

 require 'logic.php'; <html> <!--...--> <input type="text" value="<?php echo $bar; ?>" > //Logic is separated from markup <!--...--> </html> 
0
source share

I do not know that I fully understand your question. therefore, if my answer is not canceled, I will like to delete

this class will create a simple view

 class View { public function render($filename, $render_without_header_and_footer = false) { // page without header and footer, for whatever reason if ($render_without_header_and_footer == true) { require VIEWS_PATH . $filename . '.php'; } else { require VIEWS_PATH . '_templates/header.php'; require VIEWS_PATH . $filename . '.php'; require VIEWS_PATH . '_templates/footer.php'; } } private function checkForActiveController($filename, $navigation_controller) { $split_filename = explode("/", $filename); $active_controller = $split_filename[0]; if ($active_controller == $navigation_controller) { return true; } // default return return false; } private function checkForActiveAction($filename, $navigation_action) { $split_filename = explode("/", $filename); $active_action = $split_filename[1]; if ($active_action == $navigation_action) { return true; } // default return of not true return false; } private function checkForActiveControllerAndAction($filename, $navigation_controller_and_action) { $split_filename = explode("/", $filename); $active_controller = $split_filename[0]; $active_action = $split_filename[1]; $split_filename = explode("/", $navigation_controller_and_action); $navigation_controller = $split_filename[0]; $navigation_action = $split_filename[1]; if ($active_controller == $navigation_controller AND $active_action == $navigation_action) { return true; } // default return of not true return false; } } 

soo now you can create your own templates and you can call it from anywhere where just

 $this->view->my_data = "data"; $this->view->render('index/index'); // 

and on your index / index.php you can call the data $ this-> my_data;

0
source share

All Articles