Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
beebmx committed Jan 23, 2019
0 parents commit 080e1bd
Show file tree
Hide file tree
Showing 876 changed files with 87,786 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/kirby
composer.lock
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Fernando Gutiérrez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Kirby Blade

Kirby Blade use Laravel `illuminate/view` and `jenssegers/blade` packages.

This package enable [Laravel Blade](https://laravel.com/docs/5.7/blade) for your own Kirby applications.

## Installation

### Installation with composer

```ssh
composer require beebmx/kirby-blade
```

## What is Blade?

According to Laravel Blade documentation is:

> Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension.
## Usage

You can use the power of Blade like [Layouts](https://laravel.com/docs/5.7/blade#template-inheritance), [Control Structures](https://laravel.com/docs/5.7/blade#control-structures), [Sub-Views](https://laravel.com/docs/5.7/blade#including-sub-views), Directives and your Custom If Statements.

All the documentation about Laravel Blade is in the [official documentation](https://laravel.com/docs/5.7/blade).

## Options

The default values of the package are:

| Option | Default | Values | Description |
|:--|:--|:--|:--|
| beebmx.kirby-blade.views | site/cache/views | (string) | Location of the views cached |
| beebmx.kirby-blade.directives | [] | (array) | Array with the custom directives |
| beebmx.kirby-blade.ifs | [] | (array) | Array with the custom if statements |

All the values can be updated in the `config.php` file.

### Views

All the views generated are stored in `site/cache/views` directory or wherever you define your `cache` directory, but you can change this easily:

```php
'beebmx.kirby-blade.views' => '/site/storage/views',
```

### Directives

By default Kirby Blade comes with 4 directives:

```php
@js('js/app.js')
@css('css/app.css')
@kirbytext($page->text())
@kt($page->text())
```

But you can create your own:

```php
'beebmx.kirby-blade.directives' => [
'greeting' => function ($text) {
return "<?php echo 'Hello: ' . $text ?>";
}
],
```

### If Statements

Like directives, you can create your own if statements:

```php
'beebmx.kirby-blade.ifs' => [
'logged' => function () {
return !!kirby()->user();
},
],
```

After declaration you can use it like:

```php
@logged
Welcome back {{ $kirby->user()->name() }}
@else
Please Log In
@endlogged
```
39 changes: 39 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "beebmx/kirby-blade",
"description": "Enable Blade for Kirby 3",
"keywords": [
"kirby", "kirby-3", "blade", "view", "template"
],
"version": "1.0.0",
"type": "kirby-plugin",
"license": "MIT",
"authors": [
{
"name": "Fernando Gutierrez",
"email": "fernando@beeb.mx"
}
],
"require": {
"php": "^7.1.3",
"composer/installers": "~1.0"
},
"require-dev": {
"illuminate/view": "^5.7",
"jenssegers/blade": "^1.1"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"files": [
"config.php",
"helpers.php"
],
"psr-4": {
"Beebmx\\": "src/"
}
},
"minimum-stability": "dev"
}
19 changes: 19 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Kirby\Cms\App as Kirby;
use Beebmx\Template;

Kirby::plugin('beebmx/kirby-blade', [
'options' => [
'views' => function () {
return kirby()->roots()->cache() . '/views';
},
'directives' => [],
'ifs' => [],
],
'components' => [
'template' => function (Kirby $kirby, string $name, string $contentType = null) {
return new Template($kirby, $name, $contentType);
}
]
]);
9 changes: 9 additions & 0 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

function b($value, $doubleEncode = true)
{
if ($value instanceof Htmlable) {
return $value->toHtml();
}
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode);
}
3 changes: 3 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';
28 changes: 28 additions & 0 deletions src/Blade/Blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Beebmx\Blade;

use Jenssegers\Blade\Blade as BladeProvider;
use Beebmx\View\ViewServiceProvider;
use Illuminate\Container\Container;

class Blade extends BladeProvider
{
/**
* Constructor.
*
* @param string|array $viewPaths
* @param string $cachePath
* @param ContainerInterface $container
*/
public function __construct($viewPaths, $cachePath, ContainerInterface $container = null)
{
$this->viewPaths = $viewPaths;
$this->cachePath = $cachePath;
$this->container = $container ?: new Container;
$this->setupContainer();

(new ViewServiceProvider($this->container))->register();
$this->engineResolver = $this->container->make('view.engine.resolver');
}
}
156 changes: 156 additions & 0 deletions src/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Beebmx;

use Kirby\Cms\App as Kirby;
use Kirby\Cms\Template as KirbyTemplate;
use Beebmx\Blade\Blade;
use Exception;
use Kirby\Toolkit\F;
use Kirby\Toolkit\Tpl;
use Kirby\Toolkit\Dir;

class Template extends KirbyTemplate
{
protected $blade;
protected $views;
protected $defaultType;
protected $name;
protected $templates;
protected $type;
public static $data = [];

public function __construct(Kirby $kirby, string $name, string $type = 'html', string $defaultType = 'html')
{
$this->templates = $kirby->roots()->templates();
$this->views = $this->getPathViews();

$this->name = strtolower($name);
$this->type = $type;
$this->defaultType = $defaultType;

$this->setViewDirectory();
}

/**
* Detects the location of the template file
* if it exists.
*
* @return string|null
*/
public function file(): ?string
{
if ($this->hasDefaultType() === true) {
try {
// Try the default template in the default template directory.
return F::realpath($this->getFilename(), $this->root());
} catch (Exception $e) {
//
}
// Look for the default template provided by an extension.
$path = Kirby::instance()->extension($this->store(), $this->name());
if ($path !== null) {
return $path;
}
}
$name = $this->name() . '.' . $this->type();
try {
// Try the template with type extension in the default template directory.
return F::realpath($this->getFilename(), $this->root());
} catch (Exception $e) {
// Look for the template with type extension provided by an extension.
// This might be null if the template does not exist.
return Kirby::instance()->extension($this->store(), $name);
}
}

/**
* @param array $data
* @return string
*/
public function render(array $data = []): string
{
if ($this->isBlade()) {
$this->blade = new Blade(
$this->templates,
$this->views
);
$this->setDirectives();
$this->setIfStatements();

return $this->blade->make($this->name, $data);
} else {
return Tpl::load($this->file(), $data);
}
}

public function setViewDirectory()
{
if (!file_exists($this->views)) {
Dir::make($this->views);
}
}

protected function setDirectives()
{
$this->blade->compiler()->directive('js', function ($path) {
return "<?php echo js($path) ?>";
});

$this->blade->compiler()->directive('css', function ($path) {
return "<?php echo css($path) ?>";
});

$this->blade->compiler()->directive('kirbytext', function ($text) {
return "<?php echo kirbytext($text) ?>";
});

$this->blade->compiler()->directive('kt', function ($text) {
return "<?php echo kirbytext($text) ?>";
});

foreach ($directives = option('beebmx.kirby-blade.directives', []) as $directive => $callback) {
$this->blade->compiler()->directive($directive, $callback);
}
}

protected function setIfStatements()
{
foreach ($statements = option('beebmx.kirby-blade.ifs', []) as $statement => $callback) {
$this->blade->compiler()->if($statement, $callback);
}
}

public function getFilename()
{
if ($this->isBlade()) {
return $this->root() . '/' . $this->name() . '.' . $this->bladeExtension();
} else {
return $this->root() . '/' . $this->name() . '.' . $this->extension();
}
}

public function isBlade()
{
return !!file_exists($this->root() . '/' . $this->name() . '.' . $this->bladeExtension());
}

/**
* Returns the expected template file extension
*
* @return string
*/
public function bladeExtension(): string
{
return 'blade.php';
}

protected function getPathViews()
{
$path = option('beebmx.kirby-blade.views');
if (is_callable($path)) {
return $path();
}
return $path;
}
}
Loading

0 comments on commit 080e1bd

Please sign in to comment.