Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Mitchum committed Jan 23, 2016
0 parents commit 9cdcae4
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v1.0.0
## 01/23/2016

1. [](#new)
* ChangeLog started...
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Grav

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Grav Plugin - View

`View` is a [Grav](http://github.com/getgrav/grav) plugin that allows you find a page and send the collection to a template.

## Features

* Override parameters from referencing page.
* Assign any template to any view.
* Add views to modular pages, allowing you to manage, order, etc.

# Installation

Installing the View plugin can be done in one of two ways. Our GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.

## GPM Installation (Preferred)

The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's Terminal (also called the command line). From the root of your Grav install type:

bin/gpm install view

This will install the View plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/view`.

## Manual Installation

To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `view`. You can find these files on [GitHub](https://github.com/ellioseven/grav-plugin-view).

You should now have all the plugin files under

/your/site/grav/user/plugins/view

> NOTE: This plugin is a modular component for Grav which requires [Grav](http://github.com/getgrav/grav), the [Error](https://github.com/getgrav/grav-plugin-error) and [Problems](https://github.com/getgrav/grav-plugin-problems) plugins, and a theme to be installed in order to operate.
# Usage

1. Add modular to modular page
2. Select 'View' as Modular Template
3. Specify a page. Optionally, you can specify a template, pagination, and parameters.
4. If you specify a template, you will need to create it in your theme,
`/your/site/grav/user/themes/templates/<template>`, where template is the path of your specified template.

## Using Parameters

You can override the parameters from the referencing page, so you can do things like this:

```
items:
'@taxonomy.category': 'animals'
```

## View Template

You will have 2 extra variables available to you:

1. `view` - The collection made from the referencing page.
2. `context` - The modular template used to display the view.

A simple template may look like so:

```
<h1>{{ context.title }}</h1>
<ul>
{% for item in view %}
<li>{{ item.title }}</li>
{% endfor %}
</ul>
```

### Default View Template

You can also define a default template as a fallback in the plugin configuration )`http://example
.com/admin/plugins/view`).

[Page collections](http://learn.getgrav.org/content/collections) are very powerful here, and allow you to filter the
output of the page collection into your view.

# Updating

As development for View continues, new versions may become available that add additional features and functionality, improve compatibility with newer Grav releases, and generally provide a better user experience. Updating View is easy, and can be done through Grav's GPM system, as well as manually.

## GPM Update (Preferred)

The simplest way to update this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm). You can do this with this by navigating to the root directory of your Grav install using your system's Terminal (also called command line) and typing the following:

bin/gpm update view

This command will check your Grav install to see if your View plugin is due for an update. If a newer release is found, you will be asked whether or not you wish to update. To continue, type `y` and hit enter. The plugin will automatically update and clear Grav's cache.

## Manual Update

Manually updating View is pretty simple. Here is what you will need to do to get this done:

* Delete the `your/site/user/plugins/view` directory.
* Downalod the new version of the View plugin from [GitHub](hhttps://github.com/ellioseven/grav-plugin-view).
* Unzip the zip file in `your/site/user/plugins` and rename the resulting folder to `view`.
* Clear the Grav cache. The simplest way to do this is by going to the root Grav directory in terminal and typing `bin/grav clear-cache`.

> Note: Any changes you have made to any of the files listed under this directory will also be removed and replaced by the new set. Any files located elsewhere (for example a YAML settings file placed in `user/config/plugins`) will remain intact.
12 changes: 12 additions & 0 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: View
version: 1.0.0
description: A plugin that allows you find a page and it's collection to a template.
icon: cog

form:
fields:
header.view.template:
type: text
label: 'Default Modular Template'
placeholder: 'eg: partials/view'
help: 'Specify a template to use display view collection'
47 changes: 47 additions & 0 deletions blueprints/modular/view.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
title: View
'@extends': default

form:
fields:
tabs:
fields:
view:
type: tab
title: View
fields:
basic:
type: section
title: Basic
underline: true
fields:
header.view.page:
type: pages
label: Page
help: 'Specify a page to retreive view collection'
validate:
required: true
header.view.template:
type: text
label: 'Modular Template'
placeholder: 'eg: partials/view'
help: 'Specify a template to use display view collection'
header.view.pagination:
type: toggle
label: 'Pagination'
highlight: 1
options:
1: 'Yes'
0: 'No'
validate:
type: bool
help: 'Enable/disable pagination for the view collection'
advanced:
type: section
title: Advanced
underline: true
fields:
header.view.params:
type: frontmatter
label: 'Parameters'
validate:
type: ignore
29 changes: 29 additions & 0 deletions templates/modular/view.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% set view_settings = {} %}
{% set params = header.view.params|default({}) %}

{# Parse view_settings #}
{% if header.view %}
{% set view_settings = header.view %}
{% endif %}
{% if page.header.view %}
{% set view_settings = view_settings|merge(page.header.view) %}
{% endif %}
{% set view_settings = config.get('plugins.view')|merge(view_settings) %}

{# Get view #}
{% set view = page.find(header.view.page) %}

{% if view %}

{# Get filtered view #}
{% if view.header.content %}
{% set params = view.header.content|merge(params) %}
{% endif %}
{% set view = view.collection(params, view_settings.pagination) %}

{# Output template #}
{% if view_settings.template %}
{% include ((view_settings.template ~ '.html.twig')) with {'view': view, 'context': page} %}
{% endif %}

{% endif %}
67 changes: 67 additions & 0 deletions view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Grav\Plugin;

use \Grav\Common\Plugin;
use \Grav\Common\Grav;
use \Grav\Common\Page;
use \Symfony\Component\Yaml\Yaml as YamlParser;

class ViewPlugin extends Plugin
{

/**
* Assign listeners
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigPageVariables' => ['onTwigPageVariables', 0]
];
}

/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
$this->active = true;
return;
}

/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}

public function onGetPageTemplates($event)
{
$types = $event->types;
$locator = Grav::instance()['locator'];
$types->scanBlueprints($locator->findResources('plugin://' . $this->name . '/blueprints'));
$types->scanTemplates($locator->findResources('plugin://' . $this->name . '/templates'));
}

/**
* Set needed variables to display view.
*/
public function onTwigPageVariables($event)
{
$page = $event['page'];

// Parse frontmatter and return to vars.
if (isset($page->header()->view['params'])) {
$view = $page->header()->view;
$view['params'] = (array) YamlParser::parse($view['params']);
$page->modifyHeader('view', $view);
}
}
}
1 change: 1 addition & 0 deletions view.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enabled: true

0 comments on commit 9cdcae4

Please sign in to comment.