简体   繁体   中英

Reusable user interface in PHP?

For most of my projects I make an administration interface, which has the same design for every project. The design of the header, the footer, the topbar, the leftmenu, the css, etc. are always the same. It is a pity to create the views every time; so I was thinking: maybe there would be a nice way to put the admin interface in my MVC library, as it is reused by every project?

But for the moment, in every single view I got code like the following:

<?php $this->_include('/includes/doctype.php'); ?>
<head>
    <?php $this->_include('/includes/head.php'); ?>
    <title>Some title</title>
</head>
<body>
<?php $this->_include('/includes/topbar.php'); ?>
<div id="page">
<?php $this->_include('/includes/header.php'); ?>
<?php $this->_include('/includes/leftmenu.php'); ?>
<div id="content" role="main">

    <h1>Some title</h1>

    <p>Blah blah blah.</p>

</div><!-- /#content -->
<?php $this->_include('/includes/footer.php'); ?>
</div><!-- /#page -->
</body>
</html>

Would it be a good idea to extract the custom content from the structure of the interface, and put that structure in my library somehow to make it reusable? After that how will it be possible to customize the title and the actual menus?

I do this all the time. I have a custom header and footer file that are called at the start and end of every page.

<?PHP
Require("includes/header.php");

...


Require("includes/footer.php");
?>

The header provides a database handle, a datetime string and handles logon, priveleges, logging of pageviews etc.

The footer provides a standard HTML page but includes some systematised variables. It also generates the menu dynamically from the driving database then closes the database connection.

This way when I write code, I don't get mixed up in the HTML and any bugs are easy to find.

I like variables akin to:

$display_scripts - adds extra data in the head section.
$display_onload_scripts - adds onload scripts to body section.
$display_style_sheets - option to include link to additional stylesheets
$display_above_menu - will appear above the menubar.  NOT recommended.
$display_below_menu - will appear immediately below the menubar.
$display_one_column - page contents when only one column is to be used
$display_left_column - page contents when two columns used.  Left pane.
$display_right_column - page contents when two columns used.  Right pane.
$display_footer - appears in footer division.

My main code then just has to generate the appropriate variable. Fundamentally, what you need to do is examine the source of a good age you have produced then replace the stuff you want to change with variables.

Here is a schematised version of the file I use (pseudocode) to give you an idea of how I do it.

// Code here generates the menu from database
// Code here genereates popup alert messages from other users

 //permanent links to external style sheets go here.
 //You can also select skins here.

<?PHP
echo $display_style_sheets;
echo "<title>".$display_page_title."</title>";
?>

<script type="text/javascript" src="JAVASCRIPT GOES HERE.js"></script>

</head>

<body  <?PHP echo $display_onload_scripts;?> >



<div id="page_area" > 
        <div id="banner">
    </div>

    <?php
    echo $display_above_menu;
    if(!$hide_menu){echo $display_menu;}  //Insert the menu variable here.
    echo $display_below_menu;
    ?>

<div id="content_area">
<div id="inner_content">

<?PHP
if($display_number_of_columns==1)
    {
    echo "<div id='onecolumn'>".$display_one_column."</div>"; //I only use this one
    }
if($display_number_of_columns==2)
    {
    echo "<div id='leftcolumn'>".$display_left_column."</div>";  //these are left for legacy support from before I got better at CSS.
    echo "<div id='rightcolumn'>".$display_right_column."</div>";
    }
echo "<div id='footer'>".$display_footer."</div>";  //just in case - I hardly use it.
echo $display_pop_box;   //for user alert messages to other users
?>

</div>
</div>
</div>

<div id="logbox"> Automatic Logout statement</div> //this is called by JS to activate timeouts.

</body>
</html>

<?PHP
$mysqlidb->close();
?>

Sorry it's such a lot of code. The layout allows easy adaptation and makes it simple to find the offending variable if things are not going as expected. There are more elegant solutions but this works well for me and is very fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM