Skip to content Skip to sidebar Skip to footer

Repeating A Block Of Html Like A Function

I am learning html, css and content management by building my own personal site, but I have very little experience in any of these areas. One thing that has bothered me is the amou

Solution 1:

Since you asked for a live example, here is one in PHP:

news_print.php

functionoutput_some_news ($link, $title, $subtext)
{
    echo"<div class='talk'>
     <a href='$link'>
         $title
        <div class='info'>
            $subtext
       </div>
     </a>
 </div>";
}

// this is just for show. Usually the data come from a database or data file$topics = array (
    array ("http://celebslife.com", "Breaking news", "Justin Bieber just grew a second neuron"),
    array ("http://nerds.org"     , "New CSS draft available", "We won't be forced to use idiotic lists to implement menus in a foreseeable future"));

functionoutput_news ($topics)
{
    foreach ($topicsas$topic)
    {
        output_some_news  ($topic[0], $topic[1], $topic[2]);
    }
}

And from within your HTML page:

news.php

<?phpinclude'news_print.php'; ?><divclass='news'><?php output_news($topics); ?></div>

As for using PHP as a preprocessor, it is pretty straightforward:

C:\dev\php\news> php news.php > news.html

will produce pure HTML from your PHP script.

The PHP engine will be invoked from the command line instead of a web server, and its output will be stored in a file instead of being sent back to a browser, that's all.

Of course you will have some differences. For instance, all the web-specific informations like caller URL, cookies, etc. will not be available if you use PHP offline. On the other hand, you will be able to use command line arguments and environment variables.

Post a Comment for "Repeating A Block Of Html Like A Function"