Phalcon';s Volt引擎在使用elseif条件时在jQuery块中生成错误


Phalcon's Volt engine generates error in jQuery block when using elseif conditional

在我的项目中使用Phalcon的Volt引擎时,我遇到了一个奇怪的PHP错误。这个例子看起来很简单,但尽管我已经检查了很多次代码,但我似乎无法处理简单的if-elseif-endif结构。

模板代码在这里,它被放在jQuery callcack函数的上下文中的Javascript块中:

{% if table.form.rendered_in == 'offcanvas' %}
            //offcanvas form
            //set attributes
            $(row).find('td.edit-control').
                attr('data-source', '{{table.form.full_action_url}}?get_form').
                attr('data-canvas', 'body').
                attr('data-target', '#rightSlider').
                attr('data-toggle', 'offcanvas').
                click(function () { 
                    console.log('! show edit form: '+record_id);    
                    //edit_one(record_id);
                    if (!right_offcanvas_visible) {
                        //request form with ajax
                        var url = $(this).attr('data-source');
                        var data = {
                            'choose_record': [record_id]
                        };
                        //console.log('Serialized data: '+data);    
                        //$('div#rightSlider').offcanvas('show');
                        TASK.Ajax.Post(url, data, function(response) {  
                            $('div#rightSlider').find('div.rightSliderContent').html(response);
                            //$('div.offcanvas').offcanvas({canvas: 'body'}); // todo: make it work!
                            console.log('! edit one record form set up');    
                        });
                    }
                });
            //delete    
            $(row).find('td.delete-control').
                attr('data-source', '{{table.form.full_action_url}}?get_delete_form').
                attr('data-canvas', 'body').
                attr('data-target', '#rightSlider').
                attr('data-toggle', 'offcanvas').
                click(function () { 
                    if (!right_offcanvas_visible) {
                        //request form with ajax
                        var url = $(this).attr('data-source');
                        var data = {
                            'choose_record': [record_id]
                        };
                        TASK.Ajax.Post(url, data, function(response) {  
                            $('div#rightSlider').find('div.rightSliderContent').html(response);
                        });
                    }
                });
        {% elseif table.form.rendered_in == 'page' %}
            //on same page above the table
            $(row).find('td.edit-control').
                attr('data-source', '{{table.form.full_action_url}}?get_form').
                click(function () { 
                    console.log('! show edit form above table: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#form-page-main').html(response);
                        $('html, body').animate({ //scroll smoothly to form
                            scrollTop: $('div#form-page-main').offset().top - config.scrollDistanceFromTop
                        }, config.timeAnimateToAjaxForm);
                    });
                });            

            $(row).find('td.delete-control').
                attr('data-source', '{{table.form.full_action_url}}?get_delete_form').
                click(function () { 
                    console.log('! show delete form above table: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#form-page-main').html(response);
                        $('html, body').animate({ //scroll smoothly to confirmation
                            scrollTop: $('div#form-page-main').offset().top - config.scrollDistanceFromTop
                        }, config.timeAnimateToAjaxForm);
                    });
                });
        {% elseif table.form.rendered_in == 'modal' %}
            // rendered in modal window
            $(row).find('td.edit-control').
                attr('data-source', '{{table.form.full_action_url}}?get_form').
                attr('data-target', '#largeModal').
                attr('data-toggle', 'modal').
                click(function () { 
                    console.log('! show edit form in modal: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#largeModal').find('div.modal-body').html(response);
                    });
                });            

            $(row).find('td.delete-control').
                attr('data-source', '{{table.form.full_action_url}}?get_delete_form').
                attr('data-target', '#smallModal').
                attr('data-toggle', 'modal').
                click(function () { 
                    console.log('! show delete form in modal: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#smallModal').find('div.modal-body').html(response);
                    });
                });
        {% endif %}

这个错误最初可能是由Volt编译器生成的。如果下面引用,Volt文件不会编译到PHP中。

{% elseif table.form.rendered_in == 'page' %}

错误显示:中出现意外的ENDIF/第307行的app/views/partials/gridditor.volt

if-elseif-endif结构在Javascript块的其他地方工作得很好。更奇怪的是,当我将elseif替换为多个if endif、if endif。。。如下所示,一切正常。

