1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How can I seperate code from design?

Discussion in 'PHP' started by EnPassant, May 1, 2008.

  1. #1
    Hi guys,

    I've learned php but I still do not know a reliable solution to seperate code from design. I already know i can use includes and CSS to clean the page but.. something like this.. (please be patient with me :))

    I want this to be the index.php for example;

    <html>
    <head>

    <title>{TITLE}</title>

    </head>
    <body>

    <div>Logo</div>

    {DynamicText}

    </body>
    </html>

    I see this in a lot of programs, how can I make my template files real simple not the ugly way..

    basically, how to put the php code in one place, and the template files in another, but still be able to call any php function during writing my templates.. like the one above.. look at the ugly way to do it.

    <html>
    <head>

    <title><?php include ('lang.php');?><?echo $title;?>

    I want to avoid this and put a simple {TITLE} where title dynamically goes to another php file and grab the title variable and put it in a fast efficient way.

    I am developing a small application and this would save me a lot of time really.

    Any simple, straightforward help / input?
     
    EnPassant, May 1, 2008 IP
  2. vishnups

    vishnups Banned

    Messages:
    166
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Search for Smarty....You can separate code from design
     
    vishnups, May 1, 2008 IP
    EnPassant likes this.
  3. ggggqqqqihc

    ggggqqqqihc Peon

    Messages:
    92
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Haha, I guess what you need is a template engine. Look at Smarty, a very powerful PHP template engine.
    www.smarty.net
     
    ggggqqqqihc, May 1, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    You may want to look into MVC (Model, View, Controller) style coding. The purpose of MVC is to keep design, and controls separate which makes much more transportable applications.

    Most of the production frameworks use MVC style formatting, and you can make your own MVC framework if you don't need a full framework.
     
    jestep, May 1, 2008 IP
    EnPassant likes this.
  5. EnPassant

    EnPassant Active Member

    Messages:
    612
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #5
    My application is quite small and Smarty is for large/high traffic sites I hear.

    Do you still think Smarty is what I am looking for?!

    All repped.
     
    EnPassant, May 1, 2008 IP
  6. djacobs

    djacobs Well-Known Member

    Messages:
    238
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Listen to suggestion jestep's about the MVC paradigm

    But if you want a basic template system as you suggested, a few lines of code can accomplish that. I have a system I wrote laying on my computer, when I get off of work I'll send you it.
     
    djacobs, May 1, 2008 IP
  7. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I actually do something like this on my websites. I make an index.php file which is very small. It simply includes the main component PHP files which have all the functions/classes/etc and it grabs what page they're trying to access from the vars in the URL and then it initiates an output buffer, includes that file, and stores the file's contents in a variable with ob_get_contents() and then I end that buffer and do the same thing with the main template file and the main template file simply has vars in it that are replaced with whatever was already set in the rest of the index file.

    Easier way to output variables onto a page with PHP though is <?=$var?>, looks so much nicer than <?echo $var;?>

    Here is a real example of what my index file is on one of my sites:
    <?
    
    DEFINE("IS_INDEX", "1");
    
    include("./include.php");
    if(!defined("INCLUDE_FILE"))
      die("Could not load config file.");
    
    $url_var_value = $_GET[$config->url_var];
    
    $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
    
    if(empty($url_var_value))
      $url_var_value = "main";
    
    $requested_page = $config->pages_dir . $url_var_value . ".php";
    	
    if(!file_exists($requested_page))
      $requested_page = $config->pages_dir . "error.php";
    
    $navigation = array();
    $sidemenu = array();
    $header = GetContents("./head.php");
    $content = GetContents($requested_page);
    $body = GetContents("./layout.php");
    
    print $body;
    	
    ?>
    PHP:
    and here is the GetContents function in my include.php file
    function CreateGlobals() {
      #THIS FUNCTION WILL EASILY CREATE A LINE OF CODE
      #TO GLOBAL EVERY VARIABLE THAT CURRENTLY EXISTS
      #IN THE GLOBAL SCOPE SO THAT IT CAN BE USED IN
      #THE LOCAL SCOPE WHERE YOU CALL THIS FUNCTION
    
      #USAGE: eval(CreateGlobals());
      return 'global $' . implode(array_keys($GLOBALS), ',$') . ';';
    }
    
    
    function GetContents($file) {
      eval(CreateGlobals());
      ob_start();
      include($file);
      $contents = ob_get_contents();
      ob_end_clean();
      return $contents;
    }
    PHP:
    In there is also my CreateGlobals function which a lot will argue you should NEVER use but frankly I need EVERY public variable usable in my included files since they're files like my layout and other page files.

    Good luck.
     
    zerxer, May 1, 2008 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145