-
Notifications
You must be signed in to change notification settings - Fork 7
Data
Any template can import data. Data is stored in the /data/ folder. It uses YAML to format each file. We have added some commonly used data sets to get you started but feel free to create as many different variations as you wish.
To grab a random entry from a file you can simply use the printData() function to reference the file. Here is a demo that grabs a random title from /data/blog-titles.yml. The first variable entered into printData is always the name of the file.
<?php printData('title'); ?>If you wish to retrieve a specific entry you can reference a number for that entry. For example if you wish to grab the second entry in /data/titles.yml you can do the following:
<?php printData('title', 1); ?>Note that entries start at 0.
Instead of referencing the a number value you can also grab entries using a variable name. These can be set in any file but /data/company.yml has examples of how to do this. To grab the site name you can do:
<?php printData('company', 'name'); ?>You can store multiple sets of data (or objects) in one file. This may be useful for storing similar data although we'd probably recommend using separate files so they're easier to use. If you have two objects inside a data file you can retrieve a random entry using the following.
<?php printData('blog', 'title'); ?>If you wish to retrieve a specific entry from that object we can pass a third parameter through the function like this:
<?php printData('blog', 'title', 1); ?>Note that this will retrieve the second entry inside title because numbers start from 0.
If you wish to manipulate data you can retrieve it using getData(). The same parameters as the above values still apply but will return it rather than print it.
Here is an example of returning a price and then manipulating it to give us a was, now and save value.
<?php for($i=0; $i<10; $i++) : ?>
Was: £<?php printData('price', $i); ?>
Now: £<?php echo getData('price', $i) * .8; ?>
Save: £<?php echo getData('price', $i) * .2; ?>
<?php endif; ?>