glowieframework/skeltchgo

Standalone Skeltch templating engine for PHP

v1.0.3 2023-03-16 00:32 UTC

This package is auto-updated.

Last update: 2024-11-10 20:46:49 UTC


README

SkeltchGo

Latest Version Total Downloads License PHP Version

Lightweight PHP templating engine

SkeltchGo is a standalone version of Glowie Skeltch templating engine for PHP, intented to use outside the framework.

Requirements

  • PHP version 7.4 or higher
  • Composer version 2.0 or higher

Installation

Through Composer:

composer require glowieframework/skeltchgo

Usage

Create a SkeltchGo instance through the static make() method.

// Include Composer autoloader
require_once('vendor/autoload.php');

// Setup SkeltchGo
use Glowie\SkeltchGo\SkeltchGo;
$skeltch = SkeltchGo::make();

This method returns an instance of ViewRenderer.

The make() method accepts three optional arguments:

  • viewFolder (string) - Folder where the view files are stored, relative to the running script. (Defaults to views)
  • cacheFolder (string) - View cache folder, relative to the running script. Must have writing permissions. (Defaults to cache)
  • cache (bool) - Enable views caching. Highly recommended in a production environment. (Defaults to true)

Rendering views

Views must be .phtml files inside the views folder. Extension is not needed.

From the script

$skeltch->renderView('myView');

From another view

{ view('myView') }

Rendering partials

To render a view in a private scope, use partials. They will not inherit global or parent view parameters.

From the script

$skeltch->renderPartial('myView');

From another view

{ partial('myView') }

Rendering layouts

Layouts must be .phtml files inside the views folder. Extension is not needed. The second parameter is an optional view file to render within the layout.

From the script

$skeltch->renderLayout('myLayout', 'myView');

From another view

{ layout('myLayout', 'myView') }

To retrieve the internal view content inside the layout use:

{ content }

Passing parameters

There are two ways of passing parameters to the views:

// Globally to all views at once
$skeltch->view->myParam = 'Lorem ipsum';

// Restricted to a single view and its childs
$skeltch->renderView('myView', [
    'myParam' => 'Lorem ipsum'
]);

Then retrieve it in the view as a property of itself:

{{ $this->myParam }}

View helpers

Setup a helper method by passing a name and a closure to the helper() method:

$skeltch->helper('sayHello', function($name){
    return "Hello, $name!";
});

Then call it in your view file using:

{{ $this->sayHello('World') }}

Documentation

To learn how to use all methods and templating syntax, read Skeltch complete documentation.

Note: some Skeltch methods are restricted to the framework environment and are not available in SkeltchGo.

Credits

SkeltchGo and Glowie are currently being developed by Gabriel Silva.