Download the PHP package dijitaltrix/scythe-view without Composer
On this page you can find all versions of the php package dijitaltrix/scythe-view. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package scythe-view
Scythe View
pronounced [sahyth] - Olde English word for a curved blade for cutting by hand
A simple implementation of Laravel Blade for rendering blade syntax in views into a PSR-7 Response object. This has no dependencies and it works great with Slim Framework 3.
If you're unfamiliar with Blade as a view renderer it's main advantage is it's lightness, it is essentially a simple wrapper around PHP. The syntax behaves as a PHP programmer would expect it to, so for those familar with PHP there is very little mental juggling required to understand Blade.
This implementation does not aim for feature parity, that is beyond the scope of this project. It does offer some useful methods and the ability to add custom directives through callbacks. If you need full compatibility you can use the full Laravel Blade package via philo/laravel-blade.
Note: Currently unsupported features @verbatim
@component
@hasSection
@auth
@guest
*These could be added using a directive callback to suit your framework
Table of Contents
- Installation
- Getting Started
- Language Reference
- Comments
- Variables
- Variable manipulation
- Control Structures
- Loops
- Inheritance
- Directives
- Stacks
- Methods
- addDirective
- addNamespace
- exists
- renderString
- To do or not to do
Installation
Install with Composer:
Getting started
Use with Slim 3
Add Scythe to your container, passing the required parameters views_path
and cache_path
And assuming you set views_path as app/src/views
you may use it as follows:
You may add namespaces and directives using the methods outlined in the Methods section below.
Use with any PSR-7 Project
Language Reference
Comments
Opening and closing php tags can be used follows
@php
@endphp
Variables
All variable display functions will escape your output using htmlentities() except for echo raw: {!! $danger !!}
echo
Displays the contents of $name
echo raw
Use this with caution when displaying user generated data
echo with a default
Displays 'Anonymous' if $name is not set
set
Sets the value of a variable
unset
Removes a variable from the scope
isset
Checks whether a variable is set
has
Checks whether a variable is set and has a value
Variable manipulation
Twig has some really nice string modifiers, we have these ;) All examples below are wrapped with htmlentities()
upper
Converts the string to uppercase
lower
Converts the string to lowercase
ucfirst
Converts the string to lowercase, then capitalises the first word
ucwords
Converts the string to lowercase, then capitalises each word
format
This is just a wrapper around the sprintf function, to help your muscle memory you may also call it as @sprintf
wrap
Control structures
if
Scythe supports all possible combinations of if structures
unless
If NOT something, then what?
switch
Loops
for
foreach
forelse
while
each
A more compact version of the foreach loop that adds the @include command
It also supports the generation of the forelse structure if you pass a view to display when there are no records
Loop variable
A loop helper is available, this works identically to the Blade $loop helper
Property | Description |
---|---|
$loop->index | The index of the current loop iteration (starts at 0). |
$loop->iteration | The current loop iteration (starts at 1). |
$loop->remaining | The iteration remaining in the loop. |
$loop->count | The total number of items in the array being iterated. |
$loop->first | Whether this is the first iteration through the loop. |
$loop->last | Whether this is the last iteration through the loop. |
$loop->depth | The nesting level of the current loop. |
$loop->parent* | When in a nested loop, the parent's loop variable. |
*Not implemented yet
Inheritance
extends
Calls a parent template to insert sections into
section
Sections define areas of content to be inserted into a parent template
parent
Sections can be merged between templates using the @parent directive.
Will display as
replace or yield
Replaced with the contents of the child template section definition
include
Include another blade template, all variables will be available to the included template
Directives
Define your own placeholders that insert content via callbacks
Stacks
Push to stacks from your child templates, then they will be appended to the @stack directive in your parent template
Methods
In addition to the Blade syntax, the renderer instance offers the following methods
addDirective($placeholder, $callback)
addNamespace($name, $path)
Namespaces allow you to add view paths outside the default views_path. Use them for external packages or to help organise your code into modules.
exists($template)
The exists()
function will return true
if the template file is in the view path, or false
if not.
renderString($string, $data=[])
The renderString()
function will parse a string converting any blade commands .
To do or not to do
Components and slots
No plans to introduce this, use @include as an alternative.
@inject
No plans to implement this I feel it's not good practice to allow the view to pull in whatever data classes etc.. it likes. Data and dependencies should be generated in the 'Domain' and injected into the view Response so the view should already have everything it needs.
As the template renders down to plain php you are of course able to execute in any code you like between PHP tags.
Custom if statements
It is possible to add similar functionality through custom Directives