当包含部分内容时,我可以传递php变量吗?
我是新来的OctoberCMS,所以我不知道很多事情。 我读了十月的文档,我知道如何在静态方式使用谐音时传递变量:当包含部分内容时,我可以传递php变量吗?
{% 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
编辑
刚刚重新阅读你原来的问题,你问的结合,形成要素,而不是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
})
可以以这种方式使用的部分内部变量:
<p>Country: {{ country }}, city: {{ city }}.</p>
是的,但是在需要访问变量之前,OP需要将变量传递给局部变量。 – waterloomatt
有一个专用函数onStart(),它可以帮助我们在脚本访问它们之前定义变量。 –
是的,但在这种情况下,国家应该是枝条变量,我不知道如何使用它们,我试过但没有结果。 –
根据本页底部的blub(“访问逻辑” - https://octobercms.com/docs/markup/templating),Twig应该能够访问PHP变量;当然没有美元符号。 – waterloomatt
在调用您的偏好之前,尝试输出一个变量并查看它是否显示 - {{country}} – waterloomatt