在我的项目中使用 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 编译器首先在下面引用的 elseif 生成的,volt 文件没有被编译到 PHP 中。
{% elseif table.form.rendered_in == 'page' %}
错误说: 第 307 行的 .../app/views/partials/grideditor.volt 中出现意外的 ENDIF
if-elseif-endif 结构在 Javascript 块的其他地方也能很好地工作。更奇怪的是,当我用多个 if-endif、if-endif、... 替换 elseif 时,一切正常。
{% 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)
任何建议都非常感谢!谢谢!