i have figured out some blade templates (laravel) like #section('title', getOption('app_name') . ' - Login')
#section('body') as its real php formis <?php $__env->startSection('title', getOption('app_name') . ' - login'); ?>
<?php $__env->startSection('content'); ?> . and these are correct ,,
But i wanna know the real php form of #extends(layouts.app) .
anybody got an Idea ?
All of your compiled blade views are stored in storage/framework/views/ if you want to dig around and try to understand how Laravel turns a blade template into php.
It looks like your views define the sections first, then pass them to the layout
as variables. Inside your view which #extends(layouts.app) you might see this at the end:
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render(); ?>
The sections were defined before that line and passed as variables. When you look inside the layout itself, you'll see things like this for including each of those sections:
<?php echo $__env->yieldContent('content'); ?>
Related
I am using CakePHP 3.0 and I am using inside the Cell/Header directory a display for the header.
<div id="header">
<div class="header-title">
<h2><?php echo (isset($pageTitle)) ? $pageTitle : 'Test'?></h2>
</div>
</div>
This code snippet runs perfectly and on every page.
I am trying to set the $pageTitle dynamically. This means that I am trying to set it for every page differently.
I have set inside the UsersController.php
$this->set('pageTitle', 'Nebojsa');
In index method.
But when I go to the url /users/index , this page title stays 'test'.
I have also tried to assign the value inside the Template/Users/index.ctp
<?= $this->assign('pageTitle', __('Users')); ?>
But it won't work.
What am I doing wrong ? Could you maybe point me in the right direction, where should I look ?
Between view templates and layouts you can use:
<?= $this->assign('pageTitle', __('Users')); ?>
And get the value back in any other template or layout using:
<?= $this->fetch('pageTitle') ?>
As far as setting the title from the controller your code is correct.
$this->set('pageTitle', 'Nebojsa');
<?php echo (isset($pageTitle)) ? $pageTitle : 'Test'?>
How to structure view hierarchy without using blade? What are the pure php counterparts of blade directives (i,e #section, #extend , etc)?
Perhaps, something similar to <?php extend('foo') ?>
In Phalcon framework, while it has its own template engine (Volt) all of its template engine is also available in pure PHP syntax.
Since Blade directives just compile to normal PHP, it is technically possible to use the view structuring features without actually using Blade. I don't think it's very pretty though, and I personally would think twice about this decision.
You can find all the PHP code, Blade is compiled to, in this class:
Illuminate\View\Compilers\BladeCompiler
Here are some of them:
#section('content')
<?php $__env->startSection('content'); ?>
#endsection
<?php $__env->stopSection(); ?>
#extends('layout')
This is a bit a tricky one. Usually Blade compiles it and then adds it to a footer variable which is printed at the bottom. So instead of putting it at the top (like you would with #extends) you have to place this at the end of your view:
<?php echo $__env->make('layout', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
#yield('content')
<?php echo $__env->yieldContent('content'); ?>
To put this in a pure PHP way you'll have to check out the storage/framework/cache/views and see what's happening there. Basically, is what Blade compiles to PHP code (instead of using # and with proper function calls).
One way I can think is:
In your template where you use yield:
<!-- template.php -->
<div class="container">
<!-- instead of using yield('container') -->
<?php echo "_yield:container"; ?>
</div>
In your file, instead of using section and stop
<!-- view.php -->
<!-- instead of using extend('template') -->
<?php $templatePath = 'template.php'; ?>
<?php $sections = []; ?>
<!-- instead of using section('container') -->
<?php $currentSectionName = 'container'; ob_start(); ?>
<p>This will be in my container div</p>
<!-- instead of using stop -->
<?php
// get the current html
$sections["_yield:".$currentSectionName] = ob_get_contents();
ob_end_clean();
ob_start();
require($templateName);
$template = ob_get_contents();
ob_end_clean();
echo str_replace($template,array_keys($sections),array_values($sections));
?>
Of course, this approach is simplistic at best. The code provided is not intended as a copy & paste solution, more like the concept.
Everything else is simple:
#foreach($arr as $k=>$v)
...
#endforeach
translates to
<?php foreach($arr as $k=>$v) : ?>
...
<?php endforeach; ?>
That's how it's exactly done by the BladeCompiler. The same is with if and while.
The pure PHP equivalent to Blade is to split your code in sections like header and footer (for example) and then use require in your page to blend those sections in the corresponding place.
<?php
require("template/header.php");
// Here goes the body code
require("template/footer.php");
?>
There is no pure PHP functions that i can think of, to extend a page from a main template, a you do using the yield directive.
Blade compiles into PHP every time and what it compiles is stored in to storage/framework/views/*
The following link is a list of all things blade can compile to, you should be able to extract some knowledge out of this:
https://github.com/illuminate/view/blob/master/Compilers/BladeCompiler.php
The general idea for most templating engine is that they structure your code like so:
if ($condition):
// do stuff
endif;
while ($condition):
// do stuff
endwhile;
foreach ($array as $key => $value):
// do stuff
endforeach;
For further reference, see https://secure.php.net/manual/en/control-structures.alternative-syntax.php
Neither of blade directives is a 'pure' PHP function. PHP functions cannot start with # and all blade directives do. In short, blade directives are shortcuts or synonyms to PHP built-in functions or control structures.
You are free to use any other template engine – it doesn't have to be Blade. Blade is built-in, but you are not locked to it. Just install a vendor package or make your own one, and return your HTML output with response, instead of using View facade.
Basically in default.ctp I have this for my title:
<title>
<?= $this->fetch('title') ?>
</title>
And inside of the controller I have this line:
$this->set('title', 'Test-Title');
But it does nothing, it still displays controllers name(Jobs, controllers full name os JobsController.ctp)
But if I put this inside of my view file:
$this->assign('title', 'Test-Title');
It changes the title. So what is wrong with $this->set('title', $title) ?
fetch() returns the contents of a block not a variable. Using set() in your Controller is setting a variable that can be output in your View templates by echoing the variable:-
<?php echo $title; ?>
If you want to use fetch() you need to use it in combination with assign() in the View templates to define the block. For example in your View template use:-
<?php $this->assign('title', $title); ?>
And then in the layout template:-
<title><?php echo $this->fetch('title'); ?></title>
In CakePHP 3 the idea is to set the page title by assigning it in the View as it relates to the rendering of the page. This differs from how this was originally handled in CakePHP 2 where you'd define title_for_layout in your controller and then echo the $title_for_layout variable in the layout template (this was deprecated in favour of the CakePHP 3 approach in later versions of Cake 2).
You can just set() the variable in your controller:
// View or Controller
$this->set('title', 'Test-title');
Then use it as a standard variable is in your layout or view:
<!-- Layout or View -->
<title>
<?php echo $title; ?>
</title>
Details here: http://book.cakephp.org/3.0/en/views.html#setting-view-variables
Using assign() is different, which is why it works with fetch(). assign() is used with View Blocks: http://book.cakephp.org/3.0/en/views.html#using-view-blocks
In CakePHP 3 layout template, make sure to set the title as below.
<title>
<?= $this->fetch('title') ?>
</title>
Then in your view:
<?php $this->assign('title', 'Title Name'); ?>
This is the way CakePHP will use its built-in View classes for handling page title (view blocks) rendering scenarios.
Just for completion, I came across a situation where a malformed .js script with undefined variables referenced between <head></head> resulted in the <title></title> tags being posted to DOM (showed in page source) but Chrome, Firefox and (from memory) MSIE all failed to deliver the content of the title to the APP UI, again from memory - iOS mobile was unaffected.
If you want stick to your code, after setting "title" variable just simply write this:
<?= __('Main Project Name') ?>
<?php if( isset($title)) $this->assign('title', $title); ?>
<?= ' - ' . $this->fetch('title') ?>
I have done this, this way in default.ctp
<?php
$cakeDescription = __d('cake_dev', 'Your Title');
?>
<title>
<?php echo $cakeDescription ?>: <?php echo $title_for_layout; ?>
</title>
In my view file, I have done this.
<?php $this->assign('title', 'Your Title');?>
This works fine using php echo
<!-- app/views/example.blade.php -->
<p><?php echo $taylorTheVampireSlayer; ?></p>
The below code out puts the double brackets.
Seems like the blade templating system isn' working on my localhost/lampstack.
I have checked all permissions and tried the original code to
<!-- app/views/example.blade.php -->
<p>{{ $taylorTheVampireSlayer }}</p>
Try using these!
Blade::setContentTags('{{', '}}'); // for variables and all things Blade
Blade::setEscapedContentTags('{{{', '}}}'); // for escaped data
Hope this helps! happy larveling :)
I had the same issue and it has been solved by changing the name of the views by adding " .blade " between "name" and ".php" like Eluong said.
name.php : issues
name.blade.php : solved
In Laravel 3, I used to do this.
<?php render('partials.header'); ?>
This was done in "PHP" views, without using Laravel's Blade templates.
What's the equivalent of this in version 4?
I tried
<?php #include('partials.header'); ?>
This doesn't work.
If I do
#include('partials.header')
I have to save my file as ".blade.php"
How do I include a "subview" without using the blade template?
There are different ways to include a view within a view in Laravel 4. Your choice will depend on any one of the outcomes outlined below...
For Flexibility
You can compile (render) the partial views in the appropriate Controller, and pass these views to the Main View using the $data[''] array.
This may become tedious as the number of views increase, but hey, at least there's a lot of flexibility :)
See the code below for an example:
Controller
...
public function showMyView()
{
/* Header partial view */
$data['header'] = View::make('partials.header');
/* Flexible enough for any kind of partial views you require (e.g. a Header Menu partial view) */
$data['header_menu'] = View::make('partials.header_menu');
/* Footer partial view */
$data['footer'] = View::make('partials.footer');
return View::make('myView', $data);
}
...
View
You can include the partials above as follows (at any position in your View code):
<html>
<head></head>
<body>
<!-- include partial views -->
<?php echo ($header) ?>
<?php echo ($header_menu) ?>
<div id="main-content-area"></div>
<?php echo ($footer) ?>
</body>
</html>
Your partial views will now be added to your main View.
For Simplicity
There's actually a much easier way than using the method above: Simply include this in the html of the view...
View
<html>
<head></head>
<body>
<!-- include partial view: header -->
<?php echo View::make('partials.header') ?>
<div id="main-content-area">
</div>
<!-- include partial view: footer -->
<?php echo View::make('partials.footer') ?>
</body>
</html>
Make sure that the folder structure for the partials is [views/partials/header.php] in order to provide the correct file-path to the View::make() function of Laravel.
WARNING
If you try to pass the $data['page_title'] in a controller, the nested views wont receive the data.
To pass data to these nested views you need to do it like this:
<html>
<head></head>
<body>
<?php
/* Pass page title to header partial view */
$data ['page_title'] = "My awesome website";
echo View::make('partials.header', $data);
?>
<div id="main-content-area"></div>
<?php echo View::make('partials.footer') ?>
</body>
</html>
NOTE
The question clearly stated: "Without using Blade template", so I have made sure to give a solution that does not include any Blade templating code.
Good luck :)
You can nest your partials in views try this
View::make('folder.viewFile')->nest('anyname', 'folder.FileName');
Then access the nested view file from your template {{ $anyname }} this way you don't have to include files in your view and this should work for .php file also.
I am not sure how many people have been using Laravel 4 in this post, since this post, but if you are looking to include partials or separate your view types you can do it with #includes
for example, if you want a partials folder for your header, footer, sidebar etc
create a directory for the partials under
app/views/partials
Then create a partial
app/views/partials/navigation.blade.php
Then in your master template file add the line
#include('partials.navigation')
That is all it takes.
** Bonus you can also pass data to a partial or include nested partials within a partial
I know this is a bit of a late answer, but I figured since I didn't see this solution amongst the other answers it was ok.
If you want to include your header and footer on every page I would add them into the before and after filters. Just go to filters.php in your app folder
App::before(function($request)
{
echo View::make('partials.header');
});
App::after(function($request, $response)
{
echo View::make('partials.footer');
});
When doing it this way you don't need to add anything in the view files to include them.
You can use View's nest function
View::make('default.layout')->nest('header', 'default.header');
Use the third parameter to pass data to the template
View::make('default.layout')->nest('header', 'default.header', ['name' => 'John Doe', 'test' => 'It works!']);
on your views/default/header.blade.php
<div>hey {{ $name }}! {{ $test }}</div>
I am still pretty new to Laravel, but I think the below is pretty ideal ...
Route::get('/', function()
{
$data['title'] = 'sample';
return View::make('page', $data);
});
# /views/partials/header.php
# /views/partials/footer.php
View::composer('page', function($view)
{
$view->with('header', View::make('partials.header', $view->getData()));
$view->with('footer', View::make('partials.footer', $view->getData()));
});
See Laravel View Composers .. http://laravel.com/docs/responses#view-composers
/views/page.php
<?php echo $header; ?>
<div>CONTENT</div>
<?php echo $footer; ?>
From within a view, just echo the other view:
echo View::make('header'); //This will look for a view called header.php