Get the file name BLADE of LARAVEL - laravel

is it possible to know which BLADE page you are on? I would like to include a different menu where my VIEW is the MAIN, how can I do this in the template? I check if it is my MAIN and add my different menu there if in case. example: #if(config('page.layout') == 'top-nav');
Is that possible? but with another code, of course

To me it seems like you are thinking with the wrong way. You should know what(which controller, which method) has loaded that view instead of what is this view. And you can pass control variables from there if you want.
In you question, the example you have shown:
#if(config('adminlte.layout') == 'top-nav')
So top-nav is coming form config adminlte.layout which has no relation with blade template or anything else. You can definitely do that if you set your config file based on your intention.

I wanted to pick up which file or URL my BLADE was, I got it this way:
#if(Route::current()->getName() == 'name') ....
##codeHTML
#endif

Related

What's the alternative to Symfony render(controller) in Laravel

In Symfony I am using widget-like behaviour by calling controller method from twig
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}
It does the logic and returns another twig template, which is embeded here.
How in the Laravel do people solve this? I need this for displaying menu, responsive menu, product lists, breadcrumbs etc.
I have read about View composers and studied the documentation - but there's mentioned only how you can inject some variables into the view.
The same with using #inject() in blade.
But I want standalone widget (with own logic, data fetching...) with custom blade template embeded/inserted in any place I call them from.
Thanks for an advice.
Btw. It doesn't need to be Controller that I call, it could be a Service object. But the point is the same. I was personally calling these objects like _WidgetController (beginning with underscore - to tell me, they are not fully qualified views, but components/widgets). But I placed them into my Controllers folder.

File extension for Laravel Blade as an 'index' page

My 'homepage' for a Laravel install works great, which is: welcome.blade.php however, my question is - how do I get other basic 'static' pages to work correctly.
My preferred site top level navigation has for example, 'about us', so what I'd like to do is create a directory called 'about us' and then place a similar page as blade.index.php in there but it throws an error.
So, my question is - how do I name the file within a directory within the web application.
Thanks
You've got it flipped. blade always goes in the middle.
index.blade.php
return view('index');
/about/index.blade.php
return view('about.index');
You will need put your view file inside static folder of your choice and make sure your filename should contain blade in the middle for ex: "about.blade.php" and after that you will need to return that file with folder_name/file_name.
return view('Folder_name/File_name');
Try above code you are good to go.

Can I declare a view composer for a Form or HTML macro in Laravel 4?

I have a partial view that I am using to provide a pair of form select fields, and both sets of options depend on some variables set by a view composer for this view.
I would like to implement this view as a Form::macro instead so that I can parameterize some parts of it. However, I'm not sure how I can point the view composer at a macro instead of a partial view. Is this even possible?
I could go the route of pointing the composer at any view that I'm using the macro in, but I'd much rather have the option data load automatically any time I use the macro as it is a common component in my web app.
I use partials for some Bootstrap UI components, and create a macro to connect data into the template, so I only use HTML::macro or Form::macro to access them:
Form::macro('myMacro', function($data) {
return View::make('myPartial', $data);
};
This doesn't really answer the primary question, but one way I can get what I want is to use View::Make instead of #include in my views.
Where I was using:
#include('thepartial')
I can use something like this instead:
{{ View::make('thepartial')->with(array('param1'=>$param1, 'param2'=>$param2, ...) }}
This way I get the parameterization I want, and I still get the composer's effects.

How to change load layout in Joomla view?

By default parent::display($tpl); loads components/com_my_component/views/my_component/tmpl/default.php, but in some cases i need to load other php file which is in the same folder near default.php (for example components/com_my_component/views/my_component/tmpl/lol.php). How to do this from view.html.php.
P.S.
Tried load loadTemplate and setLayout methods with no luck.
Solved the problem by myself. Need to use the method setLayout and pay attention to the input syntax
$this->setLayout('dafault:lol');
parent::display($tpl);
By default, joomla looks for the layout keyword in the URL to decide which layout to display. If this variable is empty or not present then the tmpl/default.php layout will be loaded.
By editting your view.html.php file you can set the default layout by using the JView API, e.g. $this->setLayout('lol') will make the URL example.com/yourview equivalent to example.com/yourview?layout=lol.
However, this change alone will result in Joomla overriding it's default behaviour so that the layout request will be ignored. This means that the request example.com/yourview?layout=lmao will also display example.com/yourview = example.com/yourview?layout=lol
You can solve this easily by adding a condition around the setLayout function so that only if the layout keyword is not present then you will set the default layout to lol, e.g.
<?php
# ...
function display($tpl = null) {
# ...
# Edit : Set the default layout to 'lol'
$layout = JRequest::getWord('layout', '');
if (empty($layout)) $this->setLayout("lol");
// Display the view
parent::display($tpl);
}
# ...
I keep coming back to this and I've yet to find a satisfying solution.
What does work, from J1.5 right up to J3.4, for me has always been to set the $tpl variable in view.html.php
If $tpl is empty or "" then tmpl/default.php is displayed by default.
If you change $tpl to a string, e.g. $tpl="stacker" then it will look for and display tmpl/default_stacker.php
I've seen various differing theories on changing it earlier in the MVC so that it doesn't need the default_ pretext. e.g. tmpl/stacker.php
None have worked for me.

Problem with passing non-literal text to the Html.Partial() extension method

I would like to pass the file name of a partial view as data retrieved from the viewbag as such:
<div id="Zone1">#Html.Partial(ViewBag.ZoneControl1)</div>
Where the "ZoneControl1" property of the ViewBag is the name of the desired partial view retrieved from elsewhere (i.e. database, web service, etc.). If I include the text as a literal string i.e.:
<div id="Zone1">#Html.Partial("Controls/MyPartial")</div>
It works just fine. If I pass that literal string as a property of the ViewBag from the controller, or even just create a variable in the consuming view, i.e.:
#{string zone1 = "Controls/MyPartial";}
<div id="Zone1">#Html.Partial(zone1)</div>
It doesn't work. The page appears to be loading but never displays anything in the browser. Again, this works fine if I hardcode the partial view name, but not if it is passed as data from a variable or property. Anyone know why this is happening? If this is intended or unavoidable behavior, is there a workaround?
You can't use dynamic in Html.Partial (which is what ViewBag is) because it accepts only strings. One quick way around this would be to cast your ViewBag.ZoneControl:
#Html.Partial((string)ViewBag.ZoneControl1)
As for the second part (zone1 = "Controls/MyPartial") I was unable to duplicate that.
The following code is what I wrote to test it and it works just fine.
#{ string zone1 = "Controls/MyPartial"; }
<div>#Html.Partial(zone1)</div>
I assume the answer with casting the ViewBag is what you're really looking for in this case.
Well, I have it working now and I'm not exactly sure what fixed it. I copied the razor code\markup and deleted that view and created a new view and pasted in the old code. The only difference was that when I created the new view, via the wizard, I specified to NOT use a master page and the resulting page had code to specify:
#{
Layout = null;
}
The original page was created using a master page and then I changed my mind and took out the layout directive entirely. Anyway, after making those changes, it WORKED! So, I initially surmised that the reason was that a view must specify "layout = null" if not using a master page. BUT, I then took out the "layout = null" code in this new page and it still worked! So... not sure what went wrong, but to sum up:
As #BuildStarted correctly noted, you can use a property of the ViewBag object as the partial view path, but you must cast it as a string for it to work properly. So, the premise for this question was incorrect and something else was mucking things up. Just not sure what.

Resources