Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Learn by Example" addition #340

Merged
merged 1 commit into from Oct 31, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions pages/10.cookbook/03.plugin-recipes/docs.md
Expand Up @@ -11,6 +11,7 @@ This page contains an assortment of problems and their respective solutions rela
3. [Adding a search button to the SimpleSearch plugin](#adding-a-search-button-to-the-simplesearch-plugin)
4. [Learning by Example](#learning-by-example)
* [How do I read from and write data to the file system?](#how-do-i-read-from-and-write-data-to-the-file-system)
* [How do I make data from a plugin available to Twig?](#how-do-i-make-data-from-a-plugin-available-to-twig)
* [How do I inject Markdown into a page?](#how-do-i-inject-markdown-into-a-page)
* [How do I inject HTML into the final output?](#how-do-i-inject-html-into-the-final-output)
* [How do I inject assets like JavaScript and CSS files?](#how-do-i-inject-assets-like-javascript-and-css-files)
Expand Down Expand Up @@ -278,6 +279,8 @@ Before you proceed, be sure you've familiarized yourself with [the core document

Grav might be flat file, but flat file ≠ static! There are numerous ways read and write data to the file system.

* If you just need read access to YAML data, check out the [Import plugin](https://github.com/Deester4x4jr/grav-plugin-import).

* The preferred interface is via the built-in [RocketTheme\Toolbox\File](https://learn.getgrav.org/api/RocketTheme/Toolbox/File.html) interface.

* There's nothing stopping you from using [SQLite](https://sqlite.org/) either.
Expand All @@ -292,6 +295,21 @@ Grav might be flat file, but flat file ≠ static! There are numerous ways r

* [Webmention](https://github.com/Perlkonig/grav-plugin-webmention)

#### How do I make data from a plugin available to Twig?

One way is via the `config.plugins.X` namespace. Simply do a `$this->config->set()` as seen in the following examples:

* [ipLocate](https://github.com/Perlkonig/grav-plugin-iplocate/blob/master/iplocate.php#L82)
* [Count Views](https://github.com/Perlkonig/grav-plugin-count-views/blob/master/count-views.php#L88)

You can then access that in a Twig template via `{{ config.plugins.X.whatever.variable }}`.

Alternatively, you can pass variables via `grav['twig']`:

* [Blogroll](https://github.com/Perlkonig/grav-plugin-blogroll/blob/master/blogroll.php#L43), which you can then access directly [in your template](https://github.com/Perlkonig/grav-plugin-blogroll/blob/master/templates/partials/blogroll.html.twig#L32).

Finally, you can inject data directly into the page header, as seen in [the Import plugin](https://github.com/Deester4x4jr/grav-plugin-import).

#### How do I inject Markdown into a page?

According to the [Grav Lifecycle](https://learn.getgrav.org/plugins/grav-lifecycle), the latest event hook where you can inject raw Markdown is `onPageContentRaw`. The earliest is probably `onPageInitialized`. You can just grab `$this->grav['page']->rawMarkdown()`, munge it, and then write it back out with `$this->grav['page']->setRawContent()`. The following plugins demonstrate this:
Expand Down