# Template Tags DJ Press provides a rich set of template tags to help you build your blog templates. These tags allow you to retrieve and display data from your blog, create navigation menus, format dates, and more. ## Loading Template Tags To use any of the following tags, you must load the `djpress_tags` in your template file: ```django {% load djpress_tags %} ``` ## Tag Categories The template tags in DJ Press are organised into several functional categories: 1. **Data Access Tags** - Tags that start with `get_` retrieve data from the database without any HTML formatting 2. **Display Tags** - Format and display data with appropriate HTML (e.g., `site_title_link`, `blog_categories`) 3. **Post Content Tags** - Handle rendering post content, titles, dates, etc. (e.g., `post_title`, `post_content`) 4. **Navigation Tags** - Generate navigation elements like menus (e.g., `site_pages_list`, `blog_categories`) 5. **Utility Tags** - Additional helper tags for common operations ## Table of Contents - [Data Access Tags](#data-access-tags) - [Display Tags](#display-tags) - [Post Content Tags](#post-content-tags) - [Navigation Tags](#navigation-tags) - [Utility Tags](#utility-tags) ## Data Access Tags These tags retrieve data from your blog without adding any HTML formatting. They're useful when you want to access data but apply your own custom formatting. ### get_posts Returns all published posts as a queryset. Use this tag to access all published blog posts in your templates. **Returns:** queryset of all published posts. > **Related Topics:** See [url_structure.md](url_structure.md) for URL patterns and [themes.md](themes.md) for how to > use posts in your theme. #### get_posts Example This is useful for building an index page with all posts: ```django {% get_posts as all_posts %} {% for post in all_posts %} {% endfor %} ``` ### get_recent_posts Returns the most recent published posts. Tries to be efficient by checking if there's a `posts` object in the context that can be used. **Returns:** queryset of recent published posts. #### get_recent_posts Example ```django {% get_recent_posts as recent_posts %}

Recent Posts