{% if table.form.rendered_in == 'offcanvas' %}
            //offcanvas form
            //set attributes
            $(row).find('td.edit-control').
                attr('data-source', '{{table.form.full_action_url}}?get_form').
                attr('data-canvas', 'body').
                attr('data-target', '#rightSlider').
                attr('data-toggle', 'offcanvas').
                click(function () { 
                    console.log('! show edit form: '+record_id);    
                    //edit_one(record_id);
                    if (!right_offcanvas_visible) {
                        //request form with ajax
                        var url = $(this).attr('data-source');
                        var data = {
                            'choose_record': [record_id]
                        };
                        //console.log('Serialized data: '+data);    
                        //$('div#rightSlider').offcanvas('show');
                        TASK.Ajax.Post(url, data, function(response) {  
                            $('div#rightSlider').find('div.rightSliderContent').html(response);
                            //$('div.offcanvas').offcanvas({canvas: 'body'}); // todo: make it work!
                            console.log('! edit one record form set up');    
                        });
                    }
                });
            //delete    
            $(row).find('td.delete-control').
                attr('data-source', '{{table.form.full_action_url}}?get_delete_form').
                attr('data-canvas', 'body').
                attr('data-target', '#rightSlider').
                attr('data-toggle', 'offcanvas').
                click(function () { 
                    if (!right_offcanvas_visible) {
                        //request form with ajax
                        var url = $(this).attr('data-source');
                        var data = {
                            'choose_record': [record_id]
                        };
                        TASK.Ajax.Post(url, data, function(response) {  
                            $('div#rightSlider').find('div.rightSliderContent').html(response);
                        });
                    }
                });
        {% endif %}

        {% if table.form.rendered_in == 'page' %}
            //on same page above the table
            $(row).find('td.edit-control').
                attr('data-source', '{{table.form.full_action_url}}?get_form').
                click(function () { 
                    console.log('! show edit form above table: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#form-page-main').html(response);
                        $('html, body').animate({ //scroll smoothly to form
                            scrollTop: $('div#form-page-main').offset().top - config.scrollDistanceFromTop
                        }, config.timeAnimateToAjaxForm);
                    });
                });            

            $(row).find('td.delete-control').
                attr('data-source', '{{table.form.full_action_url}}?get_delete_form').
                click(function () { 
                    console.log('! show delete form above table: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#form-page-main').html(response);
                        $('html, body').animate({ //scroll smoothly to confirmation
                            scrollTop: $('div#form-page-main').offset().top - config.scrollDistanceFromTop
                        }, config.timeAnimateToAjaxForm);
                    });
                });
        {% endif %}

        {% if table.form.rendered_in == 'modal' %}
            // rendered in modal window
            $(row).find('td.edit-control').
                attr('data-source', '{{table.form.full_action_url}}?get_form').
                attr('data-target', '#largeModal').
                attr('data-toggle', 'modal').
                click(function () { 
                    console.log('! show edit form in modal: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#largeModal').find('div.modal-body').html(response);
                    });
                });            

            $(row).find('td.delete-control').
                attr('data-source', '{{table.form.full_action_url}}?get_delete_form').
                attr('data-target', '#smallModal').
                attr('data-toggle', 'modal').
                click(function () { 
                    console.log('! show delete form in modal: '+record_id);    
                    var url = $(this).attr('data-source');
                    var data = {
                        'choose_record': [record_id]
                    };
                    TASK.Ajax.Post(url, data, function(response) {  
                        $('div#smallModal').find('div.modal-body').html(response);
                    });
                });
        {% endif %}

我在Windows上使用Phalcon 1.3.3 TS(x86,PHP 5.4.19)

非常感谢您的任何建议!谢谢

您可以在volt内部启动php并完成这些事情,而volt不提供这些功能。

我相信将来我们会有

{% literal %} {% endliteral %} 
or
{% verbatim %} {% endverbatim %}

github中已经存在NFR:https://github.com/phalcon/cphalcon/issues/1253

现在我找不到任何出路,只是在这部分使用纯php,比如:

...
<?php
 // your code like it would be simple *.php file
?>
...