> For the complete documentation index, see [llms.txt](https://docs.navigaglobal.com/everyware/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.navigaglobal.com/everyware/starter-kit-packages/everyware-twig/helper-classes/viewsetup.md).

# ViewSetup

Helper class to set up Twig for use in the network.

## Registering a Twig directory

Twig uses a file loader system for registering templates. You can register your own "templates directory" if you want to use Twig templates in your theme or plugin. To do this you'll need to provide a `path` to the templates and a `namespace`.

```php
ViewSetup::getInstance()->registerTwigFolder('myNamespace', __DIR__ . '/src/templates/');
```

You'll then be able to use your templates through your `namespace` using a path relative to the provided `path` when rendering with `View`:

```php
# To render the file "/src/templates/page.twig"
View::render('@myNamespace/page');

# or
 
# To render the file "/src/templates/some-path/page.twig"
View::render('@myNamespace/some-path/page');
```

You can also use the `namespace` to `include` and `extend` templates.

```php
{% raw ‰}

<div data-gb-custom-block data-tag="extends" data-0='@myNamespace/page.twig'></div>

<div data-gb-custom-block data-tag="include" data-0='@myNamespace/some-path/page-part.twig'></div>
{% endraw ‰}
```

## Extending Twig

Twig offers multiple ways to be extended, read more about them in their [documentation](https://twig.symfony.com/doc/2.x/advanced.html).

`ViewSetup` extends this way of extending Twig.

### Globals

See Twig documentation [here](https://twig.symfony.com/doc/2.x/advanced.html#globals)

```php
    ViewSetup::getInstance()->addGlobal(string $name, $value);
```

### Filters

See Twig documentation [here](https://twig.symfony.com/doc/2.x/advanced.html#filters)

```php
    ViewSetup::getInstance()->addFilter(string $name, $callable, array $options);
```

> We instantiate a `\Twig\TwigFilter` class for you so you need to provide the data for that.

### Functions

See Twig documentation [here](https://twig.symfony.com/doc/2.x/advanced.html#functions)

```php
    ViewSetup::getInstance()->addFunction(string $name, $callable, array $options);
```

> We instantiate a `\Twig\TwigFunction` class for you so you need to provide the data for that.

### Tests

See Twig documentation [here](https://twig.symfony.com/doc/2.x/advanced.html#tests)

```php
    ViewSetup::getInstance()->addTest(string $name, $callable, array $options);
```

> We instantiate a `\Twig\TwigTest` class for you so you need to provide the data for that.

## Advanced Extension

We also offer support for the more advanced ways of extending Twig.

### Tags

See Twig documentation [here](https://twig.symfony.com/doc/2.x/advanced.html#tags)

```php
    ViewSetup::getInstance()->addTokenParser(Twig\TokenParser\TokenParserInterface $parser);
```

### Extensions

See Twig documentation [here](https://twig.symfony.com/doc/2.x/advanced.html#creating-an-extension)

```php
    ViewSetup::getInstance()->addRuntimeLoader(Twig\RuntimeLoader\RuntimeLoaderInterface $loader);
```

### RuntimeLoader

See Twig documentation [here](https://twig.symfony.com/doc/2.x/advanced.html#definition-vs-runtime)

```php
    ViewSetup::getInstance()->addRuntimeLoader(Twig\RuntimeLoader\RuntimeLoaderInterface $loader);
```