``` ### get_pages Get all published pages as an iterable queryset. This can be useful if you want to build your own menu or list of pages on the blog. **Returns:** queryset of published pages that are sorted by the `menu_order` field, and then by the `title` field. #### get_pages Example Get the pages and display as a list: ```django {% get_pages as pages %} ``` Outputs the following: ```html ``` Also see [`site_pages`](#site_pages) for a tag that produces similar output to the above but with just a single template tag. ### get_categories Get all categories as an iterable queryset. This can be useful if you want to build your own menu or list of categories on the blog. Also see `blog_categories` for a tag that produces similar output to the below example, but with just a single template tag. **Returns:** queryset of categories that are sorted by the `menu_order` field, and then by the `title` field. #### get_categories Example Get the categories and display as a list: ```django {% get_categories as categories %} ``` Outputs the following: ```html ``` ### get_tags Returns all tags as a queryset. **Returns:** queryset of all tags. #### get_tags Example ```django {% get_tags as all_tags %}
{% for tag in all_tags %} {{ tag.title }} {% endfor %}
``` ### get_post_title Returns the title of a post from the current context. #### get_post_title Parameters - `include_empty` (boolean): Whether to include the title if it is empty, using the post_title property fallback. Default is false. **Returns:** string - the title of the post. #### get_post_title Example ```django {% get_post_title as title %} ``` ### get_post_url Returns the URL of a post from the current context. **Returns:** string - the URL of the post. #### get_post_url Example ```django {% get_post_url as post_url %} ``` ### get_post_author Returns the author display name from the current context. **Returns:** string - the author display name. #### get_post_author Example ```django {% get_post_author as author %} ``` ### get_post_date Returns the date of a post from the current context. **Returns:** string - the date of the post in the format "Mon DD, YYYY". #### get_post_date Example ```django {% get_post_date as post_date %} ``` ## Display Tags These tags retrieve data and format it with HTML, ready to be displayed in your templates. ### site_title Returns the site title as configured in the settings with the `SITE_TITLE` variable. If no site title has been configured, this will return the default title: "My DJ Press Blog". **Returns:** the `SITE_TITLE` value as a string. > **Related Topics:** See [configuration.md](configuration.md) for how to configure the site title and other settings. #### site_title Example This is useful for the HTML `title` tag: ```django {% site_title %} ``` ### site_title_link This is used to return an HTML link to the index view of the blog. **Returns:** an HTML link to the index view marked as safe. #### site_title_link Parameters - `link_class` (optional): CSS class(es) to apply to the link. #### site_title_link Examples Just the tag with no argument: ```django {% site_title_link %} ``` Outputs the following: ```html My DJ Press Blog ``` The tag with a positional argument: ```django {% site_title_link "mycssclass" %} ``` Outputs the following: ```html My DJ Press Blog ``` The tag with a keyword argument: ```django {% site_title_link link_class="mycssclass" %} ``` Outputs the following: ```html My DJ Press Blog ``` The `link_class` argument can contain multiple CSS tags separated by spaces: ```django {% site_title_link link_class="class1 class2" %} ``` Outputs the following: ```html My DJ Press Blog ``` ### page_title Returns the title of the page, depending on the context of the page. A different title is returned for a category page, a blog page, author page, etc. This is useful to be used in the `` tag of the template. **Returns:** A string with no HTML formatting. #### page_title Parameters - `pre_text` (optional): text to be displayed before the page title - `post_text` (optional): text to be displayed after the page title #### page_title Examples The tag with no options for a single blog page or post: ```django <title>{% page_title %} ``` Outputs the following: ```html My blog post title ``` The tag can be combined with the `site_title` tag as well as using the `post_text` field. ```django {% page_title post_text=" | " %}{% site_title %} ``` Outputs the following: ```html My blog post title | My DJ Press Blog ``` Or you can get creative with an emoji: ```django {% page_title pre_text="🚀 " post_text=" | " %}{% site_title %} ``` Outputs the following: ```html 🚀 My blog post title | My DJ Press Blog ``` Category pages will show the title of the category, and author pages will show the display name of the author. ## Navigation Tags These tags help you build navigation elements for your blog. ### blog_categories Get a list of all categories wrapped in HTML that can be configured with optional arguments. **Returns:** all categories as HTML which has been marked as safe. > **Related Topics:** See [url_structure.md](url_structure.md) for category URL patterns and [configuration.md](configuration.md) for category-related settings. #### blog_categories Parameters - `outer_tag` (optional): the outer tags that this should be wrapped in. Accepted options are "ul", "div", "span". Default is: "ul". - `outer_class` (optional): the CSS classes to apply to the outer tag. Default: "". - `link_class` (optional): the CSS classes to apply to the link tag. Default: "". #### blog_categories Examples Just the tag, with no arguments. ```django {% blog_categories %} ``` This will output: ```html ``` Wrapped in a `div` tag with classes added, using positional arguments. ```django {% blog_categories "div" "class1" "class2" %} ``` Outputs a comma-separated list of categories, wrapped in a `div` tag. ```html
General, News
``` Wrapped in a `span` tag with classes added, using named arguments. ```django {% blog_categories outer_tag="span" outer_class="class1" link_class="class2" %} ``` Outputs the same comma-separated list of categories, but wrapped in a `span` tag. ```html General, News ``` ### blog_tags Get a list of all tags wrapped in HTML that can be configured with optional arguments. **Returns:** all tags as HTML which has been marked as safe. > **Related Topics:** See [tags.md](tags.md) for more information about tags and [url_structure.md](url_structure.md) for tag URL patterns. #### blog_tags Parameters - `outer_tag` (optional): the outer tags that this should be wrapped in. Accepted options are "ul", "div", "span". Default is: "ul". - `outer_class` (optional): the CSS classes to apply to the outer tag. Default: "". - `link_class` (optional): the CSS classes to apply to the link tag. Default: "". #### blog_tags Examples Just the tag, with no arguments. ```django {% blog_tags %} ``` Outputs a list of all tags: ```html ``` Wrapped in a `div` tag with classes added, using positional arguments. ```django {% blog_tags "div" "tag-list" "tag-link" %} ``` Outputs a comma-separated list of tags, wrapped in a `div` tag. ```html
Python, Django
``` Wrapped in a `span` tag with classes added, using named arguments. ```django {% blog_tags outer_tag="span" outer_class="tag-list" link_class="tag-link" %} ``` Outputs the same comma-separated list of tags, but wrapped in a `span` tag. ```html Python, Django ``` ### tags_with_counts Get a list of all tags with post counts wrapped in HTML that can be configured with optional arguments. **Returns:** all tags with post counts as HTML which has been marked as safe. #### tags_with_counts Parameters - `outer_tag` (optional): the outer tags that this should be wrapped in. Accepted options are "ul", "div", "span". Default is: "ul". - `outer_class` (optional): the CSS classes to apply to the outer tag. Default: "". - `link_class` (optional): the CSS classes to apply to the link tag. Default: "". #### tags_with_counts Examples Just the tag, with no arguments. ```django {% tags_with_counts %} ``` Outputs a list of all tags with post counts: ```html ``` Wrapped in a `div` tag with classes added. ```django {% tags_with_counts outer_tag="div" outer_class="tag-cloud" link_class="tag" %} ``` Outputs a comma-separated list of tags with post counts, wrapped in a `div` tag. ```html
Python (5), Django (3)
``` ### site_pages Get all site pages as a single-level list, wrapped in HTML that can be configured with optional arguments. **Returns:** all blog pages as HTML marked as safe. #### site_pages Parameters - `outer` (optional): the outer tags that this should be wrapped in. Accepted options are "ul", "div", "span". Default is: "ul". - `outer_class` (optional): the CSS classes to apply to the outer tag. Default: "". - `link_class` (optional): the CSS classes to apply to the link tag. Default: "". #### site_pages Examples Just the tag, with no arguments. ```django {% site_pages %} ``` This will output the same HTML from the `get_pages` example. ```html ``` Wrapped in a `div` tag with classes added, using positional arguments. ```django {% site_pages "div" "class1" %} ``` Outputs a comma-separated list of pages, wrapped in a `div` tag. ```html
About me, Contact me
``` Wrapped in a `span` tag with classes added, using named arguments. ```django {% site_pages outer="span" outer_class="class1" link_class="class2" %} ``` Outputs the same comma-separated list of pages, but wrapped in a `span` tag. ```html About me, Contact me ``` ### site_pages_list Get all published site pages and output as a nested list to support parent-child page relationships. This tag is ideal for building hierarchical navigation menus that reflect your page structure. **Returns:** HTML which has been marked as safe. > **Related Topics:** See [url_structure.md](url_structure.md) for page URL patterns and [themes.md](themes.md) for > creating navigation in your theme. #### site_pages_list Parameters - `ul_outer_class` (optional): The CSS class(es) for the outer unordered list. - `li_class` (optional): The CSS class(es) for the list item tags - `a_class` (optional): The CSS class(es) for the anchor tags. - `ul_child_class` (optional): The CSS class(es) for the nested unordered lists. #### site_pages_list Examples Just the tag, with no arguments. ```django {% site_pages_list %} ``` This will output the following HTML: ```html ``` With CSS classes: ```django {% site_pages_list ul_outer_class="main-nav" li_class="nav-item" a_class="nav-link" ul_child_class="sub-nav" %} ``` This will output: ```html ``` ## Post Content Tags These tags help you display the content of posts. ### post_title Display the title of a post, with intelligent handling of linking depending on the context. - In a single post view, displays just the title with no link. - In a posts list view, displays the title with a link to the full post. **Returns:** HTML with the post title, marked as safe. > **Related Topics:** See [themes.md](themes.md) for more about post display in templates and [url_structure.md](url_structure.md) for post URL patterns. #### post_title Parameters - `outer_tag` (optional): The outer HTML tag for the title. Accepted options are "h1", "h2", "h3", "h4", "h5", "h6", "p", "div", "span". If not provided, no outer tag is used. - `link_class` (optional): The CSS class(es) to apply to the link if the title is linked. - `force_link` (optional): Boolean. If true, always links the title even in a single post view. Default is false. - `include_empty` (optional): Boolean. If true, includes the title even if it is empty, using the post_title property fallback. Default is false. #### post_title Exampless Basic usage in a template: ```django {% post_title %} ``` In a single post view, this will output: ```html My Blog Post Title ``` In a posts list view, this will output: ```html My Blog Post Title ``` With an outer tag and custom link class: ```django {% post_title outer_tag="h1" link_class="post-title-link" %} ``` In a single post view with microformats enabled, this will output: ```html

