superbig / craft3-beam
Generate CSVs and XLS files in your templates
Installs: 30 726
Dependents: 0
Suggesters: 0
Security: 0
Stars: 18
Watchers: 2
Forks: 6
Open Issues: 4
Type:craft-plugin
Requires
- craftcms/cms: ^5.0.0
- league/csv: ^9.0
- mk-j/php_xlsxwriter: ^0.39.0
Requires (Dev)
- craftcms/ecs: dev-main
- craftcms/phpstan: dev-main
- craftcms/rector: dev-main
This package is auto-updated.
Last update: 2024-11-14 10:29:43 UTC
README
Generate CSVs and XLS files in your templates
Requirements
This plugin requires Craft CMS 3.0.0-beta.23 or later.
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project
-
Then tell Composer to load the plugin:
composer require superbig/craft3-beam
-
In the Control Panel, go to Settings → Plugins and click the “Install” button for Beam.
Using Beam
The starting point when working with Beam is to create a instance:
{% 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.
If you want to append content dynamically, say from a loop, you can use the append
method:
{% 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:
{% do beam.csv() %}
To generate an XLSX:
{% do beam.xlsx() %}
Changing config on the fly
To set the header of the file (the first row):
{% do beam.setHeader([ 'Username', 'Name', 'Email' ]) %}
To set the filename:
{% set currentDate = now|date('Y-m-d') %} {% do beam.setFilename("report-#{currentDate}") %}
To overwrite the content:
{% do beam.setContent([ [ 'test@example.com', 'John Doe' ], [ 'another+test@example.com', 'Jane Doe' ], [ 'third+test@example.com', 'Trond Johansen' ], ]) %}
Custom cell formatting is supported for XLSX:
{% set options = { header: ['Email', 'Name', { text: 'Number', type: 'number' }, { text: 'Date', type: 'date' }], content: [ [ 'test@example.com', 'John Doe', 100000, '2022-06-10'], [ 'another+test@example.com', 'Jane Doe', 252323, '2022-06-22'], [ 'third+test@example.com', 'Trond Johansen', 30, '2022-06-22'], [ 'third+test@example.com', 'Trond Johansen', 6233, '2023-06-22'], ] } %} {% set beam = craft.beam.create(options) %} {% do beam.xlsx() %}
These types are supported:
Brought to you by Superbig