【问题标题】:Is there a markdown friendly alternative to "truncatechars:x"?是否有“truncatechars:x”的降价友好替代方案?
【发布时间】:2016-10-18 05:02:58
【问题描述】:

经典任务是显示博客文章列表,包括每篇文章的简短摘要。

正如我在网上看到的,最常见的方法是截断原始博客文章的内容。

所以我的post_list.html 模板看起来像这样:

{% load custom_markdown %}

<div class="col-sm-12">
 <p>
  <small>
   {{ post.text | custom_markdown | truncatechars:160 }}
  </small>
 </p>
</div>

如果我截断 markdown,有时 html 标签没有正确关闭 - 结束标签被截断 - 整个 html 页面都变得一团糟。

有没有智能的截断方法或者我需要编写自己的函数?

PS 我得到的最接近的是stackoverflow post: how to truncate markdown in Ruby/Rails

【问题讨论】:

    标签: django python-2.7 django-templates markdown


    【解决方案1】:

    您可以使用truncatechars_html 标签。

    {{ post.text | custom_markdown | truncatechars_html:160 }}
    

    【讨论】:

    • 阿拉斯代尔,这太棒了!这解决了我的问题! html 格式是正确的,我没有得到几分钟前我曾经得到的奇怪的换行符!呵呵,其实博客文章列表在所有降价效果下看起来并不那么可读,但化妆品是我的工作。真的非常感谢!
    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2020-10-07
    相关资源
    最近更新 更多