Views

Introduction

Twig is the primary parsing language for views using the Streams Platform.

This documentation assumes you are already familiar with views in Laravel.

Path Hints

Path hints can be used to hint the location of files consumed by the Asset pipeline like hint::path/to/file.css.

{{ asset_add('styles.css', 'streams::ui/form.css') }}
$asset->add('styles.css', 'streams::ui/form.css');

Available Path Hints

View Hints Reference{.link}

Registering Path Hints

You can use the \Illuminate\View\Factory class to register your own path hints:

$factory->addNamespace('example', base_path('example/path');

The above path hint can now be used like:

view('example::my/view');

Basic Usage

Using Controllers

The base controllers all have Laravel's view factory pre-loaded for you to use directly:

<?php namespace Anomaly\ExampleModule\Http\Controller;

use Anomaly\ExampleModule\Widget\Contract\WidgetRepositoryInterface;
use Anomaly\Streams\Platform\Http\Controller\PublicController;

class WidgetsController extends PublicController
{

    public function index(WidgetRepositoryInterface $widgets)
    {
        return $this->view->make(
            'anomaly.module.example::widgets/index',
            [
                'widgets' => $widgets->all(),
            ]
        );
    }
}

You can also return the view response:

return $this->response->view($view, $data);

The View Helper

You can also use the view helper function:

view($view, $data);

Overriding Views

This section will go over how to cleanly override views that are in core addons or the streams-platform.

Addon Service Providers

View overrides and mobile only overrides can be defined in service providers.

protected $overrides = [
    'streams::form/partials/wrapper' => 'example.theme.test::overrides/field_wrapper',
];

protected $mobile = [
    'streams::form/partials/wrapper' => 'example.theme.test::mobile/field_wrapper',
];

Defining Service Providers{.link}

Publishing Addons

You can override views (as well as config and translation files) by publishing the addon and modifying the published files. Use the addon:publish to copy the addon's resources directory to your application's resources/{application}/addons/ directory so you can modify them and commit them to your project's VCS.

php artisan addon:publish anomaly.module.example

You can also omit the addon argument and publish all addons:

php artisan addon:publish

Make sure to delete the published files that don't need to be modified to defer to originals.{.tip}

Theme Overrides

Auto detection of "Theme Overrides" is being deprecated.{.danger}

When in use, themes can also override views by placing the view overrides in a specific directory.

For example you can override the following addon view:

view('anomaly.module.example::widgets/index');

By placing the overriding view file in your active theme like so:

{active-theme-path}/resources/views/addons/anomaly/example-widget/widgets/index.twig

The override directory in this case is {theme-path}/resources/views/addons/anomaly/example-widget/ while the view path we are overriding is widgets/index and of course the .twig file extension is needed.

Overrides Collection

You can use the add method on the \Anomaly\Streams\Platform\View\ViewOverrides class directly to override views on the fy. Overrides are defined like add($view, $override):

<?php namespace Anomaly\ExampleModule\Command;

use Anomaly\Streams\Platform\View\ViewOverrides;

class OverrideStuff
{
    public function handle(ViewOverrides $overrides)
    {
        $overrides->add('streams::errors/404', 'anomaly.module.example::errors/404');
    }
}

View Includes

View includes are slots that can be loaded with views to render later without having to directly extend the view and use Twig's blocks feature.

For example, if you wanted to include a way to render extra content in the footer from any addon or arbitrary code that might wish to do so, you would use includes to do it.

Defining Include Areas

You can define an include slot by simply trying to render it. If there are no includes in the specified slot then nothing will render:

{{ template.includes.render('footer')|raw }}

Adding Includes to an Area

To add an include to an area you need to use the \Anomaly\Streams\Platform\View\ViewIncludes collection into your class and include your view include to the slot:

use Anomaly\Streams\Platform\View\ViewIncludes;

public function register(ViewIncludes $includes) {
    $includes->include('footer', 'anomaly.module.example::something/cool');
}

Email Layouts

All email layouts are to be placed within layouts/emails to use with the email system.