As an experienced soldier in the field of CMS content operation, I know that every detail in website construction and content presentation is crucial to user experience and brand image.Especially when displaying critical elements such as contact information, how to ensure both beauty and tidiness within limited space while completely conveying the information, this is often a small challenge faced by operators.Today, let's delve into how to cleverly truncate the display of the contact address field in the template when it is too long, while ensuring that users can always obtain the complete address information.

Challenge: Balancing a long address with the interface aesthetics

When designing a website page, especially the footer, sidebar, or contact information module, we always hope that the information is presented in a concise and clear manner.However, the actual business contact address often includes details such as provinces, cities, streets, and house numbers, which leads to the text length exceeding expectations.Directly displaying a long address can disrupt the overall coordination of the layout, making the page look bulky, and may also lead to chaotic typesetting in responsive design, affecting the reading experience of users on different devices.

How can you keep the page clean while not missing any important address information?The flexible template mechanism and powerful built-in filters of Anqi CMS provide us with an elegant solution.

The solution of Anqi CMS: Flexible templates and powerful filters

The template engine of AnQi CMS supports syntax similar to Django, which makes content customization very simple.It can easily retrieve various configuration data from the background and can process these data in real-time through the "filter" chaining calls without modifying the original data.contactTags to obtain contact information set in the background.

For example, to get the contact address configured in the "Contact Settings" in the background, we can use the following tag in the template:

{% contact with name="Address" %}

This label will directly output the complete contact address string. But when the address is too long, we need to use the "small tool" provided by AnQi CMS for string processing - the filter.

核心技巧:利用 Englishtruncatechars过滤器进行截断 English

安企CMS内置了多种实用的过滤器,其中 EnglishtruncatecharsFilter is specifically designed for string truncation.Its function is to truncate a string to a specified character length and automatically add an ellipsis (…) at the truncation point, thereby effectively controlling the display length.

The usage method is very intuitive:

{{ 你的变量 | truncatechars: 期望的长度 }}

比如,如果我们的联系地址保存在 EnglishcontactAddressIn a variable, and we hope it to display at most 30 characters, it can be written like this:

{{ contactAddress | truncatechars:30 }}

So, if the address '18A, 10th Floor, Science and Technology Park, Nanshan District, Shenzhen, Guangdong Province, China' is too long, it may be displayed as '18A, 10th Floor, Science and Technology Park, Nanshan District, Shenzhen, Guangdong Province, China...' which not only indicates that the information is not fully displayed but also maintains the cleanliness of the page.

How should the 'expected length' be chosen?This usually depends on your website design and layout.You may need to test on different device sizes to find a length that can ensure most of the address information is visible and avoid wrapping or overflow.通常,20-40个字符是一个比较常见的范围。

Retain complete information: Improve user experience

It is not enough to truncate the display, we promise to “retain complete information”. To achieve this, we can combine HTML'stitleCreate a tooltip (hover tip) with properties.When the user hovers over the truncated address, the browser will display a tooltip containing the full address to ensure that the user can always check it.

