3

I hope this makes sense... I am building a crypto asset list page (easy); however, in the {% for %} loop I would like to include a variable inside a variable. Showing the code will make more sense:

Tempalte.html

{% for crypto_asset in objects__list_cryptoAssets %}
 <tr role="row" class="body-row">
 <td role="cell">{{ api_external_prices.bitcoin.usd }}</td>
 </tr>
{% endfor %}

So the {% for %} loop grabs all the crypto assets and then I can use Django template {{ asset_class.slug }} to grab all slugs... nothing exceptional here. This variable {{ api_external_prices.bitcoin.usd }} grabs external USD prices for Bitcoin, {{ api_external_prices.bitcoin.eur }} prices in EUR, and so forth... nothing exceptional here either.

Here is where the question comes :: the idea would be to have something like {{ api_external_prices.{{ asset_class.slug }}.usd }}... so each crypto would have its own price FX fetched correctly. Is it possible to have a variable inside a variable?

asked Jun 11, 2022 at 16:19

1 Answer 1

1

They are several ways you can implement this:

Template filters

You can create a template filter api_external_prices that takes asset_class and the crypto type as parameters and returns the value.

The syntax would be something link this, where api_external_prices is the name of the template filter:

{{ asset_class|api_external_prices:"usd" }}

See here for more info about this feature: https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/#writing-custom-template-filters

Methods

Another approach would be to have api_external_prices as method on your asset_class object, which returns an object that has a usd property. api_external_prices here can just be a wrapper that calls a central module/function, but this would make it much easier to use it in templates.

{{ asset_class.api_external_prices.usd }}

The first approach is similar to what you are asking, but personally I would prefer to use the 2nd approach, because it saves you from introducing as template filter.

answered Jun 11, 2022 at 17:41
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your answer. I would love to understand more about the implementation of the second method... I will put the remaining code to understand how you would tackle method 2
hmm. I thought all the data was coming from models, but now I see that you are fetching some of it in the view. That will make it difficult to apply any of the two solutions.
A third approach would be to create a data structure in the view that let's you access everything you need in the template. I would just try to map everything into a big list of nested dicts. e.g. the top dict representing the crypt assets and then each having a dict of prices by unit.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.