• No se han encontrado resultados

Capacidad calórica a presión constante (Cp)

7. Modelo de transferencia de calor en la litósfera

7.1 Propiedades termales de las rocas

7.1.2 Capacidad calórica a presión constante (Cp)

Django provides you with a lot of built-in templatetagslike the{% static %}tag that can make dealing with dynamic data easier. Several of them you are already familiar with:

• {% static %}provides a way to reference the location of your static folder without hardcoding it in.

• {% extend 'base.html'%}allows for the extending of a parent template.

• {% csrf %}provides protection against Cross Site Request Forgeries.

Django also provides a number of built in filters which make working with data in your tem-plates easier. Some examples include:

• {{ value | capfirst }}- if value = “jack” this will produce “Jack”

• {{ value | add:"10"}}- if value is “2” this will produce “12”

• {{ value | default:"Not logged in"}} - if value evaluates toFalse, use the given default - i.e.,"Not logged in"

There are tons more built-in filters and template tags. Again, check out the Django documen-tationfor more information.

Custom template tags

Can’t find what your looking for in the docs? You can also create your own custom template tags. This is great if you have certain content/html structures that you’re repeating over and over.

For example, this section of

code-1 <div class="col-lg-4">

2 <img class="img-circle" src="{% static 'img/yoda.jpg' %}"

width="140" height="140" alt="Generic placeholder image">

3 <h2>Hone your Jedi Skills</h2>

4 <p>All members have access to our unique training and

achievements ladders. Progress through the levels and show everyone who the top Jedi Master is! </p>

5 <p><a class="btn btn-default" href="#" role="button">View details

&raquo;</a></p>

-could be easily re-factored into a template tag so you wouldn’t have to type so much. Let’s do it.

Directory structure

First, create a directory in yourmainapp called “templatetags” and add the following files to ti:

Inmarketing.pyadd the following code to define the custom template tag:

1 from django import template

2 register = template.Library()

The first two lines just registercircle_header_item()as a template tag so you can use it in a template using the following syntax:

1 {% circle_header_item img_name='img.jpg' heading='nice image' %}

Just as if you were calling a regular Python function, when calling the template tag you can usekeywords or positional arguments- but not both. Arguments not specified will take on the default value.

The@register function simply creates a context for the template to use. It does this by creating a dictionary of variable names mapped to values. Each of the variable names in the dictionary will become available as template variables.

The remainder of the

line-1 @register.inclusion_tag('circle_item.html')

-declares an HTML fragment that is rendered by the template tag. Django will look for the HTML file everywhere that is specified in theTEMPLATE_LOADERSlist, which is specified in thesettings.pyfile. In our case, this is under thetemplatesubdirectory.

HTML Fragment

Thecircle_item.htmlfile looks like this:

1 {% load staticfiles %}

2

3 <div class="col-lg-4">

4 <img class="img-circle" src="{% static 'img/'|add:img %}"

width="140" height="140" alt="{{img}}">

5 <h2>{{ heading }}</h2>

6 <p>{{ caption }}</p>

7 <p><a class="btn btn-default" href="{% url button_link %}"

role="button">{{button_title}}</a></p>

8 </div>

Add this to the “templates” directory.

We took our original HTML to define the structure and then used several template variables (the ones we returned from main.templatetags.marketing.circle_header_item), which enables us to dynamically populate the structure with data. This way we can repeat the structure several times without having to repeat the HTML. Everything is pretty standard here, but there is one template tag/filter you may not be familiar with:

1 src="{% static 'img/'|add:img %}

Looking at thesrcattribute you can see it is using the standard{% static %}tag. However there is a funny looking bit -'img/'|add:img- which is used for concatenation.

1 {{ num|add:'1' }}

If you passed5in fornum, then this would render6in your HTML.

addcan also concatenate two lists as well as strings (like in our case). It’s a pretty handy filter to have.

Adding the tag

In terms of our inclusion tag, we call it directly fromindex.html:

1 <section class="container marketing">

2 <!-- Three columns of text below the carousel -->

3 <div class="row center-text">

4 {% circle_header_item img_name='yoda.jpg' heading='Hone your Jedi Skills'

5 caption='All members have access to our unique training and achievements ladders.

6 Progress through the levels and show everyone who the top Jedi Master is!' %}

7

8 {% circle_header_item img_name='clone_army.jp' heading='Build your Clan'

9 caption='Engage in meaningful conversation, or bloodthirsty battle! If it's

10 related to Star Wars, in any way, you better believe we do it.

:)' %}

11

12 {% circle_header_item img_name="leia.jpg" heading="Find Love"

13 caption="Everybody knows Star Wars fans are the best mates for Star Wars

14 fans. Find your Princess Leia or Han Solo and explore the stars

15 together." button_title="Sign Up Now" %}

16 </div>

17 </section>

NOTE: Django template tags are not allowed to span multiple lines, so you actu-ally have to string everything together in one super long line. We broke it apart for readability in this course. Be sure to turn word-wrap on in your text editor so you can view all of the text without having to scroll.