{# 假设我们已经通过 contact 标签获取了完整的联系地址到 contactAddress 变量 #}
{% contact contactAddress with name="Address" %}

{% if contactAddress %}
    <span class="truncated-address" title="{{ contactAddress }}">
        {{ contactAddress | truncatechars:35 }}
    </span>
{% else %}
    <span class="no-address">暂无联系地址</span>
{% endif %}

In this code block:

  1. We first use{% contact contactAddress with name="Address" %}get the complete addresscontactAddressa variable.
  2. {% if contactAddress %}Check if the address exists to avoid displaying empty content.
  3. <span class="truncated-address" title="{{ contactAddress }}">Is crucial. We will provide the completecontactAddressAssign to.titleproperties.
  4. {{ contactAddress | truncatechars:35 }}responsible for displaying the truncated address.
  5. elsebranch handling the case of empty address, providing a more friendly prompt.

This way, when users browse the page, they see a concise truncated address, and when they need detailed information, they just need to hover the mouse over the address, and the full address will be presented immediately in the form of a tooltip, perfectly balancing beauty and the completeness of information.

In addition to the hover tips, if the address information is extremely complex, or contains map links, navigation instructions, etc., you can also consider adding a 'View Details' or 'Contact Us' link next to the truncated address, pointing to a separate page containing all the detailed information.

{# 额外补充:如果想提供完整的地址链接到地图或其他页面 #}
{% contact contactAddress with name="Address" %}
{% if contactAddress %}
    <p>
        联系地址:
        <span class="truncated-address" title="{{ contactAddress }}">
            {{ contactAddress | truncatechars:35 }}
        </span>
        {# 利用 urlencode 过滤器,确保地址作为URL参数时不会出错 #}
        <a href="https://maps.google.com/?q={{ contactAddress | urlencode }}" target="_blank" rel="nofollow">查看地图</a>
    </p>
{% endif %}

Here we also added extraurlencodeFilter.When the address is included as part of the URL (such as jumping to Google Maps), the address may contain spaces, commas, or other special characters, which, if used directly, can cause the link to fail.urlencodeFilter can automatically encode these special characters to ensure the validity of the link.

Comprehensive Example

Now, let's integrate these techniques into an actual website footer template snippet:

{# partial/footer.html 文件片段 #}
<footer>
    <div class="container">
        <div class="contact-info">
            <h3>联系我们</h3>
            <p>联系电话:{% contact with name="Cellphone" %}</p>
            <p>
                联系地址:
                {% contact fullAddress with name="Address" %}
                {% if fullAddress %}
                    <span class="truncated-address" title="{{ fullAddress }}">
                        {{ fullAddress | truncatechars:35 }}
                    </span>
                    <a href="https://maps.google.com/?q={{ fullAddress | urlencode }}" target="_blank" rel="nofollow" class="view-map-link">
                        <i class="icon-map"></i> 查看地图
                    </a>
                {% else %}
                    <span>暂无</span>
                {% endif %}
            </p>
            <p>联系邮箱:<a href="mailto:{% contact with name="Email" %}">{% contact with name="Email" %}</a></p>
        </div>
        <div class="copyright">
            <p>{% system with name="SiteCopyright" %}</p>
            <p><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">{% system with name="SiteIcp" %}</a></p>
        </div>
    </div>
</footer>

In this example, we elegantly handled the display issue of the contact address, while ensuring the completeness and practicality of the information through hover tips and map links.

Operation Steps and Precautions

  1. Confirm modification of location:Find the template file that needs to display the contact address, for examplepartial/footer.htmlorpartial/aside.html.
  2. Backup file:Always back up any template file before making modifications, just in case.
  3. Application code:Integrate the above example code into your template and adjust the truncation length as needed (truncatechars:35of the number).
  4. Local test:Preview the effect in the local development environment or test server, ensure that it displays normally at different resolutions and on different devices, and that the hover tip function also works normally.
  5. Clear cache:If the contact address is modified in the background and the front-end is not updated, remember to go to the Anqi CMS background 'Update Cache' to clear the site cache.

With these simple yet practical tips, your website can not only present a more professional interface, but also demonstrate a focus on user experience in the details.

Common Questions and Answers (FAQ)

  1. 问:截断地址后,对网站的SEO会有影响吗?

    • Answer:In most cases, this frontend truncation display at the template level will not have a negative impact on SEO.The search engine's crawler reads the complete HTML code of the page when crawling web pages, not the visual effects processed by CSS or JavaScript in the user's browser.title属性中的完整地址,甚至原标签中未截断但被隐藏的部分,爬虫依然能够识别和索引。
  2. 问:How should I choose?truncatecharsthe **truncation length?

    • Answer:**The length of truncation does not have a fixed standard, it mainly depends on your website design layout and target user group.Suggest that you test on the PC, tablet, and mobile versions of the website.For example, on mobile devices, it may be necessary to have a shorter truncation length to avoid line breaks, while on PC, it can be relaxed appropriately.Choose a length that can maintain the layout beautiful in most cases and allow users to roughly understand the address information. Detailed information can be provided through hover tips or 'View Map' links.
  3. **问:If my contact address contains HTML tags (such as links or styles),truncatecharswill