Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post

    Website template with classes

    I recently decided that my website needed a new way of being generated, and after a bit of research, I came to the conclusion that classes would be the best for this.

    Unfortunately, I've come to a few dead ends, the first of which is adding menu items.

    Here is my code so far.

    Classes
    PHP Code:
    <?php
    class template{
        private 
    $template;

        public function 
    __construct($filepath){
            
    $this->template file_get_contents($filepath);
        }

        public function 
    replace($var$content){
            
    $this->template str_replace("#$var#"$content$this->template);
        }

        public function 
    publish(){
            eval(
    "?>{$this->template}<?");
        }
    }

    class 
    menu{
        public 
    $menus = array();

        public function 
    __construct($name$url$current){
            
    $this->menus[$name] = $url[$current];
        }
    }
    ?>

    <?php
    $template 
    = new template("layout.php");

    new 
    menu("Item 1""layout.php"0);
    new 
    menu("Item 2""http://www.google.com"1);

    print_r($menus);

    $template->replace("content""This has been replaced correctly.");

    $template->publish();
    ?>
    layout.php
    PHP Code:
    <html>
    <
    head>
        <
    title>
            
    Title
        
    </title>
    </
    head>

    <
    body>
        <
    div>
            
    #menu
        
    </div>

        <
    div>
            
    #content#
        
    </div>
    </
    body>
    </
    html
    Basically, I want to be able to use "new menu("Item 1", "layout.php", 0);" to add menu items to #menu#.

    I thought it might be best to use some sort of array, but to be honest, I've no idea what would be best.

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You have to seperate the different parts.

    Your template needs to first collect all components, then assemble them in a predetermined way.

    In your case.

    Code:
    template = new template
    menuitem1 = new menuitem
    menuitem2 = new menuitem
    menuitem3 = new menuitem
    
    template -> add menuitem1
    template -> add menuitem2
    template -> add menuitem3
    As you guessed, arrays are the correct way.

    PHP Code:
    class Template {
        private 
    $menu = array();
        public function 
    addMenuItem($item) {
            
    $this->menu[] = $item;
        }

    Then once you ask the template class for the output, build a string with the menu code, do a search and replace for #menu# on the output. This means that you have to work with the output, so you'd have to use ob_get_contents() to gather this. Then do your search and replace. Once you're done with that send the output as you would normally (using echo).

    EDIT: The reason I suggested ob_get_contents() instead of just loading the template with file_get_contents, is that if you first include the template, and then use ob_get_contents(), you can still have PHP in your template. Which leaves you more options.

  • Users who have thanked aedrin for this post:

    Jacka (11-14-2007)

  • #3
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post
    EDIT: Nevermind, I have worked out how to do the 2nd one.

    PHP Code:
    $this->menus[$name] = array("url" => $url"current" => $current); 
    Is this a sensible way to store them in the array?

    It's output as
    Code:
    Array
    (
        [Item 1] => Array
            (
                [url] => layout.php
                [current] => 0
            )
    
        [Item 2] => Array
            (
                [url] => http://www.google.com
                [current] => 1
            )
    
    )

    Or would it be better to get
    Code:
    Array
    (
        [0] => Array
            (
                [name] => Item 1
                [url] => layout.php
                [current] => 0
            )
    
        [1] => Array
            (
                [url] => Item 2
                [url] => http://www.google.com
                [current] => 1
            )
    
    )
    If the 2nd is better, how would I achieve this?
    Last edited by Jacka; 11-14-2007 at 09:23 PM. Reason: Edit

  • #4
    New Coder
    Join Date
    Dec 2006
    Location
    GB
    Posts
    79
    Thanks
    3
    Thanked 1 Time in 1 Post
    EDIT: I needed to refer to $menus as $this->menus;

    PHP Code:
        public function finishmenus(){
            
    ob_start();
                echo 
    "    <ul>\n";
                foreach(
    $menus as $menu){
                    echo 
    "        <li>\n";
                    echo 
    "            <a href=\"{$menu[url]}\"";
                    if(
    $menu[current]){
                        echo 
    " class=\"current\"";
                    }
                    echo 
    ">\n";
                    echo 
    "                {$menu[name]}\n";
                    echo 
    "            </a>\n";
                    echo 
    "        </li>\n";
                }
                
    $buffer ob_get_contents();
            
    ob_end_clean();
            
    $this->replace(menu$buffer);
        } 
    With this, I'm getting the error
    Code:
    Invalid argument supplied for foreach() in file on line 26
    Do I have to do something fancy in the foreach?
    Last edited by Jacka; 11-14-2007 at 09:39 PM.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    EDIT: Nevermind, I have worked out how to do the 2nd one.
    Correct, unless you need to do lookup by names, you want to store them in a plain array with a numeric index.


  •  

    Posting Permissions

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