CSV Format
Read CSV data from local files, WordPres media files, local or remote databases or 3rd party API, and output it anywhere in WordPress with using shortcodes, in posts/pages, headers/footers, widgets or in Visual Composer. You can configure how CSV is parsed by specifying delimiter character, enclosur
Read CSV data from local files, WordPres media files, local or remote databases or 3rd party API, and output it anywhere in WordPress with using shortcodes, in posts/pages, headers/footers, widgets or in Visual Composer.
You can configure how CSV is parsed by specifying delimiter character, enclosure character and escape character.
This is a free add-on that adds CSV support (comma-separated values) to the Twig Anything WordPress plugin.
Tutorials below demonstrate 3 examples:
- Example 1: generate text, one line for each line from CSV
- Example 2: generate a table
- Example 3: use shortcodes to output CSV anywhere in WordPress
EXAMPLE 1: MULTILINE TEXT
Let us use a CSV file with a list of countries and their corresponding top-level domain names (TLD). Here is an extract from the file:
Belgium,Brussels,EUR,Euro,BE,.be Belize,Belmopan,BZD,Dollar,BZ,.bz Benin,Porto-Novo,XOF,Franc,BJ,.bj Bhutan,Thimphu,BTN,Ngultrum,BT,.bt
In our example, let’s display TLDs for all countries that start with “D”:
Denmark - .dk Djibouti - .dj Dominica - .dm Dominican Republic - .do
Columns are numbered starting from #0, so we will need the column #0 (country name) and #5 (top-level domain name).
After the CSV input has been parsed, the resulting lines are passed to your Twig Template as the data variable. You can then loop over it with using “for” syntax: :
{% for row in data if row.0|slice(0,1)|upper == 'D' %} {{ row.0 }} - <strong>{{row.5}}</strong><br/> {% endfor %} {% for row in data %}...{% endfor %} is the loop. The code inside the loop will be rendered once for every line from the CSV file.
Let us take a closer look at this piece: if row.0|slice(0,1)|upper == 'D'
First of all, row.0 is how we access column #0, a country name in our case. Next, |slice(0,1) is a Twig filter that takes the first character of the country name. The next filter is |upper and it simply uppercases the first letter returned by the previous filter. Finally, we only allow for countries that start with the “D” letter: == 'D' .
Inside the loop, we output a country name with using {{row.0}} and a TLD name with using {{row.5}} . Plus a very basic HTML markup: <br/> to insert a line break and <strong>...</strong> to make TLD name appear bold.