【问题标题】:How to include different blade template view in if-else clause in Laravel?如何在 Laravel 的 if-else 子句中包含不同的刀片模板视图?
【发布时间】:2014-09-16 15:31:13
【问题描述】:

我已经通过Laravel Blade tutorial。我在我的项目中使用它。但我想根据下拉列表中选择的项目添加不同的刀片模板视图。

示例:- 我有一个创建 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 Script 来做。

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 (Not Found)

是否有任何解决方案可以根据下拉所选项目的值使用@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

【问题讨论】:

  • $.load("questions/type1.blade.php"); 需要 url(返回视图),而不是视图文件的路径
  • 我可以使用 PHP 吗?
  • js不能访问blade,{{}}也不能运行js
  • 有什么办法吗?我的意思是我不想在 JavaScript 页面中编写整个 HTML 代码来附加它。它不喜欢清洁剂。

标签: php laravel blade


【解决方案1】:

如果你向 laravel 发出请求,你需要有一个 route 和一个与之关联的方法(无论是在他们的 hello world 示例中的简单闭包还是 controller 类中的方法) .

that 方法的内部,您可以对要返回的视图执行逻辑:

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 Questions 的 DOM 元素的内容等于请求的整个输出(无论返回的视图中包含什么)。来自jQuery docs

此方法是从服务器获取数据的最简单方法。它大致相当于 $.get(url, data, success) ,只是它是一个方法而不是全局函数,并且它具有隐式回调函数。当检测到响应成功时(即 textStatus 为“success”或“notmodified”时),.load() 将匹配元素的 HTML 内容设置为返回的数据。

【讨论】:

  • 感谢回复..我可以用 JavaScript 做吗?我的意思是如何在更改事件的下拉菜单中加载不同类型的子视图而不刷新页面?如果我知道 Laravel 如何将 @include(blade_sub_view) 转换为 HTML 代码,我的问题就会得到解决。 ....仍在挣扎...
  • 是的,您可以使用 javascript 来完成,但您需要将 AJAX 请求定向到 laravel 路由。您不能直接请求刀片模板。
  • yup..正确的方法...谢谢兄弟.... :) 但 AJAX 将返回整个页面或返回子视图。我可以在 Question_form 中附加?
  • 我已经更新了我的答案。对 .load 的调用应该自动获取请求的输出并将$('#Questions') 的内容设置为它。
  • 脱帽致敬..您的代码正在运行.. 但是$("#Questions").load("questions/type", "question_type="+question_type); 正在替换我的整个页面。我可以附加 AJAX 返回的 HTML 代码吗?
猜你喜欢
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-07-11
  • 2019-09-23
  • 2017-08-07
  • 2015-08-20
  • 2013-07-29
相关资源
最近更新 更多