# Views

## Introduction

Views contain all the HTML that you’re application will use to render to the user. Unlike Django, views in Masonite are your HTML templates. All views are located inside `resources/templates`directory.

All views are rendered with Jinja2 so we can use all the Jinja2 code you are used to. An example view looks like:

{% code title="resources/templates/helloworld.html" %}

```markup
<html>
  <body>
    <h1> Hello {{ 'world' }}</h1>
  </body>
</html>
```

{% endcode %}

## Creating Views

Since all views are located in `resources/templates`, we can use simply create all of our views manually here or use our `craft` command tool. To make a view just run:

{% code title="terminal" %}

```
$ craft view hello
```

{% endcode %}

This will create a template under `resources/templates/hello.html`.

## Calling Views

### Helper Function

There are several ways we can call views in our controllers. The first recommended way is using the `view()` function. Masonite ships with a `HelpersProvider` Service Provider. This provider will add several new built in functions to your project. These helper functions can be used as shorthand for several commonly used classes such as the `View` and `Request` class. See the [Helper Functions](https://docs.masoniteproject.com/1.6/the-basics/helper-functions) documentation for more information.

One of the helper functions is the `view()` function which is accessible like any other built in Python function.

We can call views in our controllers like so:

{% code title="app/http/controllers/YourController.py" %}

```python
def show(self):
    return view('dashboard')
```

{% endcode %}

This will return the view located at `resources/templates/dashboard.html`. We can also specify a deeper folder structure like so:

{% code title="app/http/controllers/YourController.py" %}

```python
def show(self):
    return view('profiles/dashboard')
```

{% endcode %}

This will look for the view at `resources/templates/profiles/dashboard.html`

### From The Container

The `View` class is loaded into the container so we can retrieve it in our controller methods like so:

{% code title="app/http/controllers/YourController.py" %}

```python
def show(self, View):
    return View('dashboard')
```

{% endcode %}

This is exactly the same as using the helper function above. So if you choose to code more explicitly, the option is there for you.

{% hint style="success" %}
If this looks weird to you or you are not sure how the container integrates with Masonite, make sure you read the [Service Container](https://docs.masoniteproject.com/1.6/architectural-concepts/service-container) documentation
{% endhint %}

### Global Views

Some views may not reside in the `resources/templates` directory and may even reside in third party packages such as a dashboard package. We can locate these views by passing a `/` in front of our view.

For example as a use case we might pip install a package:

{% code title="terminal" %}

```
$ pip install package-dashboard
```

{% endcode %}

and then be directed or required to return one of their views:

{% code title="app/http/controllers/YourController.py" %}

```python
def show(self):
    return view('/package/views/dashboard')
```

{% endcode %}

This will look inside the `dashboard.views` package for a `dashboard.html` file and return that. You can obviously pass in data as usual.

## Global View Caveats

### Template Location

It's important to note that if you are building a third party package that integrates with Masonite that you place any `.html` files inside a Python package instead of directly inside a module. For example, you should place .html files inside a file structure that looks like:

```python
package/
  views/
    __init__.py
    index.html
    dashboard.html
setup.py
MANIFEST.in
...
```

and not inside the package directory. This is a Jinja limitation that says that all templates should be located in packages.

Accessing a global view such as:

{% code title="app/http/controllers/YourController.py" %}

```python
def show(self):
    return view('/package/dashboard')
```

{% endcode %}

will perform a relative import for your Masonite project. For example it will catch:

```
app/
config/
databases/
...
package/
  dashboard.html
```

So if you are making a package for Masonite then keep this in mind in where you should put your templates

### Extending Views

When you extend a view, you are isolated to the directory you are in when you want to extend templates and you're view namespace loses it's globalization and all extending should be done relative to the current template. For example, if you have a package with a directory structure like:

```python
package/
  controllers/
    PackageController.py
  templates/
    __init__.py
    index.html # needs to inherit base.html
    base.html
setup.py
MANIFEST.in
...
```

And a controller like:

{% code title="package/controllers/PackageController.py" %}

```python
def show(self):
    return view('/package/templates/index')
```

{% endcode %}

You will have to extend your template like so:

{% code title="package/templates/index.html" %}

```markup
<!-- This is right -->
{% extends 'base.html' %}

{% block content %}
    ... index.html code goes here ...
{% endblock %}
```

{% endcode %}

and not extend your template with another global view:

{% code title="package/templates/index.html" %}

```markup
<!-- This is wrong -->
{% extends '/package/templates/base.html' %}

{% block content %}
    ... index.html code goes here ...
{% endblock %}
```

{% endcode %}

## Passing Data to Views

A lot of the time we’ll need to pass in data to our views. This data is passed in with a dictionary that contains a key which is the variable with the corresponding value. We can pass data to the function like so:

{% code title="app/http/controllers/YourController.py" %}

```python
def show(self, Request):
    return view('dashboard', {'id': Request.param('id')})
```

{% endcode %}

{% hint style="info" %}
Remember that by passing in parameters like `Request` to the controller method, we can retrieve objects from the IOC container. Read more about the IOC container in the [Service Container](https://docs.masoniteproject.com/1.6/architectural-concepts/service-container) documentation.
{% endhint %}

This will send a variable named `id` to the view which can then be rendered like:

{% code title="resources/templates/dashboard.html" %}

```markup
<html>
  <body>
    <h1> {{ id }} </h1>
  </body>
</html>
```

{% endcode %}