My Blog Post Title

``` In a posts list view with microformats enabled, this will output: ```html

My Blog Post Title

``` Force link in a single post view: ```django {% post_title force_link=True %} ``` This will always generate a link to the post, even in single post view. ### post_content Display the content of a post with intelligent handling of both single posts and post lists. - In a single post view, returns the full content of the post. - In a posts list view, returns the truncated content with a "read more" link. **Returns:** HTML content for the post which has been marked as safe. **Related Topics:** See [themes.md](themes.md) for more about content display and [configuration.md](configuration.md) for content truncation settings. #### post_content Parameters - `outer_tag` (optional): The outer HTML tag to wrap the content in. Accepted options are "section", "div", "article", "p", "span". If not provided, no outer tag is used. - `read_more_link_class` (optional): The CSS class(es) to apply to the "read more" link. - `read_more_text` (optional): The text to use for the "read more" link. If not provided, defaults to "Read more". #### post_content Examples Basic usage in a template: ```django {% post_content %} ``` This will output the full content of a post in a single post view, or the truncated content with a "read more" link in a posts list view. With custom read more text and an outer tag: ```django {% post_content outer_tag="div" read_more_link_class="btn btn-primary" read_more_text="Continue reading..." %} ``` This will output: ```html

This is the full content of the post...

