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

Data support #4

Merged
merged 6 commits into from
Apr 15, 2018
Merged

Data support #4

merged 6 commits into from
Apr 15, 2018

Conversation

johanjanssens
Copy link
Member

@johanjanssens johanjanssens commented Mar 29, 2018

Note: This PR required joomlatools/joomlatools-framework#151 for testing

Folder

By default data files are loaded from is /joomlatools-pages/data, this folder is configurable, data can also be loaded from any url.

Formats

Implementation offers support for YAML, JSON, INI, PHP, and XML and Markdown. Other data formats could be added if needed.

Method

There is a new data() template function available which receives a relative path or url to the data file that needs to be loaded.

Example:

  • data('company/team') would load from /joomlatools-files/data/company/team.json
  • data('http://myurl/company/team.json') would load from this url

A file type is NOT required when loading from a url, content negotiation is handled transparently.

The method returns a ComPagesDataObject which can be iterated in the template.

Methods

Shuffle

Data can be shuffled by calling the shuffle() method data('testimonials')->shuffle() method. Methods works the same as shuffle.

Slice

Data can be sliced by calling the slice($offset, $length = NULL) method data('testimonials')->slice(0, 3) method. Methods works the same as array_slice

Filter

Data can be filtered by calling the filter($key, $value = NULL) method data('testimonials')->filter('image') method. If only a key is specified all items will be returned for which the key exists and is not NULL if a value is set the key will be matched against the value.

Flatten

First level of a multi-dimensional data object can be flattened by calling flatten($key_as_property = null)

If defined the' key_as_property method parameter will be added to each object being flattened and contain the key of the object in the array if it a named object.

Example:

foreach($list->flatten('product') as $item) {
    echo $item->product;
}

Advanced

Folders

Inspired by Jekhyll you can not only load data from a single file but also data from all files in a folder.

Example: data('company/members') if the /joomlatools-files/data/company/members/ exists, then all the files in the folder will be loaded and the data merged into one large data object, as an added bonus; this even works if the data types are different so you can mix them up

List

You can not only load data from a list of locations, either a file or a folder.

Example: data(['company/members', company/vendors']) all the files and folders will be loaded annd appended into one big data object; this even works if the data types are different so you can mix them up.

Aliases

Inspired by Jekhyll Get you can also alias an url. We use a by convention approach, instead of using a config file, you just put the url as the first line of the data file, the data in the url will then be loaded.

Example: in file /joomlatools-files/data/company/team.json on first line add http://myurl/company/team.json

Use with: data('company/team')

This also works when loading from folder, if a file in the folder contains a url instead of data, the data will be loaded from that url and merged.

Ordering

Data can be ordered per folder by adding a .order.[format] file to the folder that specifies the specific ordering of all the data files, both files and directories can be ordered.Typecan be any data format: YAML, JSON, INI, PHP, and XML.

Example with YAML:

- file1.md
- fileB.md
- folderA
- folderB

Frontmatter

It's possible to inject data dynamically in the page frontmatter. To do that use the following:

  • data://path/to/folder
  • data://path/to/folder[property]

data://path/to/folderwill fetch the complete data array and inject it, while data://path/to/folder[property] will inject a specific property.

Note: This only works for page frontmatter. It doesn't work when using markdown data files that contain frontmatter.

Notes

  • Data objects are cached per request to increase performance, this also means that the data in the object can be changed in request scope.

  • IIf the format is markdown the contents of the file will be available as $data->content.

@johanjanssens johanjanssens added this to the 0.1 milestone Mar 29, 2018
@johanjanssens johanjanssens self-assigned this Mar 29, 2018
- Allow to pass a 'format' parameter to the data() method incase the url format cannot be determined
- Support url passthrough when loading data from a folder
- Pass Joomlatools/Pages/[version] user-agent when loading data from url
- Improve error handling when loading from url
$result = array();
foreach((array) $path as $file)
{
$url = trim(fgets(fopen($file, 'r')));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johanjanssens

Found a bug here or I'm just not sure what you're trying to achieve. This line opens up the file, and reads the first line. The first line is then parsed on line 66 as a URL. This causes Cannot get content from url error.

The following lines of code works:

            // Removed line 65.
            if(parse_url($file, PHP_URL_SCHEME)) {
                $data = $this->fromUrl($file, pathinfo($file, PATHINFO_EXTENSION));
            } else {
                $data = $this->getObject('object.config.factory')->fromFile($file, false);
            }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raeldc This is by design it implements an alias feature as explained in the description. This works fine on my end, how are you testing it?

@johanjanssens
Copy link
Member Author

@raeldc I have re-tested this and cannot find anything wrong it's working as intended on my end.

@johanjanssens johanjanssens merged commit 3d07800 into master Apr 15, 2018
@johanjanssens johanjanssens deleted the feature/3-data branch April 15, 2018 22:25
@raeldc raeldc mentioned this pull request Apr 24, 2018
62 tasks
@johanjanssens johanjanssens mentioned this pull request Aug 3, 2018
@johanjanssens johanjanssens mentioned this pull request Sep 5, 2018
This was referenced Mar 25, 2019
johanjanssens added a commit that referenced this pull request Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants