# Sage9 Template & Wrapper

**URL:** https://discourse.roots.io/t/sage9-template-wrapper/11769
**Category:** sage
**Tags:** sage9, blade
**Created:** 2018-03-01T11:56:43Z
**Posts:** 7

## Post 1 by @jrgd — 2018-03-01T11:56:43Z

I want to get a specific template file rendered/populated through Blade.  
In the class, I need to replace the (old?) `(new Template(new Wrapper($template)))->layout();` to something that works with the latest Sage9

My template path seems correct (using locate\_template); however I tried with both template\_include filter and echo App\template(): both seems to interpret PHP just fine but output any blade statements without interpreting them i.e. raw text outputs appear in the page like “@extends(‘layouts.app’)”; there must be something I’m doing that is wrong :slight_smile:

How can I render the page?

---

## Post 2 by @ben — 2018-03-01T17:31:02Z

Pasting from [the Sage book](https://roots.io/books/theme-development-with-sage/):

## Rendering Blade templates from PHP (eg. hooks/actions and filters)

Find yourself needing to render a Blade template with PHP? Use the `template` helper function available in the `App` namespace.

Here’s an example of adding a `[reviews]` shortcode that renders the `partials/reviews.blade.php` file:

```
add_shortcode('reviews', function($atts) {
    return \App\template('partials.reviews');
});
```

---

## Post 3 by @jrgd — 2018-03-01T18:23:43Z

Thanks @ben I will be focusing on the App\template then

I’m using [Cortex](https://github.com/Brain-WP/Cortex) to set custom routes; it seems awesome but I must be doing something wrong somewhere and it’s probably down to my poor understanding :persevere:

I replaced `$template = (new Template(new Wrapper($tmpl)))->layout();`  
with

```
$tmpl_location = locate_template('views/'.$tmpl.'.blade.php');
 $template = App\template($tmpl_location);
```

It now shows a page withe the element but no css and overrides my custom templates to render the custom query.  
When I output what the helpers’ template function received: it shows the $file twice for a single request; the first output looks good (it’s my custom template file location); the second time it reverts to the default index.blade.php

I should probably look into Cortex more to better understand how this works.

Btw, the book along the blog, discourse and the slack chan are such precious tools to learn more; my brain felt a joyful overdrive trying to pierce the logic to render templates. Thank you so much for all the work!

---

## Post 4 by @jrgd — 2018-03-01T18:32:08Z

Actually it seems to be hitting the \App\template twice all the time – on a distinct (default front page) url, without the Cortex loaded.

---

## Post 5 by @jrgd — 2018-03-07T11:13:51Z

Turns out it was pretty simple to sort the Brain/Cortex issue in the first place:  
**_do not use the custom template parameter_** ¯\_(ツ)\_/¯

Thanks to @alwaysblank for updating [his answers](https://discourse.roots.io/t/template-and-controller-naming-for-custom-taxonomy/10701/6)

---

## Post 6 by @zacarias.calabria — 2018-06-13T16:50:46Z

Hello Jerome,

First of all I apologize for my English level, I hope we can understand each other.

I have the same problem as you, the solution proposed in the thread,

> <https://github.com/Brain-WP/Cortex/issues/15>

it does not work for versions 9.1. \* of Sage, but I do not understand the solution proposed by @alwaysblank, did it finally work for you? How did you do it?

Thanks in advance,

---

## Post 7 by @jrgd — 2018-06-13T21:07:47Z

Hey Zacarias, sorry I missed the thread on Github  
this is the route that worked in the end—as you can see it doesn’t use a custom template, just the normal route and Sage will pick up the template based on the post type. Hope this helps!

```
$routes->addRoute(new QueryRoute(
  'works/airing/{year:[0-9]+}',
  function(array $matches) {
    $airing_year = $matches['year'];
    return [
      'numberposts' => -1,
      'post_type' => 'po_te_films',
      'meta_query' => array(
          array(
            'key' => 'air_date',
            'value' => $airing_year,
            'compare' => 'LIKE',
          ),
      ),
    ];
  }
));
```
