【问题标题】:Mako templates: alphabetically order the listMako 模板:按字母顺序排列列表
【发布时间】:2016-05-09 20:27:49
【问题描述】:

我正在使用 Python/Pyramid 和 Mako 模板,这对我来说都是全新的。

我有一个包含不同模板的列表,现在列表以随机顺序出现,但我希望它按字母顺序排列。 有没有人有什么建议? 这是获取列表的代码:

<div class="panel-small">

    <h2>Choose template</h2>

    <div id="template_list">

    % if template_list and len(template_list) > 0:

        % for template in template_list:

            % if template['template']:

                <a href="#${template['type']}_${template['template_id']}" id="${template['template_id']}" class="template_link ${template['type']}">${template['name']}<br />

            % else:

                % if template['url'] is not None and template['url'].startswith('mailto'):

                    <a href="${template['url']}" class="external">${template['name']}</a><br />

                % else:

                    <a href="${template['url']}" class="external" target="_blank">${template['name']}</a><br />

                % endif

            % endif

        % endfor

    % else:

        No template.

    % endif

    </div>

</div>

【问题讨论】:

    标签: pyramid mako


    【解决方案1】:

    在模板中呈现之前,只需在 python 中对“template_list”进行排序。

    【讨论】:

    • 大概也可以直接在模板中使用sorted。
    • 我相信你可以,但我尽量让我的模板代码尽可能简单。我不知道一种方法是否比另一种更快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多