Skip to content

Commit

Permalink
Added support for rendering Wordpress Templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiopvilar committed Aug 2, 2015
1 parent 94c02f8 commit f724ced
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,20 @@ Route::get('hello/:name', function($req, $res){
});
```


You can also use layouts and template blocks, read more in [Twig documentation](http://twig.sensiolabs.org/documentation).

### Wordpress templates

Don't want to use Ampersand templates? Not problem. You can also use Wordpress templates:

```php
<?php

Route::get('search', function($req, $res){
$res->template('search'); // Will render your-theme/search.php
});
```

## Models

Ampersand does not include features related to models but it works well with [Hero](https://github.com/alterfw/hero).
Expand Down
10 changes: 10 additions & 0 deletions src/http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ public function render($template, $data = []) {
$this->write(\Render::template($template, $data, false), true);
}

private function getTemplate($template){
ob_start();
require(get_query_template($template));
return ob_get_clean();
}

public function template($template){
$this->write($this->getTemplate($template));
}

public function toJSON($data){
$this->headers = new \Slim\Http\Headers(array('Content-Type' => 'application/json'));
$this->write(json_encode($data), true);
Expand Down
10 changes: 2 additions & 8 deletions src/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,7 @@ public function rewrite_url( $wp_rewrite ) {

$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}

private function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
}

public function parse_request($wp_query) {

Expand All @@ -158,7 +152,7 @@ public function parse_request($wp_query) {
$this->getCallback($route, $wp_query->query_vars);
} else {
Ampersand::getInstance()->response()->setStatus(404);
Ampersand::getInstance()->response()->write($this->requireToVar(get_query_template( '404' )));
Ampersand::getInstance()->response()->template('404');
}

Ampersand::getInstance()->run();
Expand Down

0 comments on commit f724ced

Please sign in to comment.