Views

In PIP a view is simply a web page containing HTML code with (optional) snippits of PHP code. Views are almost always loaded by Controllers.

Loading Views

So, for example, if you had a view called welcome.php (in folder app/views/home) that contained the following HTML:

<html>
<head>
<title>My Site</title>
</head>
<body>
    <div id="main">
        <h1>Welcome to my Site!</h1>
    </div>
</body>
</html>

... you would load it in a controller by doing the following:

<?php

class HomeController extends Controller {
    
    function getIndex()
    {
        // show welcome page
        return View::make('home/welcome')
                    ->show();
    }
}
?>

Passing and Displaying Data

Pass data to the view using ->with($key, $val) :

    return View::make('home/welcome')
            ->with('someval', 200)
            ->show();
    

...then in the view you could do:

     <?php echo $someval; ?>

...and the output would be 200. Any kind of PHP variable can be passed to a view in this way.

Inserting Partials

PIP allows you to insert blocks of text (partials) from a file. Use the PIP short-hand [[ ... ]] (double square brackets) and the name of the block you want to insert.

<html>
<head>
<title>My Site</title>
</head>
<body>
    <div id="main">
      [[ main ]]
    </div>
</body>
</html>

And in your controller you can insert content from a file into the [[ place-holder ]]. The insert function takes 3 arguments, the place-holder name, the file name and an extension (the default extension is PHP).

    return View::make('home/welcome')
            ->insert('main', 'home/text', 'html')
            ->with('someval', 200)
            ->show();
    

Templating

Why use a Template?

Of course, you could use a full blown template like Blade or Twig to help simplify your views, but PHP already provides everything you need (see php.net - Alternative syntax for control structures), and PIP makes it even easier.

A good reason to use a template engine is for readability. The normal PHP start and end tags <?php ... ?> do tend to hinder readability and PHP short tags <? ... ?> are discouraged (see php.net - Use of short tags).

So PIP provides a simple compromise.

You can use standard PHP tags <?php ?>, or replace them with PIP's short tags {{ }}.

PIP Tags

PIP allows you to replace PHP tags with double curly braces:

    <?php ... ?> becomes {{ ... }}  and
    <?php echo ... ?> becomes {{= ... }} 

You can then use the PHP alternative syntax (with a colon rather than curly braces) for loops and conditional statements. You still need to include the semi-colon at the end of the statement.

Display a variable

    <?php echo $album->title(); ?>
    or
    {{= $album->title(); }}
    

If Statement

    // if
    {{ if($album->hasArtist()): }}
        // true
    {{ endif; }}
  

If / Else Statement

    // if / else
    {{ if($album->title() == ''): }}
        // no title
    {{ else: }}
        // show title
    {{ endif; }}
    
    // Shorthand version
    {{= ($album->hasArtist()) ? $member->artist() : 'No Artist'; }}
    

Foreach loops

    // foreach
    {{ foreach($albums as $album): }}
        <p>The album name is {{= $album->title; }}.</p>
    {{ endforeach }}

See PHP.net - Control stuctures - alternative syntax for more examples


Good practice: Embed your PHP output within your HTML, rather than generating HTML code in echo statements.