ng150716
ng150716

Reputation: 2244

Get Content of Template Block to Use in IF Statement

Currently I have a block setup for my page titles in my base.html. It takes a unique value from each template page, and adds the website name to the end. For example: ABOUT US - My Site Name. The "ABOUT US" is passed by template and " - My Site Name" appears on every page regardless.

However, I want to use the same base.html on my index page, but only show "My Site Name." I was hoping to use an if statement by parsing the block each template passes to look for a unique value, signifying I am on the index page.

So far my code looks like this:

<title>
    {% if {% block title %}{% endblock %} == "index_pg" %}
        My Site Name
    {% else %}
        {% block title %}{% endblock %} - My Site Name
    {% endif %}
</title>

obviously this doesn't work. Anyone ideas on how I can accomplish this? Thanks.

Upvotes: 1

Views: 41

Answers (1)

YellowShark
YellowShark

Reputation: 2269

Typically I would use two blocks, one wrapped around the other, something like this:

# base.html template
<title>
  {% block title %}
    {% block inner_title %}{% endblock inner_title %} - The Stock Column
  {% endblock title %}
</title>

So for most of your pages, you would extend base.html, and do this:

{% block inner_title %}Page XYZ{% endblock inner_title %}

And then on your index page, you would also extend base.html, but you would then do this:

{% block title %}TEST{% endblock title %}

The resulting output from the first one would be:

<title>Page XYZ - The Stock Column</title>

And the output of the second one, from your index page, would be:

<title>TEST</title>

Upvotes: 1

Related Questions