Template inheritance and sections without Blade syntax - php

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.

Related

php-cs-fixer doesn't indent HTML inside PHP control structures

I'm trying to use php-cs-fixer with a WordPress project, which means I (unfortunately) have files with a mix of PHP and HTML. I'm using the #PSR12 ruleset.
I'm having trouble with getting HTML within PHP control structures to indent correctly. Take this example snippet:
<?php if (!empty($related_posts)) : ?>
<div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
<h3 class="has-text-align-center">Related <?= esc_html($title) ?></h3>
</div>
<?php endif ?>
php-cs-fixer reformats it to:
<?php if (!empty($related_posts)) : ?>
<div class="module--related_posts alignfull has-2-columns has-hover-state slider-on-mobile">
<h3 class="has-text-align-center">Related <?= esc_html($title) ?>
</h3>
</div>
<?php endif ?>
Note the closing h3 tag has been moved to a new line, and the first-level indent within the if statement body has been removed.
The h3 issue I can live with, as this is resolved if I put the opening tag on its own line:
<h3 class="has-text-align-center">
Related <?= esc_html($title) ?>
</h3>
...but the lack of indent within the if statement is going to do my head in. The same thing happens with for and while statements.
Is there a rule in php-cs-fixer that I've overlooked that will resolve this?
The Answer of keradus on the "PHP code does not align with html code" issue:
PHP CS Fixer was never written with supporting mixed html/php file.
Some fixers are supporting one php part and one html part inside single file, but not big mix of them, like in template files.
If we would like to support template files, we would need to not only detect and track concrete fixers, but also provide some big integration test (like we do for Sf ruleset) that it remains to work for most important rules.
Before that happen, I would not claim that we officially support html/php mixed-files.
And in another answer:
we do not aim to fix mixed file (PHP and HTML in single file)
Not really an answer but I ended up just switching to PHP_CodeSniffer to get around this.

Assign php code to smarty template engine

I make a new website for my company and i want to use smarty (v3.1.29) for it. Now the problem is that we store the code for all pages in our database (home, products, downloads, ...). Some pages contains PHP inline functions like:
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
In my template (.tpl) file I have a div-section to load the content from our database:
<html>
<body>
<div id="content">
{$content}
</div>
</body>
</html>
So my code looks like this afterwards:
<html>
<body>
<div id="content">
<?php include("functions.php"); ?>
<p> Hello <?php echo printWorldInColor(); ?> </p>
</div>
</body>
</html>
Is there a way that smarty processes the PHP-code before parsing it?
Hint: I dont want to edit my template file. I just want to parse the database content to my content-section of the website.
What i tried:
saved database-content into a string and replaced PHP-tags with smarty-PHP-tags, then assign it to the template
SmartyBC-Class
You can't do that in Smarty. Also running php code stored in the database sounds like a terrible idea. But if for some reason you have to go on with this nonsense (and considering that you can't use eval), you can try this:
Read the php code from the database.
Save to a temporal php file
Turn on output buffering with ob_start()
include the file you have created
assign the output to a variable with ob_get_clean()
assign the variable to the template
But if I was you, I would try to do the project in another way.

how to insert php code in one-page bootstrap template

actually am working on a php website, and am finished with it, and I want to make a good style to my project, I found many templates and am interested to “one-page bootstrap templates”, i have downloaded ones and don't know how to use them, I want to put my php pages in one of them but I don't know how to do that.
if you are coding php without any framework and template engine you can combine php and html( here bootstrap template) like this :
<html>
<body class="container">
text
<?php if $list=true ?>
<ul>
...
<ul>
<?php endif ?>
</body>
<html>

Laravel 4 - including a "partial" view within a view (without using Blade template)

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

how to layout inside other layout in Zend Framework

I want to do this:
master.phtml
<html>
<body>
<?php echo $layout;?>
</body>
</html>
layout.phtml
<div class="grid_3">
<?php echo $content;?>
</div>
view.phtml
<?php
$this->loadCustomLayout('layout.phtml');
?>
the content
then... the master are "master.phtml"... in layout goes the content of "layout.phtml"... and... inside the "content" goes the content of "view.phtml"
is possible do this ?
thanks.
You can use a partial to do that, instead of nesting Layouts...
Here is a feature request submitted to the Zend Issue Tracker for this functionality. There is a patch suggested and provided to Zend_Layout which provides this functionality, but it is not yet part of the Zend Framework. Go vote for it to be added!
http://framework.zend.com/issues/browse/ZF-8013
You could also try this approach:
http://www.developly.com/creating-3-step-layouts-with-zendlayout

Categories