Beam plugin for Craft CMS 3.x
Generate CSVs and XLS files in your templates, (*1)
, (*2)
Requirements
This plugin requires Craft CMS 3.0.0-beta.23 or later., (*3)
Installation
To install the plugin, follow these instructions., (*4)
-
Open your terminal and go to your Craft project:, (*5)
cd /path/to/project
-
Then tell Composer to load the plugin:, (*6)
composer require superbig/craft3-beam
-
In the Control Panel, go to Settings → Plugins and click the “Install” button for Beam., (*7)
Using Beam
The starting point when working with Beam is to create a instance:, (*8)
{% set options = {
header: ['Email', 'Name'],
content: [
[ 'test@example.com', 'John Doe' ],
[ 'another+test@example.com', 'Jane Doe' ],
[ 'third+test@example.com', 'Trond Johansen' ],
]
} %}
{% set beam = craft.beam.create(options) %}
This will return a BeamModel
behind the scenes., (*9)
If you want to append content dynamically, say from a loop, you can use the append
method:, (*10)
{% set myUserQuery = craft.users()
.group('authors') %}
{# Fetch the users #}
{% set users = myUserQuery.all() %}
{# Display the list #}
{% for user in users %}
{% do beam.append([user.username, user.name, user.email]) %}
{% endfor %}
To generate an CSV:, (*11)
{% do beam.csv() %}
To generate an XLSX:, (*12)
{% do beam.xlsx() %}
Changing config on the fly
To set the header of the file (the first row):, (*13)
{% do beam.setHeader([ 'Username', 'Name', 'Email' ]) %}
To set the filename:, (*14)
{% set currentDate = now|date('Y-m-d') %}
{% do beam.setFilename('report-#{currentDate}') %}
To overwrite the content:, (*15)
{% do beam.setContent([
[ 'test@example.com', 'John Doe' ],
[ 'another+test@example.com', 'Jane Doe' ],
[ 'third+test@example.com', 'Trond Johansen' ],
]) %}
Brought to you by Superbig, (*16)