www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: Easy title/keyword tags

  1. #16
    Join Date
    Apr 2012
    Posts
    23
    Quote Originally Posted by NogDog View Post
    I might do something like this (or I might not ):
    PHP Code:
    <?php
    /**
     * Get HTML text for title element plus keywords and description meta tags
     * @return string
     * @param string $pageName Top-level key in $data array
     */
    function getHtmlHead($pageName="default")
    {
       
    $data = array(
          
    "default" => array(
             
    "title" => "Default Title",
             
    "keywords" => array(
                
    "word1",
                
    "word2"
             
    ),
             
    "description" => "This is a sample description."
          
    ),
          
    "index" => array(
             
    "title" => "Home",
             
    "keywords" => array(
                
    "foo",
                
    "bar"
             
    ),
             
    "description" => "Welcome to the home page."
          
    ),
          
    "schedule" => array(
             
    "title" => "Schedule",
             
    "keywords" => array(
                
    "Larry",
                
    "Moe",
                
    "Curly"
             
    ),
             
    "description" => "This is the schedule page."
          
    )
       );
       if( ! 
    array_key_exists($pageName$data)) {
          
    $pageName "default";
          
    error_log("Invalid pageName '$pageName' in ".__FUNCTION__."()");
       }
       
    $template = <<<EOD
    <html>
    <head>
    <title>%s</title>
    <meta name='keywords' content='%s' />
    <meta name='description' content='%s' />
    </head>
    <body>

    EOD;

       return 
    sprintf(
          
    $template,
          
    htmlspecialchars($data[$pageName]['title']),
          
    htmlspecialcahrs(implode(', '$data[$pageName]['keywords'])),
          
    htmlspecialchars($data[$pageName]['description'])
       );
    }
    Usage:
    PHP Code:
    <?php
    require_once 'path/to/the/above/file.php';
    echo 
    getMetaTags('schedule');
    ?>
    <div>Main content here...</div>
    Hi NogDog,

    A belated question/reply to this thread but I wanted to ask you something about the solution you provided.

    Basically I am using what some people describe as an ungly case/switch method within my index.php gile to switch the title tags etc etc.

    Your method looks nice and I wondered if there are any performance implications using a nested array?

  2. #17
    Join Date
    Dec 2011
    Posts
    34
    I think you will find the performance neglible.
    a switch/case & if/else will create code like
    Code:
        move AX,VariableA
        compare AX, ConstantA
        jumpnotequal Compare2
        move ax, Data1
        jump done
    Compare2:
        compare AX, ConstantB
        jumpnotequal, Compare3
        move ax, Data2
        jump done
    ...
    ...
    while an array system will reuse the comparison routine
    Code:
       move AX, VariableA
       MOV DX, ArrayConstant
       move IX, 0
    Compare1:
       Compare [DX+IX], AX
       jumpequal done
       Increment IX
       jump Compare1
    done:
    .
    .
    .
    Although the use of the array shortens the amount of code produced, it does have the slight overhead of the math to calculate the position of the next element. The code snippets are pseudo code, and very simplified, but it's just to give an idea of what is produced by the interpreter/compiler.

    For myself, I prefer using an array system. Because it keeps code more compact. When you been around for awhile and see if/else...switch/case statements hitting 30-50 blocks. and trying to fix/update the code becomes a chore.

    My rule of thumb is never to use more than 3-5 if/else...switch/case statements. At this point consider using an array system.


    New coders often find using arrays difficult so postpone learning them.

    Anyways good luck on yer coding

  3. #18
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Another good rule of thumb is to code for maintainability first, and worry about performance only if and when you find that you have to. (More often than not, your main performance bottlenecks will be the database, not your PHP code -- unless it's really, really awful ). That's not to say you shouldn't think about code efficiency at all, but I'd rather worry about saving hours of debugging versus microseconds of performance.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #19
    Join Date
    Apr 2012
    Posts
    1
    To all,
    Great work. You all have given very valuable information
    Thank You

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles