how to layout inside other layout in Zend Framework - php

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

Related

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>

Template inheritance and sections without Blade syntax

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.

How to Make or Call sidebar in CakePHP

I have such type of code in view, add.ctp file in Cake PHP.
<div id="container">
<div id="content">
------------------
</div>
<div id="sidebar">
----------------
</div>
</div>
Now in Layout, in default.ctp file, we access this code by this line.
<?php echo $this->fetch('content'); ?>
I have sidebar in each and every view file, and if I need some changes then I will go in each and every file and then change.
Now My Question is that, can I made a file in layout like sidebar.ctp or any thing else that I just call this file in my view. If I can, then how I will made such type of file.
You could do it with include or elements like this
<?php echo $this->element('sidebar'); ?>
With the element, you make the sidebar.ctp file in the View/Elements/ folder.
Check for more information: Cakephp 2 Elements
The other way is with include (not my choice, but another way to accomplish it)
<?php include('../View/Layouts/sidebar.ctp'); ?>
You can use elements and if the content in elements is dynamic you can use the blocks supported in latest version of cakephp.
http://book.cakephp.org/2.0/en/views.html

Cakephp, adding a view to a sidebar

I'm struggling trying to understand cakephp's views, blocks and layouts.
I need everypage to show a left and right sidebar which content might change. At this moment I have the right sidebar defined in /pages/home.ctp but I'm guessing it would be better to extend that sidebar since it has to appear in everypage. Correct me if that thought is wrong.
Then, I have this view add.ctp for the 'usuarios' table, it practically shows the fields login and password. I want to show this view in the sidebar, but I'm really lost as how to do that.
Thanks in advance.
Lets make this thing easy. Like #patrick said, there is a lots of way.
Start with layout file. Rearrange your default.ctp layout like-
default.ctp layout
<div id="container">
<div id="header">
<?php echo $this->element('header');?>
</div>
<div id="left-sidebar">
<?php echo $this->element('left-sidebar');?>
</div>
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
</div>
<div id="right-sidebar">
<?php echo $this->element('right-sidebar');?>
</div>
<div id="footer">
<?php echo $this->element('footer');?>
</div>
</div>
Now create elements ctp files as header.ctp, left-sidebar.ctp, right-sidebar.ctp and so on and place them to app/View/Elements.
Your left-sidebar.ctp file may looks like this...
left-sidebar.ctp
// to show login form //
if you just need to show on view.ctp place few logic here for login form.
//end login form//
show other sidebar contents
There are a couple ways to do it, depending on your Cake version. If you're using >=2.1 (which I assume you are since you asked about blocks), then you should try those to see if they work for your setup. The way I usually do things is that if all views for a controller need common markup then those view files would extend a base view within the Controller directory, e.g.
#/View/Posts/index.ctp
<?php
$this->extend('_skel'); //arbitrary filename, I use '_skel' since that makes sense
echo $this->Html->para(null, 'Hello');
#/View/Posts/_skel.ctp
<?php
echo $this->Html->div('sidebar', 'Sidebar for posts...');
echo $this->fetch('content'); // This gets all output from the Posts/index.ctp view
Then all your Posts views which extend _skel will have the sidebar automatically.
Your login module might make sense as an element - something that could be used anywhere in your views.

Reusable user interface in PHP?

For most of my projects I make an administration interface, which has the same design for every project. The design of the header, the footer, the topbar, the leftmenu, the css, etc. are always the same. It is a pity to create the views every time; so I was thinking: maybe there would be a nice way to put the admin interface in my MVC library, as it is reused by every project?
But for the moment, in every single view I got code like the following:
<?php $this->_include('/includes/doctype.php'); ?>
<head>
<?php $this->_include('/includes/head.php'); ?>
<title>Some title</title>
</head>
<body>
<?php $this->_include('/includes/topbar.php'); ?>
<div id="page">
<?php $this->_include('/includes/header.php'); ?>
<?php $this->_include('/includes/leftmenu.php'); ?>
<div id="content" role="main">
<h1>Some title</h1>
<p>Blah blah blah.</p>
</div><!-- /#content -->
<?php $this->_include('/includes/footer.php'); ?>
</div><!-- /#page -->
</body>
</html>
Would it be a good idea to extract the custom content from the structure of the interface, and put that structure in my library somehow to make it reusable?
After that how will it be possible to customize the title and the actual menus?
I do this all the time. I have a custom header and footer file that are called at the start and end of every page.
<?PHP
Require("includes/header.php");
...
Require("includes/footer.php");
?>
The header provides a database handle, a datetime string and handles logon, priveleges, logging of pageviews etc.
The footer provides a standard HTML page but includes some systematised variables. It also generates the menu dynamically from the driving database then closes the database connection.
This way when I write code, I don't get mixed up in the HTML and any bugs are easy to find.
I like variables akin to:
$display_scripts - adds extra data in the head section.
$display_onload_scripts - adds onload scripts to body section.
$display_style_sheets - option to include link to additional stylesheets
$display_above_menu - will appear above the menubar. NOT recommended.
$display_below_menu - will appear immediately below the menubar.
$display_one_column - page contents when only one column is to be used
$display_left_column - page contents when two columns used. Left pane.
$display_right_column - page contents when two columns used. Right pane.
$display_footer - appears in footer division.
My main code then just has to generate the appropriate variable. Fundamentally, what you need to do is examine the source of a good age you have produced then replace the stuff you want to change with variables.
Here is a schematised version of the file I use (pseudocode) to give you an idea of how I do it.
// Code here generates the menu from database
// Code here genereates popup alert messages from other users
//permanent links to external style sheets go here.
//You can also select skins here.
<?PHP
echo $display_style_sheets;
echo "<title>".$display_page_title."</title>";
?>
<script type="text/javascript" src="JAVASCRIPT GOES HERE.js"></script>
</head>
<body <?PHP echo $display_onload_scripts;?> >
<div id="page_area" >
<div id="banner">
</div>
<?php
echo $display_above_menu;
if(!$hide_menu){echo $display_menu;} //Insert the menu variable here.
echo $display_below_menu;
?>
<div id="content_area">
<div id="inner_content">
<?PHP
if($display_number_of_columns==1)
{
echo "<div id='onecolumn'>".$display_one_column."</div>"; //I only use this one
}
if($display_number_of_columns==2)
{
echo "<div id='leftcolumn'>".$display_left_column."</div>"; //these are left for legacy support from before I got better at CSS.
echo "<div id='rightcolumn'>".$display_right_column."</div>";
}
echo "<div id='footer'>".$display_footer."</div>"; //just in case - I hardly use it.
echo $display_pop_box; //for user alert messages to other users
?>
</div>
</div>
</div>
<div id="logbox"> Automatic Logout statement</div> //this is called by JS to activate timeouts.
</body>
</html>
<?PHP
$mysqlidb->close();
?>
Sorry it's such a lot of code. The layout allows easy adaptation and makes it simple to find the offending variable if things are not going as expected. There are more elegant solutions but this works well for me and is very fast.

Categories