Create a dictionary using a multi-choice field
If you need to define a dictionary of data that may change in the future or you just want the flexibility of amending the data without opening the templates, you can choose a multi-choice field to store your data into. Here is how it works:
Let's assume you want to create a dictionary to store the product fee attributes and their labels from IPS. Without using a field, the common way is to define a variable as follows:
<:let ipsFeeDict = {'detail_est_fee_dollar': 'Establishment fee ($)', 'detail_trustee_fee_percent': 'Trustee fee', 'detail_other_fee_dollar':'Other fee', ...}:>
The problem with the above approach is that modifying the dictionary within the template necessitates opening the template, implementing the change, and re-testing the template. This process is not only time-consuming but also create the risk of introducing errors.
Using a multi-choice field
Navigate to fields definition and create a multi choice field. Start adding options so that:
1. the value of the option is a key of the dictionary
2. the text of the option is the value attached to the key
3. the "default" option is ticked
Your field options should look like this:
Next navigate to your template then create the dictionary as follows:
<:let ipsFeeDict = dict(zip($client.pf_ips_product_fee_list.value, $client.pf_ips_product_fee_list.text.splitlines())):>
Here, you create a dictionary that combines two lists. zip(fields, values) returns an iterator that generates 2-items tuples. If you call dict() on that iterator, then you’ll be building the dictionary you need. The elements of fields become the dictionary’s keys, and the elements of values represent the values in the dictionary.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article