sjelfull / craft3-beam

Generate CSVs and XLS files in your templates
https://superbig.co
MIT License
18 stars 6 forks source link

Example for dynamic output #7

Closed DarrylHardin closed 5 years ago

DarrylHardin commented 5 years ago

It would be helpful if there was an example that showed how to input values dynamically rather than static.

This is what I have

        {% set pastOrders = craft.orders.customer(craft.commerce.customers).isCompleted(true).all() %}

        {% set footwear = "" %}
        {% set userContent = [] %}
        {% for order in pastOrders %}
            {% for item in order.lineItems %}{% set footwear = item.description %}{% endfor %}
            {% set userContent = userContent|merge([[order.customer.user, footwear]]) %}
        {% endfor %}
        {% spaceless %}
        {% set options = {
            header: ['User', 'Footwear Purchased'],
            rows: [userContent]
        } %}
        {{ craft.beam.csv(options) }}
        {% endspaceless %}

However it returns array to string conversion error. If I change the [[order.customer.user, footwear]] to [order.customer.user, footwear] it works, but puts everything on multiple columns and a single row.

DarrylHardin commented 5 years ago

Okay, I finally figured it out for me. Feel free to use if you'd like. I think it would be nice to have this or something similar to it if there is a better way to go about it.

{# Get past orders #}
{% set pastOrders = craft.orders.customer(craft.commerce.customers).isCompleted(true).all() %}
{# set userRow array #}
{% set userRow = [[]] %}
{# loops through past orders and sets up to output correctly #}
{% for order in pastOrders %}
          {% set userRow = userRow|merge([[order.customer.user.email, order.customer.user ]]) %}
{% endfor %}
{% spaceless %}
        {% set options = {
           header: ['Email', 'Name'],
            rows: userRow
        } %}

      {{ craft.beam.csv(options) }}
{% endspaceless %}
sjelfull commented 5 years ago

I agree, as this is something that trips up people a lot. It's awkward to work with arrays and appending data in Twig.

There is plans to make this easier with Beam, but I don't have time to execute on them yet. Track #3 for updates.

Thanks for letting me know, though.

sjelfull commented 5 years ago

With 2.1 and the new append() method, this should be much easier.

Please check it out, and let me know if anything could be improved even further.

mark-chief commented 1 year ago

@sjelfull Could you please help me out? I am trying to dynamically create a CSV and just cant get it to work / understand the docs.. here's my example below.. Thanks for your direction

{% set entries = craft.entries()
    .section('bulkOrders')
    .all() %}

{# Display the list #}
{% for entry in entries %}
    {% do beam.append([entry.title, entry.firstName]) %}
{% endfor %}

{% set options = {
    header: ['Title', 'First Name']
} %}

{% set beam = craft.beam.create(options) %}

{% do beam.csv() %}
sjelfull commented 1 year ago

Hi, looks like you are setting the beam variable after you are using it in the loop?

What happens if you move it before the loop? What kind of errors are you seeing?

mark-chief commented 1 year ago

Hi, looks like you are setting the beam variable after you are using it in the loop?

What happens if you move it before the loop? What kind of errors are you seeing?

Thanks, That fixed it! Doh!

Is it possible to loop through matrix blocks in the append method?

entry.title, entry.firstName, entry.block1.title, entry.block2.title and so on?

sjelfull commented 1 year ago

Sure, you can do that. It will just append a new row to the sheet.