Download the PHP package mpphp/view without Composer
On this page you can find all versions of the php package mpphp/view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package view
Short Description MPPHP view library.
License MIT
Homepage https://mpphp.github.io/mpphp/
Informations about the package view
Veiw
MPPHP view library.
A view is the file that contains the code that gets printed on the browser period. And the MVC architecture demands that we keep our business logic away from these view files. Traditionally beginners i thought to make database calls or process their forms on the same file as the form:
<form method="post">
// Required inputs
<input type="submit" name="submit" value="Sign in">
</form>
This would be a bad idea because now your View now has knowledge of your backend code. Now most people at this point would say theu write their logic in a different file and include it like so:
<form method="post">
// Required inputs
<input type="submit" name="submit" value="Sign in">
</form>
Well technically, the two versions are the same, the view is still aware of the back end logic.
There are lots of third party Templating Engines out there like Smarty, Latte, Mustache, but most PHP frameworks come bundled with their own Templating engine, for example Symfony has Twig, Laravel has Blade and Phalcon has Volt. And MpPHP well, it just has a single file containing functions that mimic the important features of a Templating Engine like Inheritance and Sections/Blocks that we like to call VIEW.
Defining A Layout
Two of the primary benefits of using a templating engine are template inheritance and sections. To get started, let's take a look at a simple example. First, we will examine a "master" page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Blade view:
As you can see, this file contains typical HTML mark-up. However, take note of the _viewnest() and the _view__include() functions. The _viewinclude function, as the name implies, includes a template, while the _view__nest() function is used to display the contents of a given section.
Now that we have defined a layout for our application, let's define a child page that inherits the layout.
Extending A Layout
When defining a child vtemplate, use the _viewextends() function to specify which layout the child template should "inherit". Templates which extend a layout may inject content into the parent template by wrapping its content in a function with the same name passed into it's parents _view__nest() function. Remember, as seen in the example above, the contents of these sections will be displayed in the layout _viewnest('content'):
<!-- Stored in resources/views/child.phtml -->
Child Page
<p>This is my body content.</p>
From this section down, we would be talking about best practices for using PHP in a view or a template file.
Displaying Data
To display a PHP value to the browser, we would use echo
or print
but if the only thing your doing withing a PHP opening an closing tag is an echo
or print
you can use the shorthand version like so:
<?= $data ?>
Much simpler than:
or
Dont you think?
Control Structures
It is common for young developers to mix their view with business logic in a way that makes it impossible for a front end developer who has little knowledge of PHP to work with their projects, for example it is comon to find a view that has the following code:
<body>
</body>
If your working on a school project with your friends and your job is at the back end of things, i guess we can all agree the above could be alot for the person handling the front. A cleaner way to get this do would be to drop out of PHP as soon as we don't need it:
<body>
<div>
<p>
<?= $info ?>
</p>
</div>
</body>
This would be easier for any front end personel to work on your project without having to worry about PHP, I mean it's there but they can easily be ignored compared to the previous version.
This step would work for both Control structures and logical expressions likewise.