Arturo's avatar
Level 10

Ajax and partial view Url

How I could get the partial balde url to call in a function. I need put the partial view in public folder?

This is my code

$.get(' {{ @include('layout/partials/_filter') }} ', function(template) {
                $('.container').html(template);
            },"text");
0 likes
9 replies
RachidLaasri's avatar

It's a bit hard to get the full blade template url.

This another solution : listen for the button click or your event and then :

 $('.container').html("@include('layout/partials/_filter')");
pmall's avatar

?!!! You just want to display / hide a part of your html ? Why using an ajax call ?

Bloomanity's avatar

Make a route that renders that partial and have that in your ajax call

pmall's avatar

That is not a good approach. Just render the partial and show/hide it.

nolros's avatar

take a look AngularJS ng-if, ng-show and ng-hide. Blade is no good for DOM manipulation. It is ok for one time render as it doing a <?php > code execution, but if you are looking for conditional post render then JavaScript is the way to go.

            <div ng-if="channel.counter > 0" class="mme-channel-console">
                <div class="channel-action">
                    <div class="channel-action-btn level">
                        <span>{[{  channel.counter }]}</span>
                    </div>
                </div>
            </div>
<div class="form bg-blue space clearfix ng-cloak" ng-cloak ng-show="setMiniControl['add']">
 // .. content
</div>

Also, Angular has a ton of conditional DOM directives, ajax, etc. It was designed for the very thing of loading partials based on any JS condition. You can also use ng-model and filter to filter content in a ng-repeat as example or can build your own filters for just about anything. You can build you own directives example that has a template in it which executes on some condition in the DOM.

No with all of that said you can get AJAX to return a string that you can then use to wrap around a placeholder object

 endMoving = function(e){
    e.preventDefault();
 var url = "http://localhost:8888/imageaction/";

    $.ajax({
     type:   "GET",
     url:    url,
     data:   {
       _token:     token
     }}).done(function(newHtml) {
       $('#placeholder').html(newHtml )
     });
  };

Laravel - have no tested pass html string back

public function getHtml()
{

      /// or however you get the html
       $html = "<div>this is a test</div>";
    return return response($html, 200);

}
JohnRivs's avatar

Or you could simple surround the partial with an if statement. Use the controller to determine if it should be visible, and pass a boolean variable (or function) to put on that if statement.

nolros's avatar

@JohnRivs, question, but for that @if to execute the entire page will need to be loaded again with the condition true? versus ajax / jquery which can with html() or wrap() manipulate that part of the DOM. Blade @if is not a DOM event based model ... unless I'm missing something?

JohnRivs's avatar

@nolros honestly I'm kinda confused about @arturo 's purpose, but looks like he's trying to change .container's content, so I assumed the view file containing .container has to be loaded first. This file would contain the if statement.

Arturo's avatar
Level 10

Sorry with the delay

y have a template in mustache in one file "list.html" Whit ajax I call a json and recall the list whit the content I change dinamic param with a onclick action and I need to charge the template


$( ".reload" ).click(function() { var filter = $(this).attr("slug"); $.get('/filter.blade.php', function(template) { $('.container').html(template); },"text"); $.ajax({ //get the subdomain dinamic url: "http://jsonUrl/dinamicParam", dataType: 'json', success: function(data) { var list = Mustache.to_html($('#list').html(), data); //#list is under container in the file list.html $('.container').html(list); } }); }

I need some type to render to URL view. I hope I explained well

Please or to participate in this conversation.