FryPHP

Examples

FryPHP Hello World

Let's create our main program or controller and name the file index.php:


<?php

ini_set
('error_reporting'E_ALL E_STRICT);
ini_set('display_errors'true);

require_once 
"path_to_fry/core/Fry.php";

try {
    
// lets create new template object
    
$tpl = new FryTemplate("template.php");
    
// set local variable named 'string' to it
    
$tpl->set('string''Hello World!');
    
    
// Create Fry engine
    
$fry = new Fry();
    
// Set previously created template to Fry
    
$fry->setTemplate($tpl);
    
    
// try to render everything and echo result
    
echo $fry->render();
    
} catch (
Exception $e) {
    
    echo 
"<b>Error:</b><br/>\n" $e->getMessage() 
            . 
"<br/>\n<b>Trace:</b><br/>\n" 
            
preg_replace("/(\n)|(\r\n)/""\\1<br/>"$e->getTraceAsString());
}

?>

Now lets create a view - a new template file and name it 'template.php'

<h1>

<?php echo $this->get('string'?>

</h1>

In template, $this refers to Fry object, but using $this in template we get local and global variables and call FryHelpers.

Now lets open our example with a browser and we should see:

Hello World!

Deeply nested templates work just like this example, only one thing - we set one master template with Fry::setTemplate(FryTemplate), and all other nested templates with Fry::setTemplatePart('name', FryTemplate). And in templates we render parts with Fry::renderPart('name'). Example can be seen by downloading FryPHP in 'examples' directory.

More examples

More complex examples can be found by downloading FryPHP in the directory 'examples'.