简体   繁体   English

如何将Php foreach转换成Twig?

[英]How to convert Php foreach to Twig?

I'm using CodeIgniter with Twig, i'm trying to integrate FullCalendar I got the librerary form Here , my problem is with this line - in the Index.php file : 我正在使用CodeIgniter和Twig,我正在尝试集成FullCalendar我得到了librerary表单在这里 ,我的问题是这行 - 在Index.php文件中:

events: [
    <?php foreach($events as $event):
        $start = explode(" ", $event['start']);
        $end = explode(" ", $event['end']);
        if($start[1] == '00:00:00'){
            $start = $start[0];
        }else{
            $start = $event['start'];
        }
        if($end[1] == '00:00:00'){
            $end = $end[0];
        }else{
            $end = $event['end'];
        }
    ?>
        {
            id: '<?php echo $event['id']; ?>',
            title: '<?php echo $event['title']; ?>',
            start: '<?php echo $start; ?>',
            end: '<?php echo $end; ?>',
            color: '<?php echo $event['color']; ?>',
        },
    <?php endforeach; ?>
]

Since i'm using Twig - i can't translate this part into twig, i've tried : 因为我正在使用Twig - 我无法将这部分翻译成树枝,我尝试过:

events: [
    {
        {% for res in events_from_db %}
        id : "{{ res.id}}",
        title : "{{ res.title}}",
        start : "{{ res.start}}",
        end : "{{ res.end}}",
        color : "{{ res.color}}",
        {% endfor %}
    }
]

But it's getting only the last key of the Array.... So i figure - why not simply insert the array directly - So i did this: 但它只获得数组的最后一个键....所以我想 - 为什么不直接插入数组 - 所以我这样做:

var events_from_db = '{{ events_from_db|json_encode|raw }}';

var json_res = JSON.parse(events_from_db);
events_res = [];
$.each(json_res, function (k,v) {
    events_res.push(v);
});

events: [
    events_res
]

But it didn't worked either... 但它也没有用......

Your loop should be outside the JS brackets: 你的循环应该在JS括号之外:

{% for res in events_from_db %}
    {
        id : "{{ res.id}}",
        title : "{{ res.title}}",
        start : "{{ res.start}}",
        end : "{{ res.end}}",
        color : "{{ res.color}}",
    },
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM