SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Atlanta
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help on Dynamic Website Please...

    I'm having trouble visualizing the approach I should take to writing this site. It needs to be dynamic. It's a news site so the stories will be changing constantly. I want to build this right from the ground up so that it is modularized and scalable.

    I figure I would create two templates. One for the main page and one for the subsequent story pages. Use PHP includes for the common elements and a $content variable for the actual information. Would this be a good move? If so,

    How should I start thinking about feeding the information into these templates. The front page will have constant navigational elements that link to the stories of the day or week. For example, if story #1 in the first spot is about speakers, where is the best place to pull this story from? A DB? The story about speakers might only be there for a week, it needs to be replaced by the next story about microphones...is there a way to automate this process efficiently. Or should I just create regular .html files and link to them as I normally would?

    I'll have few more questions, but I'll start with these and see if they make sense I appreciate any help on this!

    M

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    they should come from a database yes, if you have a database you should defenatly go that way!

    About templating, I would create my own class to handle the template system, I'm afraid to mention it but Smarty template system is an option but not a very good one. I have a template class that is simple but works, there you can add variables to the template and include some cerain template but it does not have this ugly syntax as smarty does or any complicated things, I can post it if you want...
    - website

  3. #3
    Visible Ninja bronze trophy
    JeffWalden's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,709
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I would be interested in seeing your template.
    TAKE A WALK OUTSIDE YOUR MIND.

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    told you it wasn't much
    PHP Code:
    define ('TEMPLATE_DIR''tpl/');
    define ('TEMPLATE_EXTENSION''.tpl.php');
    class 
    Tpl {
        var 
    $variable;
        var 
    $template;

        function 
    assign($name$value) {
            if (isset(
    $this->variable[$name]))
                die(
    "TPL class: variable already exists");
            else
                
    $this->variable[$name] = $value;
        }
        function 
    display($name) {
            
    $this->template $name;
        }
        function 
    show() {
            global 
    $area;
            if (!empty(
    $this->template)) {
                @
    extract($this->variable);
                require(
    TEMPLATE_DIR $this->template TEMPLATE_EXTENSION);
            } else {
                echo(
    "error: no template selected!");
            }
        }

    And then in my php code I do something like this
    PHP Code:
    $tpl->assign('variable1'$value);
    $tpl->display('sometemplate'); 
    Then I include some standart html file that is always and where the main content should be I do
    PHP Code:
    $tpl->show(); 
    then it loads the given template. The template also gets the variables I assigned so in the template you can do
    PHP Code:
    <h1>this is 'sometemplate'</h1>
    this is the value of variable1: <?=$variable1;?>
    simple but it works.
    - website

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm.... maybe not an issue to you personally WhatTheF but you have PHP script in there... not a good idea on the matter of tiers... if you later plan to construct some sort of framework from this ?

    Maybe as an alternative, here is my HTML template class and some notes on using it...

    PHP Code:
    class Template {

    var 
    $dir;
    var 
    $returns;
    var 
    $filename;
    var 
    $variables;
    var 
    $executables;

    /**
    * set directory pathname
    */
    function SetTempDir($path) {
    $this -> dir $path;
    }

    /**
    * get directory pathname
    */
    function GetTempDir() {
    return 
    $this -> dir
    }

    /**
    * set filename to parse
    */
    function SetTemplate($file) {
    $this -> filename $this -> dir $file;
    }

    /**
    * return a functions return value, if any
    */
    function GetFunctionReturns($num) {
    # get a return result from a function held within class variable(s)
    return $this -> returns[$num];
    }

    /**
    * set TAGS used in template
    */
    function SetTempVariables($x) {
    if(
    is_array($x)) {
    foreach(
    $x as $value) {
    # define class variables for every TAG to use
    $this -> variables[] = $value;
    }
    }
    }

    /**
    * set executable functions
    */
    function SetTempFunctions($x) {
    if(
    is_array($x)) {
    foreach(
    $x as $value) {
    # define class variables for every executable function to use
    $this -> executables[] = $value;
    }
    }
    }

    /**
    * output HTML template with parsed TAGS
    */
    function ParseTemplate() {
    $fp = @ file($this -> filename);

    if(
    $fp) {
    # no errors, so begin
    while(list($number$line) = each($fp)) {
    # remove whitespace from each line
    trim($line);

    $count 0;
    $state false;

    # for each TAG found
    foreach($this -> variables as $var) {
    if(
    ereg($var$line) > (int) 0) {
    # got a TAG, so execute specific function
    $this -> returns[] = $this -> executables[$count]();

    $state true;
    break;
    }
    $count++;
    }
    if(!
    $state) { 
    # output template once completed parsing TAGS
    echo($line); 
    }
    }
    }
    }

    And a template file it's self:
    PHP Code:
    <!doctype html public "-//w3c//dtd html4.01//en" "[url=http://www.w3.org/tr/html4/strict.dtd]http://www.w3.org/tr/html4/strict.dtd[/url]">
    <
    html>
    <
    head>
    <
    title>..:: [Application] ::..</title>
    <
    link rel="stylesheet" type="text/css" href="../application/templates/styles.css" />
    <
    script language="javascript1.2" type="text/javascript" src="jscript.js"></script>
    </head>
    <body topmargin="4" marginheight="4" leftmargin="0" marginwidth="0">
    <table border="0" width="968" height="100%" align="center" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>
    <td width="100%" align="right">
    <img src="images/logo.gif" width="268" height="96" border="0"></td></tr>
    <tr>
    <td width="100%" align="center" height="24" valign="center">
    <tag_links>
    </td></tr>
    <tr>
    <td width="100%" align="center" valign="top" height="100%">
    <br />
    <tag_body>
    </td></tr>
    </tbody></table>
    </body></html> 
    There are 2 tags in there - tag_links and tag_body, so here is how you replace them with your own content - generated via PHP of course LoL:
    PHP Code:
    # variable $cfg['TEMPLATE_DIR'] = 'templates/' btw
    .
    .
    # create new instance of Template class
    #
    $tpl = new Template;
    $tpl -> SetTempDir($cfg['TEMPLATE_DIR']);
    # create new instance of class and set template directory
    # now we must set the actual template file to use
    $tpl -> SetTemplate('index.tpl');
    # also set the 2 tags we have in this template file - as an array
    $tpl -> SetTempVariables(array('tao_links''tao_body'));# and then set 2 functions to execute 
    # each function generates the specific content required
    $tpl -> SetTempFunctions(array('DisplayLinks''DisplayOptionsMenu'));
    # and finally display the template and it's content
    $tpl -> ParseTemplate();
    .

    If at some point you have a function executed via this class which has to return a value then to get access to this value, you'd use for example for the first function:

    PHP Code:
    $returned $tpl -> GetFunctionReturns(0);
    .

    As simple as that really... you will find that the method to add executable functions, it's array can also be built up dynamically as well:

    PHP Code:
    .
    .
    $Op = array();
    .
    .
    # later you have this
    $Op[] = 'FunctionToUse';
    $Op[] = $myVariable;
    .
    .
    # $myVariable could be a function to execute based on using a SWITCH condition for example
    .
    .
    # and so on
    .
    .
    $tpl -> SetTempFunctions($Op); 
    Simple really isn't it ? LoL - any questions etc then PM me.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Atlanta
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yikes!

    Uh oh. I'm new to developing with PHP so that's a bit complicated for me now- or at best, it just doesn't ring a bell so I'm confused. I need to know what the difference is between what you guys have above and just writing a site from scratch without using templates.

    What is the advantage to using those systems when developing a website?

    I appreciate your help and maybe I'll use what you have above on another project- for now I have to learn without the templates so that I can finish this project.

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay - your site once it's up and running for example, has 100 seperate pages right ?

    Your logo is at the top left of the page - all pages - right ?

    Your boss comes along and says to you to put the logo to the top right of the page due to the site's new design, okay ?

    W/out using templates, you'd need to make changes to those 100 pages - like, holly F*** aye ? 100 PAGES you scream...

    Not so w/ templates - you have one page and that's it. Basically all web pages on most sites have the same basic layout and structure nowadays - thus templates were born.

    You have the top part - logo, search box, top level links, then you have the left side for 2nd level links, news items, and the center which is the actual content.

    That's 3 parts really, so your one template page from page to page has the same content basically.

    Maybe you introduce breadcrumb navigation ? Simple using templates as all it takes it a custom tag.

    For more on templating and the reasoning begin it, then have a look over at www.phpbuilders.com and www.devshed.com for more.

    As to which class to use, an unbiased view would be to use my class simply because there is no PHP w/in the markup - IMO a good thing - read more about n-tier theory at www.phppatterns.com.

    Hope this helps you out....

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Atlanta
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all of the information, I'll be reading it all.

    I completely see it, I know it's possible to create templates and more modularized code using PHP/MySQL/CSS - Like using includes for example, I was just confused because it seems like such a huge jump from building a site with includes and the like -- to using Smarty, PostNuke and the templates above.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •