How to use the AnQiCMS `ljust` filter to align the navigation menu text to the left and fill with spaces?

Calendar 👁️ 85

AnQi CMS is an efficient and flexible content management system that provides users with a rich set of template tags and filters, helping us easily customize every aspect of the website. Today, we will explore a practical template technique: how to useljustThe filter allows your navigation menu text to be aligned neatly to the left and intelligently fill in spaces, thus enhancing the visual consistency and user experience of the website.

In web design, the navigation menu plays a crucial role, not only guiding users through the website content but also directly affecting the professionalism of the website's visual presentation.When faced with the situation where the text length of the navigation menu is uneven, if it is displayed directly, it is easy to appear uneven in the width of the menu items and chaotic alignment.ljustThe filter can shine.

AnQi CMS template basic review

In AnQi CMS, templates are the core of building website interfaces. We mainly interact with templates in two ways: double curly braces{{ 变量 }}Used for output content, and single curly braces plus percent sign{% 标签 %}Used for controlling logic, such as conditional judgments and loops.Filters are small tools that process these variables, helping us to format, convert, or beautify data, such as converting text to uppercase, or as we will discuss today, adjusting the text alignment.

Get to knowljustFilter

ljustIt is a special filter used for string processing, its function is to align the text content to the left and fill the specified number of spaces on the right until it reaches the total length we set.This is a very practical feature for those who want to maintain consistent visual width of menu items and avoid visual chaos caused by differences in text length.

Its basic usage is very intuitive:{{ "您的文本"|ljust:目标总长度 }}

Among them,"您的文本"It is the original string you need to process, and目标总长度is an integer representing the total number of characters you want the string to reach. If the length of the original text is less than目标总长度,ljustwill automatically fill in spaces on the right side of the text; if the length of the original text is equal to or greater than目标总长度the text will be displayed as is without being truncated.

ljustactual application in the navigation menu

In AnQi CMS template, we usually usenavListtags to obtain the website's navigation menu data. This tag will return an array containing navigation items, each withTitle(Display name),Link(Link address) properties, even possibly containingNavListUsed for the second-level menu.

Now, let's see an example of how to useljusta filter in the navigation menu:

