Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use block dataKey instead of block key for define for variable name #279

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

MaxMorais
Copy link

@MaxMorais MaxMorais commented Dec 30, 2024

To construct a UI like below

image

We often need to use repeater block, internally the repeater block define a key based on the blockKey, what create codes like

{%- if not key_u7ps3awh6.price.unitary -%}
  {%- if key_u7ps3awh6.price.monthly -%}
   <h4>R$ {{ key_u7ps3awh6.price.monthly.real }}<sup style="font-weight: 200;">{{ key_u7ps3awh6.price.monthly.dec }}</sup>/<sub style="font-weight: 200;">mês</sub></h4>
  {%- endif -%}
  
  {%- if key_u7ps3awh6.price.yearly -%}
   <h4>{%- if key_u7ps3awh6.price.monthly %} - {%- endif -%}R$ {{ key_u7ps3awh6.price.yearly.real }}<sup style="font-weight: 200;">{{ key_u7ps3awh6.price.yearly.dec }}</sup>/<sub style="font-weight: 200;">ano</sub></h4>
  {%- endif -%}
  
{%- else -%}
<h4>R$ {{ key_u7ps3awh6.price.unitary.real }}<sup style="font-weight: 200;">{{ key_u7ps3awh6.price.unitary.dec }}</sup>/<sub style="font-weight: 200;">und</sub></h4>
{%- endif -%}

This code works because internally, the builder defined a variable key_{block.get('blockId')}, but it makes impractical to deal with that in the long term!

Reasons are:

1 - We dont have any reference on the UI that tell us that my block have the id u7ps3awh6
2 - The block ID is internally controlled by the framework, what means, if we duplicate the block, the logic breaks and again we dont have hints about the new variable name!

My proposal is to make use of the dataKey of the repeater block, to generate a more organic key like below

image

{%- if not key_products.price.unitary -%}
  {%- if key_products.price.monthly -%}
   <h4>R$ {{ key_products.price.monthly.real }}<sup style="font-weight: 200;">{{ key_products.price.monthly.dec }}</sup>/<sub style="font-weight: 200;">mês</sub></h4>
  {%- endif -%}
  
  {%- if key_products.price.yearly -%}
   <h4>{%- if key_products.price.monthly %} - {%- endif -%}R$ {{ key_products.price.yearly.real }}<sup style="font-weight: 200;">{{ key_products.price.yearly.dec }}</sup>/<sub style="font-weight: 200;">ano</sub></h4>
  {%- endif -%}
  
{%- else -%}
<h4>R$ {{ key_products.price.unitary.real }}<sup style="font-weight: 200;">{{ key_products.price.unitary.dec }}</sup>/<sub style="font-weight: 200;">und</sub></h4>
{%- endif -%}

To construct a UI like below

![image](https://github.com/user-attachments/assets/2ddc5046-6c1f-42c3-8e2b-9eae0b1b3b86)

We often need to use `repeater` block, internally the repeater block define a key based on the blockKey, what create codes like

```jinja
{%- if not key_u7ps3awh6.price.unitary -%}
  {%- if key_u7ps3awh6.price.monthly -%}
   <h4>R$ {{ key_u7ps3awh6.price.monthly.real }}<sup style="font-weight: 200;">{{ key_u7ps3awh6.price.monthly.dec }}</sup>/<sub style="font-weight: 200;">mês</sub></h4>
  {%- endif -%}
  
  {%- if key_u7ps3awh6.price.yearly -%}
   <h4>{%- if key_u7ps3awh6.price.monthly %} - {%- endif -%}R$ {{ key_u7ps3awh6.price.yearly.real }}<sup style="font-weight: 200;">{{ key_u7ps3awh6.price.yearly.dec }}</sup>/<sub style="font-weight: 200;">ano</sub></h4>
  {%- endif -%}
  
{%- else -%}
<h4>R$ {{ key_u7ps3awh6.price.unitary.real }}<sup style="font-weight: 200;">{{ key_u7ps3awh6.price.unitary.dec }}</sup>/<sub style="font-weight: 200;">und</sub></h4>
{%- endif -%}
```

This code works because internally, the builder defined a variable `key_{block.get('blockId')}`, but it makes impractical to deal with that in the long term!

Reasons are:

 1 - We dont have any reference on the UI that tell me that my block have the id `u7ps3awh6`
 2 - The block ID is internally controlled by the framework, what means, if we duplicate the block, the logic breaks and again we dont have hints about the new variable name!
 
 
 My proposal is to make use of the `dataKey` of the `repeater` block, to generate a more organic key like below
 
 
![image](https://github.com/user-attachments/assets/c8b03707-0127-48b8-bcc2-d32adff79811)

```jinja
{%- if not key_products.price.unitary -%}
  {%- if key_products.price.monthly -%}
   <h4>R$ {{ key_products.price.monthly.real }}<sup style="font-weight: 200;">{{ key_products.price.monthly.dec }}</sup>/<sub style="font-weight: 200;">mês</sub></h4>
  {%- endif -%}
  
  {%- if key_products.price.yearly -%}
   <h4>{%- if key_products.price.monthly %} - {%- endif -%}R$ {{ key_products.price.yearly.real }}<sup style="font-weight: 200;">{{ key_products.price.yearly.dec }}</sup>/<sub style="font-weight: 200;">ano</sub></h4>
  {%- endif -%}
  
{%- else -%}
<h4>R$ {{ key_products.price.unitary.real }}<sup style="font-weight: 200;">{{ key_products.price.unitary.dec }}</sup>/<sub style="font-weight: 200;">und</sub></h4>
{%- endif -%}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant