https://github.com/folded-php/view
View utilities for your PHP web app.
https://github.com/folded-php/view
blade-template laravel php view
Last synced: 10 days ago
JSON representation
View utilities for your PHP web app.
- Host: GitHub
- URL: https://github.com/folded-php/view
- Owner: folded-php
- License: mit
- Created: 2020-09-05T13:55:52.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-17T07:50:03.000Z (over 5 years ago)
- Last Synced: 2025-05-15T10:08:51.591Z (8 months ago)
- Topics: blade-template, laravel, php, view
- Language: PHP
- Homepage: https://packagist.org/packages/folded/view
- Size: 125 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# folded/view
View utilities for your PHP web app.
[](https://github.com/folded-php/view/blob/master/LICENSE) [](https://github.com/folded-php/view/blob/master/composer.json#L14) [](https://packagist.org/packages/folded/view) [](https://travis-ci.com/folded-php/view) [](https://codeclimate.com/github/folded-php/view/maintainability) [](https://www.tickgit.com/browse?repo=github.com/folded-php/view)
## Summary
- [About](#about)
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Examples](#examples)
- [Version support](#version-support)
## About
I created this library to be able to pull it in an existing web app without too much effort setting it up.
Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.
- [folded/action](https://github.com/folded-php/action): A way to organize your controllers for your web app.
- [folded/config](https://github.com/folded-php/config): Configuration utilities for your PHP web app.
- [folded/crypt](https://github.com/folded-php/crypt): Encrypt and decrypt strings for your web app.
- [folded/exception](https://github.com/folded-php/exception): Various kind of exception to throw for your web app.
- [folded/history](https://github.com/folded-php/history): Manipulate the browser history for your web app.
- [folded/http](https://github.com/folded-php/http): HTTP utilities for your web app.
- [folded/orm](https://github.com/folded-php/orm): An ORM for you web app.
- [folded/request](https://github.com/folded-php/request): Request utilities, including a request validator, for your PHP web app.
- [folded/routing](https://github.com/folded-php/routing): Routing functions for your PHP web app.
- [folded/session](https://github.com/folded-php/session): Session functions for your web app.
## Features
- Can render Blade views, and pass data to it
- Can render plain PHP views
- Can add data to a specific views beforehand (handy if you always need some data in your layouts for example)
- Eager load the view engine such as it is not booted until you call `displayView()`
## Requirements
- PHP version >= 7.4.0
- Composer installed
## Installation
- [1. Install the package](#1-install-the-package)
- [2. Set up the view engine](#2-set-up-the-view-engine)
### 1. Install the package
In your root folder directory, run this command:
```bash
composer require folded/view
```
### 2. Set up the view engine
In the script that is called before displaying your view, add this set up code:
```php
use function Folded\setViewFolderPath;
use function Folded\setViewCacheFolderPath;
setViewFolderPath(__DIR__ . "/views");
setViewCacheFolderPath(__DIR__ . "/cache/views");
```
The cache is a place where your code is compiled to plain PHP, and stored in the disk, for faster rendering for next requests displaying the view.
## Examples
Since this library relies on Laravel's [illuminate/view](https://github.com/illuminate/view), you can refer to [the 7.X documentation](https://laravel.com/docs/7.x/blade) if you need any information regarding the Blade syntax and the available Blade directives.
- [1. Display a view](#1-display-a-view)
- [2. Pass data to your view](#2-pass-data-to-your-view)
- [3. Display a plain PHP view](#3-display-a-plain-php-view)
- [4. Always pass certain data to a view](#4-always-pass-certain-data-to-a-view)
- [5. Get the rendered view](#5-get-the-rendered-view)
### 1. Display a view
In this example, we will display a Blade view.
```php
use function Folded\displayView;
displayView("home.index");
```
With the following content inside `views/home/index.blade.php`:
```html
Hello world
```
### 2. Pass data to your view
In this example, we will pass a string to the view we display.
```php
use function Folded\displayView;
displayView("home.index", [
"name" => "John",
]);
```
With the following content inside the view:
```html
Hello world
Welcome to my website, {{ $name }}.
```
### 3. Display a plain PHP view
In this example, we will display a plain PHP file.
```php
use function Folded\displayView;
displayView("about-us.index");
```
The plain PHP view is located at `views/about-us/index.php` (notice there is no "blade" extension now), with the following content:
```html
About us
```
### 4. Always pass certain data to a view
In this example, we will pass a company name to a layout view, to be able to not add it to every view that extends the layout.
```php
use function Folded\addDataToView;
addDataToView("layouts.base", [
"companyName" => "Folded",
]);
```
### 5. Get the rendered view
In this example, we will get the rendered view in a variable (useful to send emails for example).
```php
use function Folded\getRenderedView;
$content = getRenderedView("emails.account-created");
```
## Version support
| | 7.3 | 7.4 | 8.0 |
| ------ | --- | --- | --- |
| v0.1.0 | ❌ | ✔️ | ❓ |
| v0.1.1 | ❌ | ✔️ | ❓ |
| v0.1.2 | ❌ | ✔️ | ❓ |
| v0.2.0 | ❌ | ✔️ | ❓ |
| v0.2.1 | ❌ | ✔️ | ❓ |
| v0.2.2 | ❌ | ✔️ | ❓ |
| v0.2.3 | ❌ | ✔️ | ❓ |
| v0.3.0 | ❌ | ✔️ | ❓ |