Giter Club home page Giter Club logo

grav-plugin-import's Introduction

Grav Import Plugin

This plugin allows you to import YAML and JSON files into the header of your pages, facilitating custom actions/settings.

There are no dependencies

Installation

Installing the Import plugin can be done in one of two ways. Using the GPM (Grav Package Manager), manual installation via a zip file.

GPM Installation (Preferred)

The simplest way to install this plugin is via the Grav Package Manager (GPM) through your system's Terminal (also called the command line). From the root of your Grav install type:

$ bin/gpm install import

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

Manual Installation

To manually 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 import. You can find these files either on GitHub or via GetGrav.org.

You should now have all the plugin files under

/your/site/grav/user/plugins/import

Configuration

There are currently no settings to configure for this plugin. In the future, user settings may become available. If so, import will allow you to use the Grav Admin Plugin to adjust these settings as needed.

Usage

  • Add an imports field to your page header. You can include a a single file:

    imports: 'file1.yaml'

    Or you can add multiple files:

    imports:
      - 'file1.yaml'
      - 'data:file2.json'

    When multiple files are requested, the first part of the given string (without the extension and without any leading data: part) becomes the key.

  • File names must end in either .yaml or .json, otherwise they are ignored.

  • By default, the plugin looks for files in the same folder as the page itself. But you can prefix filenames with data: to anchor your search in the user/data folder.

  • The imported data becomes a part of page.header.imports, which can be accessed via Twig.

The contents of these files is up to you, and you will need to determine how you are going to use them in your code. The goal is to make the data in the YAML files available to themes and plugins to expand the functionality of Grav in an open-ended fashion.

Examples

Single file

imports: 'file.yaml'

The plugin would look for file.yaml in the same folder as the page file. The data would be available via Twig as {{ page.header.imports.X }}.

Multiple files

imports:
    - 'file1.yaml'
    - 'data:scratch/file2.json'

The plugin would look for file1.yaml in the same folder as the page file. It would look for file2.json in user/data/scratch. The data would available as {{ page.header.imports.file1.X }} and {{ page.header.imports['scratch/file2'] }}.

Caveats

While the plugin attempts to sanitize file names (eliminating double periods, stripping leading slashes, etc.), it cannot catch all situations. In a multiuser environment, plugins like this one could potentially make it possible for a user to read data from paths you do not expect. Do not install this plugin in such environments without taking proper precautions. Pull requests strengthening the sanitize feature are warmly welcomed.

Updating

As development for the Import plugin 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 Import 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). 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 import

This command will check your Grav install to see if your GitHub 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 Import is pretty simple. Here is what you will need to do to get this done:

  • Delete the your/site/user/plugins/import directory.
  • Download the new version of the Import plugin from either GitHub or GetGrav.org.
  • Unzip the zip file in your/site/user/plugins and rename the resulting folder to import.
  • 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.

Attribution

Special thanks to the Grav team and @rhukster for giving me a starting point and direction for this plugin.

grav-plugin-import's People

Contributors

deester4x4jr avatar perlkonig avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

grav-plugin-import's Issues

Don't seem able to access values

I know you said we have to implement ourselves, but I have a problem.

{{ dump(page.header.import) }} works fine and tells me I have a nested array with three top-level elements. But absolutely everything I have tried to access the first element has failed.

{{ dump(page.header.import[0]) }} gives null

I tried to get back to basics.

{% set myvar = ['foo','bar','crap'] %}
{% set newvar = ( myvar|slice(0, 1)) %}

Works exactly as expected. But {% set newvar = ( page.header.import|slice(0, 1)) %} results in a blank, not null, when I {{ dump(newvar) }}

I really want to be able to access that array that is the top element, but I just cannot see how. Any help gratefully received.

How to implement correctly

I think I am missing something in how to implement this.

I have added a file named ex_config.json to the folder for a page.

For the docs.md file in that folder I have added imports: ex_config.json to the frontmatter and then used {{ page.header.imports.ex_config.json }} in the main body of the markdown.

This is not working and the page only renders {{ page.header.imports.ex_config.json }}

Import doesn't always inject the data the same way

If we import a single file that is entered as:

imports: file.yaml

the plugin injects the data extracted directly into the imports object. If we import an array of files as:

imports:
    - file1.yaml
    - file2.yaml

the plugin will inject each file's data into corresponding named arrays under the imports object. This is inconsistent and we should be using the latter as the preferred method, to make it easier to consistently recall the data.

Request for fork

hey @Deester4x4jr, a request for Fork has been submitted to our Repo: getgrav/grav#1191

This basically asks for the GPM version to be pointed to the forked version. Are you continuing to maintain and support this plugin?

Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.