django 1.10循环每4个元素添加hr标签

时间:2016-10-04 13:41:16

标签: html django django-templates jinja2

我想在html doc中每隔第4个子元素添加一个hr元素。这就是我到目前为止所做的:

function widget($args, $instance) {
        // Setup widget display elements
        global $tm_lunch_menu_days, $tm_lunch_menu_months;
        $before_widget = $after_widget = $before_title = $after_title = '';
        extract($args, EXTR_IF_EXISTS);
        // Retrieve additional settings
        $settings = get_option('tm_lunch_menu_settings');
        // Prepare query
        $today = mktime(0, 0, 0, date('n'), date('j'), date('Y'));
        $start = mktime(0, 0, 0, date('n'), date('j')-7, date('Y'));
        $end = mktime(0, 0, 0, date('n'), date('j')+(7*$settings['weeks']), date('Y'));
        $query_args = array(
            'post_type' => 'tm_lunch_menu',
            'numberposts' => $instance['numberposts'],
            'order' => 'ASC',
            'orderby' => 'meta_value_num',
            'meta_query' => array( array(
                'key' => '_tm_lunch_timestamp',
                'value' => array($start, $end),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
            ))
        );
        // Retrieve menus
        $menus = get_posts($query_args);
        if($menus || $settings['no_menu'] == 'display') {
            // Start widget output
            echo $before_widget;
            echo $before_title.$instance['title'].$after_title;
            // Process menus
            if($menus) {
                foreach($menus as $menu) {
                    $x=0; $y=7;
                    // Get menu info
                    $timestamp = get_post_meta($menu->ID, '_tm_lunch_timestamp', true);
                    $items = get_post_meta($menu->ID, '_tm_lunch_menu_day', true);
                    $start_day = date('w', $timestamp);
                    // While day of week is less than number of days in week
                    while($x < $y) {
                        if((in_array($start_day, $settings['days']) && !empty($items[$start_day])))
                        if(($instance['show_partial'] == 1 && ($today <= ($timestamp + (86400 * $x)))) || $instance['show_partial'] == 0) {
                            if(!$settings['format']) $settings['format'] = '%l, %M %d';
                            $week_day = date('w', $timestamp + (86400 * $x));
                            $month = date('n', $timestamp + (86400 * $x));
                            $day = date('j', $timestamp + (86400 * $x));
                            $tmp = str_replace('%F', $tm_lunch_menu_months[$month], $settings['format']);
                            $tmp = str_replace('%M', substr($tm_lunch_menu_months[$month], 0, 3), $tmp);
                            $tmp = str_replace('%l', $tm_lunch_menu_days[$week_day], $tmp);
                            $tmp = str_replace('%D', substr($tm_lunch_menu_days[$week_day], 0, 3), $tmp);
                            $tmp = str_replace('%d', $day, $tmp);
                            echo '<strong>'.$tmp.'</strong><br />';
                            echo $items[$start_day].'<br/>';
                        }
                        $x++;
                        $start_day = ($start_day < 6)? $start_day+1 : 0;
                    }
                }
            } else echo '<p>'.$settings['no_menu_msg'].'</p>';

我在那里的循环位用于分隔每个第二个元素,但我希望每隔4个元素。没有它,我每行得到4个元素。这就是我要的。基本上,我试图为每列制作一个标准高度。

2 个答案:

答案 0 :(得分:2)

{% if forloop.counter0|divisibleby:4 %}
    <div class='col-sm-12'><hr/></div></div><div></div><div class='row'>
{% endif %}

有关divisibleby的更多信息:https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#divisibleby

答案 1 :(得分:0)

您可以将两个以上的参数传递给循环标记。例如:

{% cycle "" "" "" "<div..." %}

使用divisibleby可能更为整洁,因为1GDST建议,我只是将此答案作为替代方案添加。