This is the truncated content of the post...

Continue reading...
``` Note: The `class="e-content"` will only be added if Microformats are enabled in the configuration. ### post_date Display the date of a post, optionally with links to the archives if archive functionality is enabled. **Returns:** HTML with formatted date information, marked as safe. **Related Topics:** See [url_structure.md](url_structure.md) for archive URL patterns and [configuration.md](configuration.md) for date/archive settings. #### post_date Parameters - `link_class` (optional): The CSS class(es) to apply to the date links. #### post_date Examples Basic usage in a template: ```django {% post_date %} ``` If archive functionality is disabled, this will output: ```html May 20, 2025 ``` If archive functionality is enabled, this will output links to the date archives: ```html May 20, 2025, 10:30 AM. ``` With microformats enabled, the output will include time tag: ```html ``` With a custom link class: ```django {% post_date link_class="date-link" %} ``` This will add the "date-link" class to all the date links. ### post_author Return the author link for a post. **Returns:** HTML with the author name and link, marked as safe. #### post_author Parameters - `link_class` (optional): The CSS class(es) to apply to the link. #### post_author Examples ```django {% post_author %} ``` If author links are enabled, this will output: ```html John Doe ``` If author links are disabled, this will output just the author name: ```html John Doe ``` The microformat class `p-author` will only be added if Microformats are enabled. ### post_category_link Return the category link for a post. **Returns:** HTML with the category link, marked as safe. #### post_category_link Parameters - `category`: The category object of the post. - `link_class` (optional): The CSS class(es) to apply to the link. #### post_category_link Examples ```django {% for category in post.categories.all %} {% post_category_link category %} {% endfor %} ``` If category functionality is disabled, this will output just the category name: ```html General ``` If category functionality is enabled, this will output a link to the category: ```html General ``` ## Utility Tags These tags provide additional helper functions for your templates. ### have_posts Return the posts in the context, providing a convenient way to check if there are posts to display. **Returns:** list of Post objects or a Page object of posts. #### have_posts Example ```django {% have_posts as posts_list %} {% if posts_list %}

We have posts!

{% for post in posts_list %}

{{ post.title }}

{% endfor %} {% else %}

No posts to display.

{% endif %} ``` ### category_title Return the title of a category from the current context. **Returns:** string or HTML-formatted string with the category title. #### category_title Parameters - `outer` (optional): The outer HTML tag for the category. Allowed values: "h1", "h2", "h3", "h4", "h5", "h6", "p", "div", "span". - `outer_class` (optional): The CSS class(es) for the outer tag. - `pre_text` (optional): The text to prepend to the category title. - `post_text` (optional): The text to append to the category title. #### category_title Examples ```django {% category_title outer="h1" outer_class="category-title" pre_text="Category: " %} ``` This will output: ```html

Category: General

``` ### tag_title Return the title of a tag from the current context. **Returns:** string or HTML-formatted string with the tag title. #### tag_title Parameters - `outer` (optional): The outer HTML tag for the tag title. Allowed values: "h1", "h2", "h3", "h4", "h5", "h6", "p", "div", "span". - `outer_class` (optional): The CSS class(es) for the outer tag. - `pre_text` (optional): The text to prepend to the tag title. - `post_text` (optional): The text to append to the tag title. #### tag_title Examples ```django {% tag_title outer="h1" outer_class="tag-title" pre_text="Posts tagged with: " %} ``` This will output: ```html

Posts tagged with: Python

``` If multiple tags are in the context, they will be joined with commas: ```html

Posts tagged with: Python, Django

```