I cannot get laravel blade view to work, removing the blade from file name makes it work. I am using laravel 5.2.22.
Here are the details:
Here is the route:
Route::get('/', function () {
return view('login2');
});
Location and file name of 'login2': /resources/views/login2.blade.php
Inside of login2.blade.php:
<?php
echo "a";
As soon as I change the "login2.blade.php" -> "login2.php" it works.
Any ideas?
I changed rights for folder storage to 777 and now blade views work.
Try to close tag:
<?php echo "a"; ?>
Blade template files are parsed with a template engine, so you need to close tag if you're using raw PHP inside a template.
Related
I am trying to add a new view to my laravel project, and it is just dumping the entire content on the page, including my blade code, instead of executing said code. For example, my page in the browser is this:
#extends('user.layouts.app') #section('title', 'New Reminder') #section('content')
New Reminder
#csrf
Title
Content
#endsection
When it should obviously be executing that code, not putting it on the page.
I have tried clearing the routes, and researched around but no dice.
I am calling this view the same way I am calling others in my code - in the controller like this:
return view('user.partials.stat_types.new_type');
Any help or advise is appreciated!
You must have the file name with the extension .blade.php if you want Blade to be used to compile the view. With just .php the PHP engine is used.
Change your view filename to have the .blade.php extension.
I creating blade templates by builder. I make .blade file based in json config. To don't do it in fly I'd like use cached templates.
In loop I fun functions which return me pure html. I get this html and set as #section('content') in some.blade.php
To make more complicated elements like carousel I use blade. I looks like
protected function getCarousel($element)
{
if(isset($element->componentData)){
$delay = (property_exists($element->componentData,'delay')?$element->componentData->delay:5000);
$navigationType = (property_exists($element->componentData,'navigationType')?$element->componentData->navigationType:'counter');
$size = (property_exists($element->componentData,'size')?$element->componentData->size:'standard');
$images = $element->componentData->images;
$data= ['images'=>$images,'size'=>$size,'navigationType'=>$navigationType,'delay'=>$delay,'attr'=>$this->getAttributesString($element->attr)];
return view('backend::components.carousel',$data)->render();
}
}
All works perfect until I need to only one section. But I need to 2 sections:
#content and #javascript
I'd like from my backend::components.carousel file get string like
#section('content')
html
#endsection
#section('javascript')
js code
#endsection
My environment
Laravel 5.4
PHP 5.6
is it possible or I have to don't use blade to make such code?
Thanks in advance for any hints.
I have a project in laravel5 and I have directed virtual host on /public directory. Where should be my main page index.php now? There is some index.php in public folder already but it contains some content that I dont understand. So where should be my index now? Maybe in views?
If you are using Laravel 5.1 then your initial page should be this resources/views/welcome.blade.php.
You can change it to any name. But also you should change the same in your Controller.
Note : If you are rendering the view through controller then you should have the file name like this yourfilename.blade.php
Your views should always inside resources/views/*
The index file stays at the same place, to call a new file that you made, could be with HTML, you can put that file in the view and call it from the controller, hope this is the answer you are looking for.
When a request come to your server, the first file that it is executed is index.php. But this is not the file shown. In Laravel you don't have to force any file be named index.php. So let's imagine that you are trying set up a new index file. You have to use routes.php
routes.php
Route::get("/" , "HomeController#index");
HomeController.php
function index(){
return view("home/index");
}
views/home/index.blade.php
<html>
<head>
</head>
<body>
<p>Index page</p>
</body>
</html>
When a GET request to "/" it's done, the index function of the HomeController it's executed and the view index.blade.php, stored in views/home it's shown.
This it's the basic behaviour of Laravel. So you mustn't rename or move the index.php file in public folder.
I am trying to include files in my homepage using blade syntax :-
#foreach($data as $articles)
#include(app_path().$articles->path)
<br />
#endforeach
This is not working.
The error says :-
View [/var/www/blogproject/project/app/pages/articles/first-post.php] not found
I even tried including just the first page :-
#include(app_path().'/pages/articles/first-post.php')
But the normal php include is working fine :-
<?php include(app_path().'/pages/articles/first-post.php'); ?>
Please help
That's because that file is not in the app/views directory. When you call #include('filename'), Blade automatically looks for any file with that name, inside the apps/views directory. Also, you're not supposed to write the file extension, as Blade automatically looks for files with .blade.php and .php extensions.
If you want to use files from other directories on the #include tag, add the directory to the paths array, on app/config/view.php. In your case, it'd be something like this:
app/config/view.php
<?php
// ...
'paths' => array(
__DIR__.'/../views',
__DIR__.'/../pages'
);
Then, you'd call it on blade like so:
#include('articles/first-post')
I'm sure it's a simple one-liner, but I can't seem to find it.
How can I use a different layout file for a particular action?
Update: This worked for me, thanks!
// Within controller
$this->_helper->_layout->setLayout('other-layout') //other-layout.phtml
//Within view script
<?php $this->layout()->setLayout('other-layout'); ?>
From inside a Controller:
$this->_helper->layout->setLayout('/path/to/your/layout_script');
(via these docs)
EDIT: I should mention that the path is relative to whatever your layout directory is (by default, it's application/layouts/scripts/)
You can also use like this
// Within controller
Zend_Layout::getMvcInstance()->setLayout('layout_name');
//Within view script
<?php $this->layout()->setLayout('layout_name'); ?>
Your layout must be in /layouts/scripts/ folder, otherwise you need to specify the path also. No need to write .phtml, just name of the layout