Views

What Are They? Views usually contain the majority of the HTML your pages display. By putting your HTML in views you keep it separate from the PHP and makes your HTML code a lot easier to read and manage.

Basic Usage Using views is really easy, as is everything else in Salem. To load a view you use the built in view function.

maincontroler.php

<?php
namespace Controller;
use Salem\View;
 
class MainController {
 
	public function index() {
 
		View::render('hello');
 
	}
}
?>

Using the above function would load a view located at application/views/hello.php.

Passing Data To Views You can pass some data to your views like so:

maincontroler.php

<?php
namespace Controller;
use Salem\View;
 
class MainController {
 
	public function index() {	
		View::render('hello', array(
                                                        'title'=>'Amazing!',
                                                        'content'=>"That's cool !"
                                                        )
                 );	
	}
}
?>
Now in your view the data is available in this way

hello.php

<?php use Salem\View; ?>
<!DOCTYPE html>
<html>
        <head><title> <? echo $title; ?> </title></head>
	<body>
                    <b><? echo $content; ?></b>
	</body>
</html>

Views inside other ones When you for some reason want to extend one view adding other view inside this, you can override the content for the new view or prepend the new view to the actual content:

base.php

<?php use Salem\View; ?>
<!DOCTYPE html>
<html>
        <head><title>Base VIEW</title></head>
	<body>
		<?php View::new_section('important', true); ?>			
			<p>Boring default content</p>
			<p>This will be deleted only if we call a view that fill the content of this section</p>			
		<?php View::end_new_section(); ?>
 
		<?php View::new_section('optional', false); ?>			
			<p>Something that stay here.</p>
                        <p>-May be some text will appear under this text-</p>			
		<?php View::end_new_section(); ?>
	</body>
</html>
And now the view that is going to extend this base view:

extra.php

<?php use Salem\View; View::extend('base'); ?>
 
 
<!-- Important Content -->
<?php View::section('important'); ?>
 
	<p>Hello, World!</p>
 
<?php View::end_section(); ?>
 
 
<!-- Optional -->
<?php View::section('optional'); ?>
 
	<p>WOOOoooOOOooOooo</p>
 
<?php View::end_section(); ?>

Finally, when you want to call this extended view you must do:

maincontroler.php

<?php
namespace Controller;
use Salem\View;
 
class MainController {
 
	public function index() {
 
		View::render('extra');
 
	}
}
?>


Go back to Documentation