-
Notifications
You must be signed in to change notification settings - Fork 9
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
Data support #4
Conversation
- 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'))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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);
}
There was a problem hiding this comment.
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?
@raeldc I have re-tested this and cannot find anything wrong it's working as intended on my end. |
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.jsondata('http://myurl/company/team.json')
would load from this urlA 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()
methoddata('testimonials')->shuffle()
method. Methods works the same as shuffle.Slice
Data can be sliced by calling the
slice($offset, $length = NULL)
methoddata('testimonials')->slice(0, 3)
method. Methods works the same as array_sliceFilter
Data can be filtered by calling the
filter($key, $value = NULL)
methoddata('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:
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 upList
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.
Type
can be any data format: YAML, JSON, INI, PHP, and XML.Example with YAML:
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/folder
will fetch the complete data array and inject it, whiledata://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.