-
Notifications
You must be signed in to change notification settings - Fork 8
Views
Note: This documentation covers Inphinit 1.0. For the 2.1 documentation, visit: https://inphinit.github.io/en/docs/views.html
For use first put Inphinit\Viewing\View in your controller (or other script), like this:
<?php
use Inphinit\Viewing\View;A simple example:
View::render('foo'); //register ./system/application/View/foo.phpNote: the Views render after
App::exec(see./system/boot/start.php)
You can use multiple views, example:
<?php
namespace Controller;
use Inphinit\Viewing\View;
class Foo
{
public function about()
{
View::render('components.header'); //register ./system/application/View/components/header.php
View::render('pages.about'); //register ./system/application/View/pages/about.php
View::render('components.footer'); //register ./system/application/View/components/footer.php
}
}You can use View::forceRender() before if needs force render Views, example:
View::forceRender();
View::render('foo');This makes Views are rendered before App::exec, use only if necessary.
You can send variables to View using this way:
View::render('foo', array(
'title' => 'example',
'foo' => 'foo foo foo',
'bar' => 'bar bar bar',
'baz' => 10001
));For get variables in ./system/application/View/foo.php use:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<p>$foo: <?php echo $foo; ?></p>
<p>$bar: <?php echo $bar; ?></p>
<p>$baz: <?php echo $baz; ?></p>
</body>
</html>If you using PHP 5.4+ or enable short tags in php.ini (short_open_tag=On) you can use:
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
</head>
<body>
<p>$foo: <?=$foo?></p>
<p>$bar: <?=$bar?></p>
<p>$baz: <?=$baz?></p>
</body>
</html>You can include View inside another View using like this:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php
//Render view ./system/application/View/components/header.php
View::render('components.header');
?>
<p>$foo: <?php echo $foo; ?></p>
<p>$bar: <?php echo $bar; ?></p>
<p>$baz: <?php echo $baz; ?></p>
<?php
//Render view ./system/application/View/components/footer.php
View::render('components.footer');
?>
</body>
</html>Views called within by other views are rendered at the same time as the main view
For share data in all Views use View::data($key, $value) function, for example:
<?php
use Inphinit\Viewing\View;
View::data('foo', 1);
View::data('bar', 2);
View::data('baz', 3);
Route::set('GET', '/', function () {
View::render('header');
View::render('home', array(
'text' => 'Hello, world!'
));
View::render('footer');
});
Route::set('GET', '/about', function () {
View::render('header');
View::render('home', array(
'text' => 'About us!'
));
View::render('footer');
});In ./system/application/View/header.php put this:
<?php
var_dump('header', $foo, $bar, $baz);In ./system/application/View/footer.php put this:
<?php
var_dump('footer', $foo, $bar, $baz);In ./system/application/View/home.php put this:
<?php
var_dump('home', $foo, $bar, $baz, $text);In ./system/application/View/about.php put this:
<?php
var_dump('about', $foo, $bar, $baz, $text);If navigate to http://localhost/ will return:
string(6) "header"
string(8) "foo data"
string(8) "bar data"
string(8) "baz data"
string(4) "home"
string(8) "foo data"
string(8) "bar data"
string(8) "baz data"
string(13) "Hello, world!"
string(6) "footer"
string(8) "foo data"
string(8) "bar data"
string(8) "baz data"
If navigate to http://localhost/about will return:
string(6) "header"
string(8) "foo data"
string(8) "bar data"
string(8) "baz data"
string(5) "about"
string(8) "foo data"
string(8) "bar data"
string(8) "baz data"
string(9) "About us!"
string(6) "footer"
string(8) "foo data"
string(8) "bar data"
string(8) "baz data"
Inphinit the PHP Framework is writed using PSR-1, PSR-2 and PSR-4, you can use Controllers, Views, Routes
- Getting Started
- About
- Performance
- Directory Structure
- Constants
- Routing
- Controllers
- Views
- Events
- HTTP Response
- HTTP Request
- HTTP Redirect
- HTTP Content Negotiation
- Storage and Files
- Rest
- Maintenance
- Regular Expression
- Response Cache
- DOM, XML, HTML, JSON and Arrays
- QuerySelector (CSS-selectors) with PHP
- Configuration
- Instalação
- Sobre
- Desempenho
- Estrutura das pastas
- Constantes
- Rotas
- Usando Controllers
- Usando Views
- Eventos
- Resposta HTTP
- Requisição HTTP
- Redirecionamento HTTP
- Negociação de Conteúdo HTTP
- Armazenamento e Arquivos
- Usando Rest
- Manutenção
- Expressão Regular
- Cache Resposta
- DOM, XML, HTML, JSON e Arrays
- QuerySelector (seletores-CSS) com PHP
- Configuração