Support the ongoing development of Laravel.io →
posted 10 years ago
Views Blade
Last updated 2 years ago.
0

You can send the name with a view composer.

View::composer('*', function($view) { 
   $view->with('viewName', $view->getName());
});
<div id="{{ $viewName }}">
    @yield('content')
</div>
Last updated 2 years ago.
0

Appreciate the response. This only seems to work if you actually put the {{ $viewName }} call directly within the view's php file. The problem I am running into is that I am essentially using this within a header element which is inside of my default.blade.php. Because of this, it just kicks back:

<div id="layouts.default">

For example, say I wanted my menu-bar to echo back the name of the page I was currently on within the header (that's across all pages).

With the method that you mentioned, it only displays it properly IF the {{ $viewName }} is called within a view's page and not if it's within the layouts.default page being used in the header/footer.

Also,

Do I need to put

View::composer('*', function($view) { 
   $view->with('viewName', $view->getName());
});

inside each and every one of my public function calls for each page within PagesController.php? or is there a way to call it across every single page right at the top?

Last updated 2 years ago.
0

Create a file called composers.php inside app folder (same level with routes,filters) and copy the code inside it. Edit app/start/global.php and add:

require app_path().'/filters.php'; // This was already here
require app_path().'/composers.php'; // Add this line

You don't need to call it. As you can see the first parameter is *. It means send viewName to every view. I believe you have to set which views should rendered with $viewName. For example, just send it to header.

View::composer('header', function($view) { 
   $view->with('viewName', $view->getName());
});

If your header file is inside a folder, for example views/partials/header.blade.php, It should be partials.header. The first parameter also accepts array. You can send it to multiple views.

Last updated 2 years ago.
0

Great, I have what you said working, but there are just a few problems. If I actually call it directly from a view that has a few levels then it pushes back the entire folder structure with it:

<div id="portfolio.applications.wdcs">

Instead of just the end part (the .php file's name):

<div id="wdcs">

Is there a way to call the base name so it just pulls the file name and not the entire folder structure with it?

And unless I'm misunderstanding something, If I'm using the call in my partials.header it's just echoing out the actual file that it's being called from (partials.header) instead of the actual view of the page that the user is on (about).

e.g. when I am on my about page that uses the header, I have:

<span>{{ $viewName }}</span>

and it pushes back:

<div id="partials.header"> instead of <div id="about">

Can I have it in a header/footer file and have it ignore the name of that partials file but instead pull in the actual view that is being used? Like on my old site before switching to laravel, I used the basic PHP function to check the current URL to get it:

{{basename($_SERVER['PHP_SELF'], ".php");}}

To call in the name of the current php page and display it in the header of the website.

I'm sure I'm over-complicating this, does what I said make any sense?

Thanks so much for sticking this out with me, already made so much progress and cleaned up my code a lot so far!

Last updated 2 years ago.
0

This will give you the name without structure. You can also use substr etc.

pathinfo($viewName,PATHINFO_EXTENSION);

I usually use route names for this stuff or do things manually and I've never dealt with View that much :) We are using one variable for view name ($viewName) and it's overwritten in every nested view. Maybe you should use multiple composers with different variables. One for layouts and one for nested parts.

Last updated 2 years ago.
0

Ahh okay, I'll look into having multiple composers with different variables! That makes a lot of sense.

About your solution really quick, (then I'll mark this thread as answered) where exactly would I put:

pathinfo($viewName,PATHINFO_EXTENSION);

I'm assuming in the composers.php, but how exactly would it fit in there? Everything I have attempted brings up the debug screen. I am going to read a book on Laravel to understand the structure better, I apologize for this, just a little bit more guidance would be greatly appreciated! Right now it just consists of the:

<?php
  View::composer('*', function($view) {
    $view->with('viewName', $view->getName());
  });
?>
Last updated 2 years ago.
0

To obtain only the base file name, try :

View::composer('*', function($view) {
    $viewName = end( explode( '.', $view->getName() ) );
    $view->with('viewName', $viewName);
});
Last updated 2 years ago.
0

Thanks for the attempt! It throws this error though:

ErrorException
Only variables should be passed by reference
Last updated 2 years ago.
0
View::composer('*', function($view) { 
   $view->with('viewName', pathinfo($view->getName(),PATHINFO_EXTENSION));
});
Last updated 2 years ago.
0

You're the best! Got absolutely everything working perfectly by using both of the formats in two separate ways like you explained to me. I only call to the folders that will have pages inside of them when removing all of the pathinfo:

View::composer(['portfolio.*','blog.*'], function($view) {
  $view->with('viewName', pathinfo($view->getName(),PATHINFO_EXTENSION));
});

By targeting ONLY the pages within portfolio and blog, it displays the proper echo without all of the file-paths.

Now all I need to do is use the other command to target all root pages within the views folder, OR do all pages EXCEPT for those in the "/layouts" folder, "/portfolio", and "/blog" folder (assuming the first option would be easier). I can of course just call every single page individually but it would require me to have to update it each time I added a new page page into my views root:

View::composer(['about','blog','contact','index','portfolio'], function($view) {
  $view->with('viewName', $view->getName());
});

Thanks so much for everything! Got it working EXACTLY how I was trying to get it to work, just need to find a way target only the pages within the root of the views folder.

Last updated 2 years ago.
0

Update:

View::composer('*', function($view) {
  $view->with('viewName', last(explode('.', $view->getName()))); 
});

This is all that's needed for everything to work, using explode() you do not need to manipulate the pages separately that are within folders.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

abass abass Joined 6 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.