我是OctoberCMS的新手,所以我不知道很多事情。 我阅读了 10 月份的文档,并且知道如何以静态方式使用局部变量时传递变量:
{% partial "location" city="Vancouver" country="Canada" %}
我的问题是我需要使用 php 或 js 变量。假设我有一个输入字段,用户在其中写入 ID,然后按下按钮后我想将 ID 传递给部分。我正在尝试做这样的事情:
{% partial "location" city=$city country=$country %}
有人可以帮助我吗?谢谢。
最佳答案
您是否尝试过此处记录的这种方法? https://octobercms.com/docs/cms/partials#partial-variables
{% partial "location" city=city country=country %}
编辑
顺便说一句,您需要在 onStart 函数中定义页面变量。
url = "/blah"
layout = "default"
==
<?
function onStart()
{
$this['country'] = ...;
$this['city'] = ...;
}
?>
==
{% partial "location" city=city country=country %}
编辑
您阅读过有关 AJAX 的部分吗? https://octobercms.com/docs/ajax/introduction
更具体地说 - https://octobercms.com/docs/ajax/update-partials#pushing-updates和https://octobercms.com/docs/ajax/update-partials#update-definition
罢工>
编辑
只需重新阅读您原来的问题,您会问的是绑定(bind)到表单元素,而不是 AJAX。
看看 JS API - https://octobercms.com/docs/ajax/javascript-api#javascript-api
我认为你可以这样做:
<form onsubmit="$(this).request('onMyProcessingMethod'); return false;">
$('form').request('onMyProcessingMethod', {
update: {myPartialName: '.whereIWantTheOutputToGo'},
data: {country: 'Canada'} // Not 100% sure how to access form input; maybe ID selector
})
关于javascript - 包含部分内容时可以传递 php 变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453697/