Views

Views in PyroCMS work exactly the same as views in Laravel.

**Heads Up:** Pyro's default rendering engine is [Twig](https://twig.sensiolabs.org/).

Path Hints

To avoid having to use full paths to your views there are a number of path hints available. Hints are a namespace that prefixes the view path.

"theme::hello" // path-to-your-active-theme/resources/views/hello.twig

"anomaly.module.products::admin/products/index" // path-to-products-module/resources/views/admin/products/index.twig
Available Path Hints

All paths are relative to your application's base path.

  • module: {active_module_path}/resources/views/
  • theme: {active_theme_path}/resources/views/
  • published: /resources/{app_reference}/addons/
  • app: /resources/{app_reference}/views/
  • storage: /storage/streams/{app_reference}/
  • shared: /resources/views/
  • root: /

Addons also have path hints associated to them:

  • vendor.module.example: {addon_path}/resources/views/
Registering Path Hints

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

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

Now you can use that path hint for views:

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

Basic Usage

This section will briefly go over how to render a view. For more information on rendering views please refer to Laravel's documentation.

Using Controllers

Controllers in Pyro 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(),
            ]
        );
    }
}
The View Helper

You can also use the view helper function exactly the same as above like you normally would in Laravel from any class:

return view(
    'anomaly.module.example::widgets/index',
    [
        'widgets' => $widgets->all(),
    ]
);

Overriding Views

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

Addon Service Providers

You can write a service provider and define view overrides as well as mobile only overrides there.

Publishing Addons

You can override views among other things by simply publishing the addon and modifying it's files. Publishing copies the addon's resources to your resources/{app_reference}/addons/ directory so you can modify and commit them to your project.

php artisan addon:publish anomaly.module.example

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

php artisan addon:publish

Now all you need to do is modify what you need and delete the rest!

**Note:** The view system will use the original addon view if it does not exist in the published location.
Theme Overrides

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:

{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.

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 your 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 could use includes to do this.

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 inject the \Anomaly\Streams\Platform\View\ViewIncludes collection into your class or method and use add($slot, $include) to add your view include to the slot:

use Anomaly\Streams\Platform\View\ViewIncludes;

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