Skip to content

Latest commit

 

History

History
129 lines (90 loc) · 4 KB

README.md

File metadata and controls

129 lines (90 loc) · 4 KB

STEREO Render

PHP abstraction functions to help more easily render views for Slim Framework (v4) with plain text, HTML, JSON, and Blade (using BladeOne)

These functions aim to provide a simplified and standardized interface for rendering various types of data-driven responses as PSR-7 objects for use with Slim.

Although this package can be used with any Slim4 project, it's specifically designed for use with the STEREO internet toolkit.

Installation

Easy install with composer:

composer require jyoungblood/stereo-render
use Stereo\render;
require __DIR__ . '/vendor/autoload.php';

Requirements

Usage

render::html($request, $response, $string, $status = 200)

Renders a string as HTML. Returns a standard Slim (PSR-7) response object with optional HTTP status code (200 by default).

$app->get('/', function ($req, $res, $args) {

  return render::html($req, $res, '<h2>Hey whats up</h2>');

});

Additionally, a path to an HTML file (relative to the public web root) can be specified to load and render instead of a string:

$app->get('/', function ($req, $res, $args) {

  return render::html($req, $res, 'hey/whats-up.html');

});

render::text($request, $response, $string, $status = 200)

Renders a string as plain text. Returns a standard Slim (PSR-7) response object with optional HTTP status code (200 by default).

$app->get('/', function ($req, $res, $args) {

  return render::text($req, $res, 'Hey whats up');

});

render::redirect($request, $response, $string, $status = 302)

Renders a redirect as standard Slim (PSR-7) response object with optional HTTP status code.

  return render::redirect($req, $res, 'https://google.com/');

render::json($request, $response, $data, $status = 200)

Renders an array or data as standard Slim (PSR-7) response object with application/json content type and optional HTTP status code.

$app->get('/json/', function ($req, $res, $args) {

  $data = [
    'name' => 'Ringo',
    'friends' => [
      'Paul', 'George', 'John'
    ]
  ];

  return render::json($req, $res, $data);

});

render::blade($request, $response, $parameters, $status = 200)

Renders a specific Blade template with an array of data. Returns a standard Slim (PSR-7) response object with optional HTTP status code (200 by default).

$app->get('/', function ($req, $res, $args) {

  return render::blade($req, $res, [
    'template' => 'index',
    'data' => [
      'name' => 'Ringo',
      'friends' => [
        'Paul', 'George', 'John'
      ]
    ],
  ], 200); // optional status code, 200 by default

});

The Blade compiler expects views and cache files to be directories called views and cache, respectively, in the public web root. These defaults, along with the compilation mode, can be customized in your .env file:

BLADE_VIEWS_PATH = "views"
BLADE_CACHE_PATH = "cache"
BLADE_MODE = "AUTO"

The compilation mode can be set to AUTO (default), SLOW, FAST, or DEBUG, see the BladeOne source for more information.

Check out the BladeOne and official Blade documentation to see everything you can do with this incredible templating syntax.

The BladeOne HTML Extension is also included for conveniently creating form components with near-native performance.

render::blade_template($parameters)

Renders a specicific Blade template with data array the same as render::blade(), but returns raw html instead of a PSR-7 response.

$app->get('/', function ($req, $res, $args) {

  echo render::blade_template('email/test', [
    'link' => 'https://jy.hxgf.io',
  ]);

  return $res;
});