PHPFront logo
PHPFront Online Documentation now available! PHPFront is available on Composer: composer require ox-harris/phpfront Contact us to place an ad here...

Hello World

Welcome to PHPFront!

Here is a quick setup guide - to get you started in munites.
You're about to say Hello World using PHPFront!

To learn fast, don't just do as I do! Endeavour to figure out all the whys. Use the inline comments.

All examples in the rest of this documentation are borrowing from this example.
So, if you can do this, you can do just anything!

Requirements

We assume that you have the PHPFront Template Engine already installed on your system. Download here, if not.
And by now, you must have a web server running the required version of PHP for PHPFront.

Here we go.

  1. Create a new PHP file named app.php – just the name for this example - put it somewhere on your server.
  2. Copy the PHPFront folder to the same directory as the app.php file.
  3. Create a plain HTML file named template.html and put the file in this same directory.

Code

Now, let's code our files.

template.html

      

        <!DOCTYPE html>
        <html>
        <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <!--  First part of this title's text will come from PHPFront. It will be prepended. -->
          <title> - PHPFront</title>
        </head>
        
        <body>
          
          <header>
            <nav id="menu">
              <!--  Text for all three links will come from PHPFront. So we can leave them blank. -->
              <a id="link1" href="/phpfront/"></a>
              <a id="link2" href="/phpfront/about"></a>
              <a id="link3" href="/phpfront/follow"></a>
            </nav>
          </header>
            
          <main>
            <!--  Texts for this h1 and p elements will come from PHPFront. They will replace these initial texts. -->
            <h1>[This is default Heading 1 text]</h1>
            <p>This is default Paragraph text.</p>
            
            <aside id="sidebar_wrapper">
            </aside>
          </main>
        
          <footer>
            <small> (c) 2016 Me </small>
          </footer>
          
        </body>
        </html>

      
      

app.php

      
        
        // Include the PHPFront class.
        include 'PHPFront/lib/PHPFront.php';
        
        // If you stored the PHPFront folder in a different location, 
        // your include path would change.
        // Where 'path-to-PHPFront' is the actual path to where you stored PHPFront
        include 'path-to-PHPFront/PHPFront/lib/PHPFront.php';
        
        // Is PHPFront available yet? Let's call it. (Remember to remove this test.)
        PHPFront::info();
        
        // PHPFront is now available to our app.php script, 
        // so we instantiate it:
        $PHPFront = new PHPFront;

        // Now we can start assigning content to the respective elements in 
        // the template using PHPFront's assign() function
        
        // For document title (title)
        // We are placing this content just before the title's existing text, 
        // using the CSS pseudo class '::before'
        $PHPFront->assign('title::before', 'Hello World!');
        
        // For page heading 1 (h1)
        $PHPFront->assign('h1', 'Hello World!');
        
        // For page paragraph (p)
        $PHPFront->assign('p', 'Here is my first PHPFront project');
        
        // For our menu list (with id='menu')
        $PHPFront->assign('#menu', array(
        	'@children' => array(
            	'#link1' => 'Home',
                '#link2' => 'About Me',
                '#link2' => 'Follow Me'
        	)
        ));
        
        // Now, we hand PHPFront the template to use - our template.html page
        $PHPFront->setTemplate('template.html');
        
        // If your stored template.html in a different location, your path would change.
        // Where 'path-to-template' is the actual path to where you stored template.html
        $PHPFront->setTemplate('path-to-template/template.html');
        
        // Finally we render
        $PHPFront->render();
        
      
      

Preview your app.php in a browser to see the work of your hand!

Hope that was a breeze. Now you can do just anything!

Back to Documentation Home

See Also