PHP code example of popphp / pop-view

1. Go to this page and download the library: Download popphp/pop-view library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

popphp / pop-view example snippets


<html>
<body>
    <h1><?=$title; 

use Pop\View\View;

$view        = new View('hello.phtml');
$view->title = 'Hello World!';

echo $view;

$view = new View('hello.phtml', ['title' => 'Hello World!']);

<!DOCTYPE html>
<html>
<head>
    <title><?=$title; 

use Pop\View\View;
use Pop\View\Template\File;

$view = new View(new File('hello.phtml'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

use Pop\View\View;
use Pop\View\Template\Stream;

$view = new View(new Stream('hello.html'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

$view = new View(new Stream('welcome.html'), ['user' => ['name' => 'Nick']]);

use Pop\View\View;
use Pop\View\Template\Stream;

$view = new View(new Stream('index.html'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

use Pop\View\View;
use Pop\View\Template\Stream;

$view = new View(new Stream('child.html'));
$view->title   = 'Hello World!';
$view->content = 'This is a test!';

echo $view;

use Pop\View\View;
use Pop\View\Template\Stream;

$data = [
    'items' => [
        'hello' => 'world',
        'foo'   => 'bar',
        'baz'   => 123
    ]
];

$view = new View(new Stream('index.html'), $data);

echo $view;

$data = [
    'rows' => [
        ['title' => 'First Post',  'content' => 'Some content here.'],
        ['title' => 'Second Post', 'content' => 'Some more content.'],
    ]
];

$data = [
    'items' => [
        'pages' => ['Page One', 'Page Two', 'Page Three']
    ]
];

use Pop\View\View;
use Pop\View\Template\Stream;

$data = ['foo' => 'bar'];

$view = new View(new Stream('index.html'), $data);

echo $view;

$data = ['user' => ['name' => 'Nick']];

$data = [
    'rows' => [
        ['title' => 'First Post', 'featured' => true],
        ['title' => 'Second Post'],
    ]
];

use Pop\View\View;
use Pop\View\Template\Stream;

$view = new View(new Stream('index.html', '/path/to/cache/dir'), $data);

echo $view;

// or, equivalently:
$template = new Stream('index.html');
$template->setCacheDir('/path/to/cache/dir');

$view = new View($template, $data);

use Pop\View\View;
use Pop\Filter\Filter;

$view = new View('hello.html', [
    'title' => '<b>Hello World</b>',
], new Filter('strip_tags'));

echo $view; // <b> tags stripped from every value in the data

$view = new View('hello.html', $data);
$view->addFilter(new Filter('strip_tags'));
$view->addFilters([
    new Filter('htmlentities', [ENT_QUOTES, 'UTF-8']),
]);

$view = new View('hello.html', [
    'title'   => '<b>Hello</b>',
    'content' => '<b>World</b>',
]);

// 'content' is left untouched; only 'title' gets stripped of tags
$view->addFilter(new Filter('strip_tags', null, 'content'));

use Pop\View\View;

$view = new View('hello.html');

// Property access
$view->title = 'Hello World!';

// Array access
$view['content'] = 'This is a test!';

// Explicit accessors
$view->set('foo', 'bar');
echo $view->get('foo');

// Merge additional data in without disturbing what's already set
$view->merge(['content' => 'This is a test!']);

// Replace all data at once
$view->setData(['title' => 'Hello World!']);

// Read everything back
$allData = $view->getData();