[英]How to include different blade template view in if-else clause in Laravel?
我已經看過Laravel Blade教程 。 我在項目中使用了它。 但是我想基於下拉列表中選擇的項目添加不同的刀片模板視圖。
示例:-我有一個create Question.blade.php頁面。
Question.blade.php
<div class="question_type">
<?php $question_type= array('--' => '--- Select Question Type ---')
+QuestionType::lists('name', 'id') ?>
{{ Form::select('question_type', $question_type,'--' ,array('id'=>'question_type','class' => 'form-control')) }}
</div>
現在,我想根據Question_Type下拉列表中選擇的選項添加不同類型的視圖。 我正在嘗試用Java腳本來實現。
Java腳本代碼:-
function addQuestion()
{
var question_type=$("#question_type").val();
switch (question_type)
{
case 1:
$("#Questions").load("questions/type1.blade.php");
case 2:
$("#Questions").load("questions/type2.blade.php");
default:
$("#Questions").load("questions/default_type.blade.php");
}
}
但我收到錯誤localhost / mywebsite / question / questions / type1.blade.php 404(未找到)
有什么解決方案可以根據下拉的所選項目的值使用@if @else或@switch包括刀片視圖?
我的偽代碼:-
@if {{question_type.selected.val='1'}}
@include(question.type1);
@elseif {{question_type.selected.val='2'}}
@include(question.type2);
@else
@include(question.default_type);
@endif
如果要請求laravel,則需要具有與其關聯的方法的路由 (無論是在hello world示例中是簡單的閉包還是在控制器類內部的方法)。
在該方法的內部,您可以在其中執行邏輯以返回哪個視圖:
if($someCondition) {
return View::make('template1');
} else {
return View::make('template2');
}
在您的JavaScript中,將您的question_type
作為參數發送到AJAX請求中。 然后,您可以在控制器方法中做出決定:
$("#Questions").load("questions/type", "question_type="+question_type);
您的路線:
Route::get('questions/type', function() {
switch(Input::get('question_type')) {
case 1:
return View::make("questions/type1.blade.php");
case 2:
return View::make("questions/type2.blade.php");
default:
return View::make("questions/default_type.blade.php");
}
});
您對jQuery.load()
調用將隱式設置ID題等於請求的整個輸出(無論返回的視圖中包含什么)的DOM元素的內容。 從jQuery文檔 :
此方法是從服務器獲取數據的最簡單方法。 它與$ .get(url,data,success)大致等效,除了它是方法而不是全局函數,並且具有隱式回調函數。 當檢測到成功的響應時(即,當textStatus為“成功”或“未修改”時),. load()將匹配元素的HTML內容設置為返回的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.