8 Replies - 752 Views - Last Post: 15 April 2020 - 03:34 AM Rate Topic: -----

#1 justawebuser   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 09-February 14

Laravel Blade - echo and stop section in a view which extends a layout

Posted 14 April 2020 - 12:32 PM

Assume that I have a page like the following

@extends('master')@section('content')

    <?php
    ob_start();

    // Some codes here

    if($condition)
    {
     ob_clean();
     echo("<div class='alert'>You're a member already</div>");
     stop_section();
    }
    // Some codes here


    echo ob_get_clean();
    ?>
 @stop


Sometimes I need to end the page and not flush the rest except the master page layout. what is buffered before could be cleaned by ob_clean(). The problem is after that. If I use "exit" or "die" I will lose @stop section and my master layout will not be loaded. So what should I do to show the message and end the page process but keep loading the master layout.

Is This A Good Question/Topic? 0
  • +

Replies To: Laravel Blade - echo and stop section in a view which extends a layout

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,066
  • Joined: 15-January 14

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 14 April 2020 - 12:55 PM

How about using else?
Was This Post Helpful? 1
  • +
  • -

#3 justawebuser   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 09-February 14

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 14 April 2020 - 01:03 PM

View PostArtificialSoldier, on 14 April 2020 - 12:55 PM, said:

How about using else?


It could get messy. In my old CMS I used to do it like this:
return "<div class='alert'>You're a member already</div>"

Was This Post Helpful? 0
  • +
  • -

#4 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 565
  • Joined: 25-August 13

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 14 April 2020 - 01:08 PM

I'm sorry, you may have your reasons, but are you absolutely sure you know how to use Blade? Wouldn't the proper way of writing this page be

@extends('master')

@section('content')
    <!-- stuff here -->
    
    @auth
        <div class="alert">You're already logged in!</div>
    @endauth

    @if (Auth::user()->isAlreadySubscribed())
        <div class="alert">You're already a member!</div>
    @else
        <div class="alert">You're not a member!</div>
    @endif

    <!-- more stuff -->
@endsection



Here's the docs, if you haven't already read them.

P.S. I don't mean to be a jerk, lol. I use Laravel daily for all my projects, so I'm passionate about it <3
P.P.S. isAlreadySubscribed() would be a method you would define in the User model, not a built-in method, just so you know

This post has been edited by Splashsky: 14 April 2020 - 01:10 PM

Was This Post Helpful? 1
  • +
  • -

#5 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 565
  • Joined: 25-August 13

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 14 April 2020 - 01:23 PM

Ah, I see what you're going for. In which case, use my example but the @else contains all the rest of your content, easy!
Was This Post Helpful? 1
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,066
  • Joined: 15-January 14

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 14 April 2020 - 02:06 PM

I wouldn't say it's messy, I'd say it's the right way to do it. Otherwise if something is happening in your code that you don't expect then instead of being able to see a clear structure about how the code runs you're going to need to search through the whole thing and any functions you're calling to check if some function somewhere is redirecting the execution in a way you don't expect. With an if/else structure it's pretty obvious what's going on.
Was This Post Helpful? 1
  • +
  • -

#7 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 565
  • Joined: 25-August 13

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 14 April 2020 - 02:20 PM

View PostArtificialSoldier, on 14 April 2020 - 02:06 PM, said:

I wouldn't say it's messy, I'd say it's the right way to do it.

Not in the context of Laravel, it's not. Errors are handled by dedicated stack trace pages, and generally execution immediately halts once the view is finished being rendered. See my reply above for how it should be handled in a Blade template.

Not to mention; he uses @stop to close a section, which isn't a built-in Blade directive. He's also using PHP tags within the template, which is redundant and bad practice. There are dedicated tags for parsing straight PHP, which are @php and @endphp

It's clear OP doesn't actually know how to use Blade, and the docs should 100% be referenced before he continues further, as his code will be incorrect, inefficient, and not work well any way if at all.

This post has been edited by Splashsky: 14 April 2020 - 02:27 PM

Was This Post Helpful? 1
  • +
  • -

#8 justawebuser   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 83
  • Joined: 09-February 14

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 15 April 2020 - 12:52 AM

As I have said in my last 2 topics I'm moving and old pure PHP project to a Laravl7.
At the moment I just want to make it work then I will keep refactoring alongside taking some advanced online courses.
Maybe I'm not well aware of Blade features but at the moment I have just 3 lines of routes in web.php for the whole project and I have only one Controller named Router. Post and Get requests both are handled inside view itself, although I know it's not the right way.

But in matters of logic, to be honest I'm not a fan of using "else" inside a function/method and I believe every blade file is the content of a view function.

Some programmers may like the following logic:


function view()
{

	if($condition)
	{
		if($condition->condition2)
		{
			if($cnd3)
			{
				//cnd3 is true
			}
			else
			{
				//cnd3 is false
			}

		}
		else
		{
	       	if($cnd4)
			{
				//cnd4 is true
			}
			else
			{
				//cnd4 is false
			}

		}

	}
	else
	{
		//cnd is false
	}
	
	return;
}


And some others prefer this:
function view()
{

	if($condition && $condition->condition2)
	{
			if($cnd3)
			{
				//cnd3 is true
			}
			else
			{
				//cnd3 is false
			}

	}
	elseif($condition && !$condition->condition2)
	{
			if($cnd4)
			{
				//cnd4 is true
			}
			else
			{
				//cnd4 is false
			}

		
	}
	else{
		
		//cnd is false
	}
	
	return;
 
}


But I like this one:
function view()
{
	if(!$condition)
	{
	  //cnd is false
	  return ;

	}
	if(!$condition->condition2)
	{
		if(!$cnd3)
		{
			//cnd3 is false
			return;
		}
		 //cnd3 is true
		 return;
  
	}
		if(!$cnd4)
		{
			//cnd4 is false
			return;
		}
		 //cnd4 is true
		 return;
	}
 
}

This post has been edited by justawebuser: 15 April 2020 - 01:06 AM

Was This Post Helpful? 0
  • +
  • -

#9 Splashsky   User is offline

  • D.I.C Addict
  • member icon

Reputation: 9
  • View blog
  • Posts: 565
  • Joined: 25-August 13

Re: Laravel Blade - echo and stop section in a view which extends a layout

Posted 15 April 2020 - 03:34 AM

Oh, believe me, I totally get moving an old project to Laravel, I've done it a few times. What I'm saying is that you want to set your structure up correctly in the first place to save yourself lots of effort and trouble later. I also pointed out how using the old school PHP tags within a Blade file since there are the @php and @endphp directives. You have @stop at the end of your section but the directive is @endsection.

My point is that you need to understand how Blade works so that you're not making simple, avoidable mistakes from the start. It looks like for this page, you're seeing if the user is already logged in - you can do this at the routing level by applying the guest middleware to the route, which you can read about at this link: https://laravel.com/.../7.x/middleware

You can save a lot of time and headache by understanding the framework you're moving your project to, and it's a waste of Laravel's features and purpose to continue as you were.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1