Skip to content

Commit

Permalink
Merge pull request #47 from stuartmaxwell/dev
Browse files Browse the repository at this point in the history
Add category_name template tag
  • Loading branch information
stuartmaxwell authored May 18, 2024
2 parents c246d80 + 13dbb10 commit 5f7e303
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion djpress/templates/djpress/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1>{% post_title %}</h1>
{% comment %}If we get posts for a category, then we can show the category header.{% endcomment %}
{% if category %}

<h1>{{ category.name }}</h1>
<h1>{% category_name %}</h1>

{% endif %}

Expand Down
17 changes: 17 additions & 0 deletions djpress/templatetags/djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,20 @@ def post_content(context: Context) -> str:
return ""

return mark_safe(post.content_markdown)


@register.simple_tag(takes_context=True)
def category_name(context: Context) -> str:
"""Return the name of a category.
Args:
context: The context.
Returns:
str: The name of the category.
"""
category: Category | None = context.get("category")
if not category:
return ""

return category.name
12 changes: 12 additions & 0 deletions djpress/tests/test_djpress_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,15 @@ def test_post_content(create_test_post):

output = f"<p>{create_test_post.content}</p>"
assert djpress_tags.post_content(context) == output


@pytest.mark.django_db
def test_category_name(category):
context = Context({"category": category})
assert djpress_tags.category_name(context) == category.name


def test_category_name_no_category():
context = Context({"foo": "bar"})
assert djpress_tags.category_name(context) == ""
assert type(djpress_tags.category_name(context)) == str
2 changes: 1 addition & 1 deletion templates/djpress/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

{% elif category %}

{{ category.name }} - {% get_blog_title %}
{% category_name %} - {% get_blog_title %}

{% elif author %}

Expand Down

0 comments on commit 5f7e303

Please sign in to comment.