2
我有一些客戶端靜態jQuery代碼,我想用我的網站的每個頁面上使用的服務器動態代碼替換,並取決於我的頁面在(被問路線)。 我現有的jQuery代碼(模板html.eex文件):Phoenix:在視圖中使用「conn」和「case」
if (SOME CONDITION) {
$(".page1.firstChild").css({"margin-top": "70px"});
$(".page2.firstChild").css({"margin-top": "70px"});
$(".page3.firstChild").css({"margin-top": "70px"});
$(".page4.firstChild").css({"margin-top": "70px"});
} else {
$(".page1.firstChild").css({"margin-top": "0px"});
$(".page2.firstChild").css({"margin-top": "0px"});
$(".page3.firstChild").css({"margin-top": "0px"});
$(".page4.firstChild").css({"margin-top": "0px"});
}
所以,我想,以取代「.page1
」,「.page2
」 ......「.pageN
」與被放置在一個變量該模板使用<% currentPage %>
,並在我的layout_view中定義,以便可以在與該佈局相關的每個頁面上訪問它。所以,我想這一點:
defmodule myApp.LayoutView do
use myApp.Web, :view
def currentPage do
case @conn.request_path do
"/page1" -> "page1"
"/page2" -> "page2"
"/page3" -> "page3"
end
end
end
我得到這個錯誤:
undefined function: nil.request_path/0
什麼是這樣做的權利的最佳途徑? (我也不確定「案例」代碼)。