开发者

Creating Views in PHP - Best Practice [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
开发者_StackOverflow

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 4 years ago.

Improve this question

I am working on a website with 2 other developers. I am only responsible to creating the views.

The data is available in an object, and I have getters to read the data then create XHTML pages.

What is the best practice to do this, without using any template engine?

Thanks a lot.


If you don't want to use a templating engine, you can make use of PHP's basic templating capabilities.

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

For example:

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

Please note that, all PHP control structures (like if, foreach, while, etc.) also have a syntax that can be used for templating. You can look these up in their 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 short tag usage will be enabled on the server, for simplicity you can use them as well (not advised in XML and XHTML). With short tags, you can simply open your PHP part with <? and close it with ?>. Also, <?=$var?> is a shorthand for echoing something.

First example with short tags:

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

You should be aware of where you use line breaks and spaces though. The browser will receive the same text you write (except the PHP parts). What I mean by this:

Writing this code:

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

is not equivalent to this one:

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

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


Use a separate file to read the data:

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

Then reference the resulting state in the HTML file:

require 'logic.php';

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


i dont know it i get realy your question. so if my answer not exaclty i will like to de deleted

this class will create 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 templates and can call it from any where just like

$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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