PHP code example of dijitaltrix / scythe-view

1. Go to this page and download the library: Download dijitaltrix/scythe-view library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dijitaltrix / scythe-view example snippets


$container['view'] = function($c) {
    return new \Dijitaltrix\Views\Scythe([
        'views_path' => 'path/to/views',
        'cache_path' => 'path/to/cache',
    ]);
}

// app/routes.php
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->view->render($response, "hello", $args);
});

// app/src/views/hello.blade.php
<h1>Hello {{ $name }}</h1>


$view = new \Dijitaltrix\Views\Scythe([
    'views_path' => 'path/to/views',
    'cache_path' => 'path/to/cache',
]);

$response = $view->render(new Response(), "/path/to/template", $data);


{{ $name }}
//  echo htmlentities($name); 

{!! $name !!}
//  echo $name; 

{{ $name or 'Anonymous' }}
//  (isset($name)) ? htmlentities($name) : 'Anonymous'; 

@set($muppet, 'Kermit')
//  $muppet = 'Kermit'; 

@unset($muppet)
//  unset($muppet); 

@for ($i = 0; $i < 11; $i++)
    Currently reading {{ $i }}
@endfor

@foreach ($muppets as $muppet)
    <p>Say howdy to {{ $muppet->name }}</p>
@endforeach

@forelse ($muppets as $muppet)
    <p>Say howdy to {{ $muppet->name }}</p>
@empty
    <p>Sadly no muppets can be with us today</p>
@endforelse

@while (true)
    <p>One Infinite Loop</p>
@endwhile

@each('muppets/profile', $muppets, 'muppet')

// @foreach ($muppets as $muppet)
//   @

@each('muppets/profile', $muppets, 'muppet', 'muppets/profile-missing')

// @forelse ($muppets as $muppet)
//   @

@section('body')
<ul>
@foreach ($muppets as $muppet)
    @

// output a string
$view->addDirective('/@hello/i', 'Hello world');

// output the result of a callback
$view->addDirective('/@hello/i', function(){
    return 'Hello world';
});
//TODO pass parameters from directive to callback

// child.blade.php
@extends('parent')

@section('title', 'The title')

@push('head')
<link rel="stylesheet" href="/css/child.css" type="text/css" media="screen">
@endpush

@push('scripts')
<script src="child.js"></script>
<script type="text/javascript">
    alert('Some alert');
</script>
@endpush

// parent.blade.php
<html>
<head>
    @stack('head')
</head>
<body>
    @yield('body')
    @stack('scripts')
</body>
</html>
    


$view->addDirective('@rand', function() {
    if (rand(1,10) > 5) {
        throw new Exception("This is pointless");
    }
});


// add the namespace name first, followed by the path to the root of the namespace
$view->addNamespace("admin", "/src/Admin/views");

// namespaces are referenced with '::'
// for example
$view->exists('admin::user/dashboard');

// this will load the file at src/Admin/views/user/profile.blade.php
$view->render($response, 'admin::user/profile', $args);


$view->exists('admin/dashboard')

// Namespaces are specified as follows
$view->exists('admin::user/dashboard')

$blade = "<h1>{{ $title }}</h1>";

$view->renderString($blade, ['title'=>'The Muppet Show']);

// <h1>The Muppet Show</h1>