# Using Twig Templates

**URL:** <https://discourse.slimframework.com/t/using-twig-templates/401>\
**Category:** Questions\
**Created:** [June 14, 2016, 7:48pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401 "2016-06-14T19:48:32Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [June 14, 2016, 7:48pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/1 "2016-06-14T19:48:32Z")

</div>

I’m trying to setup a site using Twig templates and I’m able to render a view with not problem, but I’m not sure how to setup partial views in a template. Something similar to this:

```
<html>
<head>
</head>
<body>
<div class="header">Main Header</div>
<div class="container">
// twig views here based on the route
</div>
</body>
</html>

```

Seems like $twig-\>loadTemplate(‘template.html’) is close to what I’m looking for, but not sure if Slim/Twig uses this as well.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![NigelGreenway](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@NigelGreenway](https://discourse.slimframework.com/u/NigelGreenway)\
**Post date:** [June 14, 2016, 8:26pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/2 "2016-06-14T20:26:11Z")

</div>

Hi,

What is your current configuration at the moment for your Slim app?

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [June 14, 2016, 8:37pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/3 "2016-06-14T20:37:27Z")

</div>

Here’s what I currently have. It’s working, but again, not sure how to go about providing views within the template with Twig:

```
$app = new \Slim\App();
$container = $app->getContainer();
$container['view'] = function ($container) {
    $view = new \Slim\Views\Twig('templates/');
    $view->addExtension(new \Slim\Views\TwigExtension(
        $container['router'],
        $container['request']->getUri()
    ));
    return $view;
};

$app->get('/',function($request,$response,$args) {
  return $this->view->render($response, 'template.html');
});

$app->run();
```

---

<div class="post-metadata">

**Author:** ![NigelGreenway](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@NigelGreenway](https://discourse.slimframework.com/u/NigelGreenway)\
**Post date:** [June 14, 2016, 8:52pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/4 "2016-06-14T20:52:06Z")

</div>

just to confirm, are you asking about how to include other templates within each other?

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [June 14, 2016, 8:54pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/5 "2016-06-14T20:54:07Z")

</div>

Yes. Basically I want a main template file (template.html) and then based on the route, I want to include the page content (about.html) within the template.html file.

---

<div class="post-metadata">

**Author:** ![NigelGreenway](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@NigelGreenway](https://discourse.slimframework.com/u/NigelGreenway)\
**Post date:** [June 14, 2016, 8:58pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/6 "2016-06-14T20:58:52Z")

</div>

Ah, so you will want to look at the [Twig docs](http://twig.sensiolabs.org/doc/templates.html).

A simplified version:

```auto
{% extends 'base.html.twig' %}

{% block body %}
<h1>My About Page</h1>

<!-- some stuff -->
{% endblock %}

```

An old repo, but look at [this](https://github.com/NigelGreenway/lister/tree/development/app/templates), it may help?

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [June 14, 2016, 9:00pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/7 "2016-06-14T21:00:48Z")

</div>

I did try the {% extends %} part, but without the .twig extension and got a multiple body tag error. I will dig into a little deeper. But, yes, this is what I’m after. Thanks!

---

<div class="post-metadata">

**Author:** ![NigelGreenway](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@NigelGreenway](https://discourse.slimframework.com/u/NigelGreenway)\
**Post date:** [June 14, 2016, 9:27pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/8 "2016-06-14T21:27:52Z")

</div>

No worries. Any further questions, let me know. 🙂

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 15, 2016, 9:12am UTC](https://discourse.slimframework.com/t/using-twig-templates/401/9 "2016-06-15T09:12:57Z")

</div>

If the

```
{% extends ...%} 

```

tag confuses you, note that you can use

```
{% include ... %}

```

to write templates which are in style/structure equivalent to smarty templates.

I personally use the extends variation, but I’m actually not so sure what the advantage is vs. using a traditional include-based approach.

---

<div class="post-metadata">

**Author:** ![NigelGreenway](https://avatars.discourse-cdn.com/v4/letter/n/bbce88/32.png) [@NigelGreenway](https://discourse.slimframework.com/u/NigelGreenway)\
**Post date:** [June 15, 2016, 9:35am UTC](https://discourse.slimframework.com/t/using-twig-templates/401/10 "2016-06-15T09:35:39Z")

</div>

The `extends` keyword pulls in a template where you can overwrite blocks, whereas the `include` keyword allows you to pull in partial files for specific content.

For example: `about.html.twig` extends `base.html.twig` whereas `about.html.twig` can include `footer.html.twig`.

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 15, 2016, 9:44am UTC](https://discourse.slimframework.com/t/using-twig-templates/401/11 "2016-06-15T09:44:11Z")

</div>

Yeah, it’s just a different way of writing your templates. If you’re coming from something like Smarty, using the include template may be a better option to get you started since that’s a type of structure you’re using to working with.

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [June 15, 2016, 2:24pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/12 "2016-06-15T14:24:50Z")

</div>

I used Smarty for years up until recently, which makes the include my first choice.

Using the {% include ‘about.html’ %} did the trick. I’m sure I overlooked the simplicity over it several times 🙂

Thanks again for the help.

---

<div class="post-metadata">

**Author:** ![robrothedev](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/robrothedev/32/137_2.png) [@robrothedev](https://discourse.slimframework.com/u/robrothedev)\
**Post date:** [June 15, 2016, 2:37pm UTC](https://discourse.slimframework.com/t/using-twig-templates/401/13 "2016-06-15T14:37:02Z")

</div>

So in the end, this is how I have the routing setup. And it works.

Thanks again for the help.

**The App**

```
$app->get('/',function($request,$response) {
	$data["page"] = "home.html";
	return $this->view->render($response,'template.html',$data);
});

$app->get('/about',function($request,$response) {
	$data["page"] = "about.html";
	return $this->view->render($response,'template.html',$data);
});

$app->get('/contact',function($request,$response) {
	$data["page"] = "contact.html";
	return $this->view->render($response,'template.html',$data);
});

```

**The HTML**

```
<html>
<head>

<!-- styles/scripts -->

</head>
<body>
<div class="header">
	<h1>Header</h1>
</div>
<hr>
<div class="content">
	{% include page %}
</div>
</body>
</html>
```

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [June 16, 2016, 12:19am UTC](https://discourse.slimframework.com/t/using-twig-templates/401/14 "2016-06-16T00:19:05Z")

</div>

Just a last comment: for the sake of clarity, you might want to give your templates a ‘.twig’ extension. From a functional point of view this is (of course) completely irrelevant, but I prefer to be able to tell what file something is by looking at it’s filename/extension.
