2017-09-27 40 views
1

我是新來的OctoberCMS,所以我不知道很多事情。 我讀了十月的文檔,我知道如何在靜態方式使用諧音時傳遞變量:當包含部分內容時,我可以傳遞php變量嗎?

{% partial "location" city="Vancouver" country="Canada" %} 

我的問題是,我需要使用PHP或JS的變量。假設我有一個輸入字段,用戶在其中寫入一個ID,然後在按下按鈕後,我想將ID傳遞給一個部分。我試圖做這樣的事情:

{% partial "location" city=$city country=$country %} 

有人可以幫助我嗎?謝謝。

回答

0

您是否嘗試過此方法? 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-updateshttps://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 
}) 
+0

是的,但在這種情況下,國家應該是枝條變量,我不知道如何使用它們,我試過但沒有結果。 –

+0

根據本頁底部的blub(「訪問邏輯」 - https://octobercms.com/docs/markup/templating),Twig應該能夠訪問PHP變量;當然沒有美元符號。 – waterloomatt

+0

在調用您的偏好之前,嘗試輸出一個變量並查看它是否顯示 - {{country}} – waterloomatt

0

可以以這種方式使用的部分內部變量:

<p>Country: {{ country }}, city: {{ city }}.</p> 
+0

是的,但是在需要訪問變量之前,OP需要將變量傳遞給局部變量。 – waterloomatt

+0

有一個專用函數onStart(),它可以幫助我們在腳本訪問它們之前定義變量。 –

相關問題