[WordPress] 外掛分享: CSV Format

首頁外掛目錄 › CSV Format
WordPress 外掛 CSV Format 的封面圖片
10+
安裝啟用
尚無評分
3871 天前
最後更新
問題解決
WordPress 3.6.1+ v1.1 上架:2015-07-27

內容簡介

這個 WordPress 外掛可以從本地檔案、WordPress 媒體檔案、本地或遠端的資料庫或第三方 API 讀取 CSV 資料,並使用簡碼在 WordPress 的任何地方輸出,例如在文章、頁面、標題/頁腳、小工具或 Visual Composer 中。

您可以設定如何解析 CSV,包括指定分隔字元、封套字元和跳脫字元。

這是一個免費的附加元件,可以將 CSV 支援(逗號分隔值)添加到 Twig Anything WordPress 外掛中。

下面的教程演示了三個範例:

範例 1:產生多行文字,每行來自 CSV
範例 2:產生一個表格
範例 3:使用簡碼來在 WordPress 的任何地方輸出 CSV

範例 1:多行文字

讓我們使用一個 CSV 檔案,列出各個國家和它們對應的頂級網域名稱(TLD)。

比利時,布魯塞爾,EUR,歐元,BE,.be
貝里斯,Belmopan,BZD,美元,BZ,.bz
貝南,波多諾伏,XOF,法郎,BJ,.bj
不丹,廷布, BTN,紐扣陀, BT,.bt

在範例中,讓我們顯示以“D”開頭的所有國家的 TLD:

丹麥 - .dk
吉布地 - .dj
多米尼加 - .dm
多明尼加共和國 - .do

列是從 #0 開始編號的,所以我們需要第 #0(國家名稱)和第 #5(頂級網域名稱)欄。

在 CSV 輸入解析後,將結果行作為數據變量傳遞到您的 Twig 模板。你可以使用for語法迴圈控制流程:

{% 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 %} 是迴圈,每一行 CSV 檔案將渲染一次。

讓我們來看看這段代碼:if row.0|slice(0,1)|upper == 'D'

首先,row.0 是我們訪問第 #0 欄,即國家名稱。接下來,|slice(0,1) 是一個 Twig 過濾器,它取得國家名稱的第一個字元。下一個過濾器是 |upper,它只是將前一個過濾器返回的第一個字元轉換為大寫。最後,我們只允許以“D”字母開頭的國家: == 'D'。

在迴圈內部,我們使用 {{row.0}} 顯示國家名稱,{{row.5}} 顯示 TLD 名稱,以及一個基本的HTML標記:<br/> 用於插入斷行,<strong>...</strong> 來使 TLD 名稱顯示粗體。

範例 2:表格

使用相同的 CSV 檔案,我們現在要在 HTML 標記內輸出數據,生成表格。

如果您對 HTML 不是很熟悉,我建議您查看以下教程,您將可以在十分鐘內學會!

教程 1
教程 2

您也可以使用以下表格生成器,只需將它們生成的 HTML 複製並粘貼到您的模板中即可。這些生成器還允許應用一些基本樣式,例如邊框、顏色、邊距、填充等。

生成器 1
生成器 2

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.1) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「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, 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 }} - {{row.5}}
{% 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:
to insert a line break and ... to make TLD name appear bold.
EXAMPLE 2: TABLE
Using the same CSV file, let’s now output our data in a table with using HTML markup.
If you are not very familiar with HTML, I recommend going through the following tutorials – you’ll learn in 10 minutes, I guarantee!

Tutorial 1
Tutorial 2

You can also use the following table generators and just copy-paste the HTML they generate right into your template. The generators also allow to apply some basic styling, like borders, colors, margins, paddings etc.

Generator 1
Generator 2

So, we will use the following HTML tags:

...

to make a table

...

to make a table header with column headings

...

to make a table body with all the rows from CSV

...

to make a table row

...

to make a cell in the header’s row

...

to make a cell in a regular rows in the table’s body

Here is our template:

{% for row in data if row.0|slice(0,1)|upper == 'D' %}

{% endfor %}

Country TLD
{{ row.0 }} {{ row.5 }}

Notice how we use the {% for row in data %} syntax to loop over CSV lines,
and then output a table row

...

inside the loop, so that
a new row is made for every CSV line.
Actually, you can use just any HTML and stylize your table the way you need.
Just don’t forget to put {{row.0}}, {{row.1}} etc where you want
your csv values in the cells

...

.
EXAMPLE 3: SHORTCODES
While you prepare your template, you usually preview it many times by clicking
the “Preview Changes” button. Once you are happy with the preview, you will
want to embed it somewhere – in a post or a page, footer / header,
in a widget or as a Visual Composer block.
To embed your template, you will use a shortcode:
[twig-anything slug="my-template-slug"]

In WordPress, every Twig Template has its own slug, just like posts and pages.
It can be found under the template title.
Alternatively, you can use the template ID:
[twig-anything id="123"]

文章
Filter
Apply Filters
Mastodon