Skip to content

Commit

Permalink
Data handling to templates rewritten
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Ek committed Jan 22, 2016
1 parent 418115e commit 35c5f95
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bladerunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Bladerunner
Plugin URI: http://bladerunner.aekab.se
Description: Laravel Blade template engine for WordPress
Version: 1.0.5
Version: 1.0.6
Author: Andreas Ek
Author URI: http://www.aekab.se/
License: MIT License
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ekandreas/bladerunner",
"type": "wordpress-plugin",
"version": "1.0.5",
"version": "1.0.6",
"description": "WordPress Blade L5 template engine",
"keywords": ["laravel", "blade", "wordpress"],
"license": "MIT",
Expand Down
37 changes: 19 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ https://laravel.com/docs/5.2/blade
* Your views must be placed within your theme folder.
* Your templates must have .blade.php extension.

## Pass data to template
A simple way to pass data to a view before it's loaded.

Set the filter ``bladerunner/templates/data`` before running a template to pass custom data to the template, eg:
```
$data = [
'this' => 'that',
'other' => 'perhaps',
];
add_filter('bladerunner/templates/data', compact('data'));
```

Inside your view file you will be able to access the passed data like so:
```
{{ $data['this'] }}
{{ $data['other'] }}
```

## Hooks & Filters
Bladerunner continuously implements filters and hooks to modify values and processes.

Expand All @@ -80,6 +62,25 @@ add_filter('bladerunner/cache/permission', function() {
});
```

#### Template Data Filter
A simple way to pass data to a given view before it's loaded.

Set the filter ``bladerunner/templates/data/{view}`` before running a template to pass custom data to the template, eg:
```
$data = [
'this' => 'that',
'other' => 'perhaps',
];
add_filter('bladerunner/templates/data/single', $data);
```

Inside your "single.blade.php" / view file you will be able to access the passed data like so:
```
{{ $data['this'] }}
{{ $data['other'] }}
```

Default value for data is an empty array.

## Links
* [Bladerunner site with documentation and distro](http://bladerunner.aekab.se)
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: ekandreas
Tags: Blade,templates,development,laravel
Requires at least: 4.4
Tested up to: 4.4
Stable tag: 1.0.5
Stable tag: 1.0.6
License: MIT

WordPress plugin for Blade L5 templating
Expand Down
14 changes: 2 additions & 12 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,13 @@
*/
class Template
{
/**
* Stores passed data.
*
* To pass data use the below example (make sure to run before "template_include" filter):
* \Bladerunner\Template::$data['variableName'] = $value;
*
* @var array
*/
public $data = [];

/**
* Constructor.
*/
public function __construct()
{
add_filter('template_include', [$this, 'templateFilter'], 999);
add_action('template_redirect', [$this, 'addPageTemplateFilters']);
$this->data = apply_filters('bladerunner/templates/data', []);
}

/**
Expand Down Expand Up @@ -79,7 +68,8 @@ public function templateFilter($template)
$content .= " View file '$view_file', compiled at " . date('Y-m-d H:i:s') . "\n";
$content .= "*/\n";
$content .= "\$blade = new \Bladerunner\Blade('$views', '$cache');\n";
$content .= "echo \$blade->view()->make('$view_file', json_decode('" . json_encode(self::$data) . "', true))->render();\n";
$content .= "\$view_data = apply_filters('bladerunner/templates/data/$view_file', []);\n";
$content .= "echo \$blade->view()->make('$view_file', \$view_data)->render();\n";
Cache::storeTemplate($view_file, $content);
}

Expand Down

0 comments on commit 35c5f95

Please sign in to comment.