<nav class="main-navigation">
    <ul>
        {# 使用 navList 标签获取主导航菜单数据 #}
        {% navList navs %}
            {# 遍历每一个主导航项 #}
            {% for item in navs %}
                <li class="nav-item">
                    <a href="{{ item.Link }}">
                        {# 应用 ljust 过滤器,将导航文本左对齐并填充到15个字符的视觉宽度 #}
                        {{ item.Title|ljust:15 }}
                    </a>
                    {# 如果存在二级菜单,则进一步遍历 #}
                    {% if item.NavList %}
                        <ul class="sub-nav">
                            {% for sub_item in item.NavList %}
                                <li class="sub-nav-item">
                                    <a href="{{ sub_item.Link }}">
                                        {# 子菜单文本也可以应用 ljust,可以设置不同的目标长度 #}
                                        {{ sub_item.Title|ljust:20 }}
                                    </a>
                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            {% endfor %}
        {% endnavList %}
    </ul>
</nav>

In this code, we first use{% navList navs %}Retrieved all navigation data and assigned it tonavsthe variable. Then, we traversed{% for item in navs %}each main navigation item in a loop. Inside<a>the tag, we applied{{ item.Title }}the|ljust:15The filter means that regardlessitem.Titleof the original length, it will be left-aligned and spaces will be filled on the right until the entire string reaches a visual width of 15 characters. Ifitem.TitleLike "Home

For the secondary menusub_item.TitleWe also applied it toljustthe filter and set its20target length, which can be adjusted according to the actual content and design requirements of the submenu.

In this way, even if the text length of each menu item is different, each one will be rendered in the browser.<a>The text within the tags will maintain a uniform width visually, making the entire navigation menu look more orderly and professional. Of course, here the15and20This is just an example, you should adjust the number based on the actual menu text length and the expected filling effect.

Advanced considerations and related filters

  • Behavior when the length is insufficient: ljustThe filter is responsible for padding and will not truncate the original text.If your navigation text may be very long and exceeds the target length you have set, it will display as it is.truncatecharsortruncatewordsFilter, in useljustTruncate text before to avoid layout expansion.
  • Dynamic length: In some advanced scenarios, you may need to dynamically calculate based on the actual page content or the length of the longest navigation itemljustThe target length, which may require some辅助 processing on the front end using JavaScript, or through calculation during template rendering to obtain the maximum length value.
  • Other alignment method:exceptljust(Left aligned), Anqi CMS also provides:rjustandcenterFilter:
    • rjust: Fill spaces on the left side of the text to achieve right alignment

Related articles

In Anqi CMS template, how to center the product title within a fixed area?

In Anqi CMS template, centering the product title within a fixed area is a common layout requirement, which can effectively enhance the visual aesthetics and user experience of the website.Due to the similar Django template engine syntax adopted by AnQi CMS and its good support for frontend styles, we can achieve this goal by combining HTML structure and CSS styles. ### Understanding Anqi CMS Template Structure Firstly, we need to clarify how the product title is called in the Anqi CMS template.

2025-11-07

How to display comments of the `commentList` tag

In website operation, article comments are an indispensable part of enhancing user interaction and activating the community atmosphere.AnQiCMS (AnQiCMS) provides us with a powerful and flexible comment management function, through the clever use of `commentList` tags, we can easily display the comments of articles on the website front-end.This article will delve into the various functions of the `commentList` tag and its practical application in templates, helping you create an interactive and user-friendly comment section.--- ### Core Tag

2025-11-07

How to get and display the carousel or advertisement images on the homepage using the `bannerList` tag?

In the operation of a website, the homepage carousel or advertisement image is undoubtedly the golden area to attract visitors' attention, convey core information, and promote important content.They not only enhance the visual appeal of a website, but are also crucial for guiding user behavior and promoting content consumption.In Anqi CMS, the functions to implement and manage these carousel or ad images are all concentrated on a powerful and easy-to-use tag —— `bannerList`.

2025-11-07

How to build a dynamic filter using the `archiveFilters` tag to display documents that meet specific conditions?

How to help users quickly find the information they are interested in when managing a content-rich website, and improve the access experience, is a challenge that every content operator needs to face.AnQiCMS (AnQiCMS) understands this need and therefore provides a powerful `archiveFilters` tag, allowing you to easily build dynamic filters to provide flexible and diverse search methods for website content. Dynamic filter, as the name implies, is to automatically generate filtering conditions for users to choose from based on different attributes of the content.

2025-11-07

How to align the date and time stamp to the right to a specified length on the AnQiCMS article detail page?

On the article detail page of the website, we often hope that the publication date or update date of the article can be displayed neatly, and sometimes they need to be aligned to the right and occupy a fixed width, so that the page looks professional and beautiful.AnQiCMS provides flexible template tags and filters to help us easily meet this requirement. ### Understanding AnQiCMS Date Handling AnQiCMS typically stores dates and times in the form of timestamps (timestamps).This means calling `{{

2025-11-07

How to distribute the padding spaces on both sides when the string length is odd for the AnQiCMS `center` filter?

In AnQiCMS template development, we often need to fine-tune text layout and alignment to make the page content look neater and more beautiful.At this time, filters like `center` are particularly practical.It can help us easily center the string and automatically fill in spaces on both sides.But, when you start using it, you may be curious about a detail: how does AnQiCMS distribute the spaces on both sides when the number of spaces to be filled is odd?### The basic function of the `center` filter First

2025-11-07

AnQiCMS `ljust` and `rjust` filters how do they handle display when the string exceeds the specified length?

When building a website, we often need to finely arrange and align the text content on the page, especially when displaying lists, tables, or other areas that require a fixed layout.AnQiCMS is a powerful template engine that provides various filters to help us meet these needs, where `ljust` (left alignment) and `rjust` (right alignment) are practical tools for controlling string alignment and padding.They can make our content look more uniform and enhance the user reading experience.However, a common issue arises when using these filters

2025-11-07

What is the default character used for filling by the `center`, `ljust`, and `rjust` filters in the AnQiCMS template?

The AnQiCMS template system provides a rich set of filters (filters) to help us format and process content.When performing text alignment operations, the `center`, `ljust`, and `rjust` filters are commonly used tools.They can center, left-align, or right-align our text content within a fixed width.When first encountering these filters, many users may be curious about what characters the system will use to fill in the blank areas when the text length is insufficient for the specified width? Below

2025-11-07