CakePhp: About Views - php

What is the difference between to these in cake php?
$this->fetch('title');
$this->extend('/Common/view');
$this->element('shop/cart.ctp');

fetch() is used mainly in layout file to output a block of content.
extend() is used to extend view. which allows you to wrap one view in
another.
element() is used to group an reusable piece of view.

You can learn lots from this online book/page:
http://book.cakephp.org/2.0/en/views.html
View Templates
Extending Views
Using view blocks
Displaying blocks
Using blocks for script and CSS files
Layouts
Using layouts from plugins
Elements
Passing Variables into an Element
Caching Elements
Requesting Elements from a Plugin
Creating your own view classes
View API
Hope it will help you!

Related

yii what are the difference between begincontent() renderpartial()?

They seem to be doing the same thing: they are passing layouts to a view.
I am new to Yii and I do not know what the difference is. Can anyone explain it?
renderPartial() - renders a named view without any layout. If you don't have any layout and want to render a named view (a view previously stored for reuse) you can use renderPartial().
beginContent() - is for managing nested layout. Inside a layout you can reuse a named view like a nested layout .
See this for a brief guide.

Add property to an existing object in twig

I am about to build a modular page theme in grav (which uses twig as a template engine).
By default, a modular theme consists of at least two twig template files: one provides the page frame including the head and another one that provides the markup of a content module. Grav's standard theme provides examples of these templates:
modular page frame example
module example
As you can see, the module (example) does not have a head or javascripts block to add javascripts to the html head. (This wouldn't even be possible, as a typical modular page consists of multiple modules. So if two different modules would try to change the javascripts block the last one would overwrite the changes made by the first one which would lead to unintended results).
However, it happens that I need to add some javascript code to the head of my page from within a module. I thought about a solution and had the idea to add the javascript-code as a property to an object that exists "site wide", namely the pages object.
This leads to my question:
How can I add properties to an existing object using the twig syntax?
Apart from that I would also like to know if you can think of any reasons why this solution could possibly not work. But if you help me with the syntax, I can also just give it a try...
Manipulating the state of an object shouldn't be done in a Twig template. The view should be outputting data that has been supplied to it.
try to change the javascripts block the last one would overwrite the changes made by the first one which would lead to unintended results
See this answer here for an explanation of how to have multiple blocks append content to a block rather than overwrite it. You can simply have a 'extra_scripts' block or something like that, then append any required javascript for a particular module to that block as per the described link. Essentially you need to use {{ parent() }} to ensure content is included from the parent

CakePHP: content appended to blocks from a view loading first

I'm using view blocks to allow individual views to add scripts and stylesheets that are specific to those views. The resources that are used everywhere are included by default in the layout. Here's the documentation on view blocks.
Everything works as described, but the content appended in my views was getting added BEFORE the default block content (even though I was using $this->append('block name')). After a lot of aggravation, I deduced that the views were actually getting referenced first, before the layout. So the scripts I was loading in the view were getting appended to nothing. Then the layout's scripts were getting appended afterwards.
I was able to solve this problem my removing my global scripts OUTSIDE of the block. But I don't like this approach for a couple reasons. In the future, I may want my view to be able to overwrite certain things. Also, it just seems wrong and counter to what view blocks are meant for.
My questions:
1. Is the behavior I described the intended behavior?
2. Is there a better work-around to manage script dependancies from scripts appended from views?
I think blocks should not be defined in layouts, that's not the way it is intended for, it should be defined in views or elements, appended by other views or elements, and then fetched by layouts or views.

Implementing master pages functionality. PHP

How to make master pages in php? Like a Layout.cshtml (and RenderBody()) in ASP.NET MVC?
Thanks!
P.S. Maybe there's a third-party tool for the purpose?
EDIT
Ok. The thing is not about MVC architecture! Do look here: http://jsfiddle.net/challenger/8qn22/109/
I want the master page/layout to stay when user gets redirected to the other page
Want an average page to be nested inside the content division. So if it is a form I want this form to be displayed like: http://jsfiddle.net/challenger/XgFGb/17/
Here's what PHP standard framework/api supports:
The require("/definitions.php") function loads class, function and constants defines from a file and outputs the content outside of PHP code to php://stdout (on a webserver this is what is sent to the browser). You might wanna use require_once for importing dependencies (php files with definitions).
Use the PHP's open and close tags to obtain something close to templating functionality.
For example a normal page would look like this:
while an included (and repeatably includable) one could look like this:
I'm not saying "don't use templating engines", just showing a clear and simple way of achieving things which PHP is purposely built for. If this is enough for you needs, I then do say "don't use templating engines for the sake of it" (btw, if you are tidy, you can easily separate logic from views, without strict and sometimes cumbersome MVC frameworks).
Off hand, I know that the Laravel framework includes the Blade templating engine. It uses a syntax very similar to Razor.
Example:
#layout('master')
#section('navigation')
#parent
<li>Nav Item 3</li>
#endsection
#section('content')
Welcome to the profile page!
#endsection
(Razor, Blade, loller skates)
In PHP, there is a very similar technique called templating. Instead of a master page, you have a template. The language itself has no built-in templating features, but there are third-party templating engines (Smarty, PHPTAL, and XTemplate, to name a few).
If you want to have "real" master pages, it is entirely possible to implement them. Just wrap you master page into a class and include() that class into your content pages.
Also Zend Framework supports a two step view, where a view template is rendered inside a layout template. I think this satisfies your need for master pages.
See following links:
Approximating Master Pages in PHP
Is there anything like MasterPages on CodeIgniter Framework?
PHP Equivalent of Master page in ASP.NET
ASP.Net MVC or Zend Framework. What is your opinion
http://hoolihan.net/blog-tim/2008/09/24/simple-masterpages-with-php/
The Twig template engine offers Template Inheritance
The most powerful part of Twig is template inheritance. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override.
Can be used as a standalone, but was made by the people behind the popular symfony framework.
A while ago (several years), I achieved something like this using Smarty, and extending it to contain a method to the effect of DisplayMaster("NameOfTemplate", "NameOfMasterTemplate")
It works by rendering a template and passing the result into another (master) template.
Above has 2 templates:
NameOfTemplate, just has the main content section, e.g.
<div>...{$someProcessing}</div>
NameOfMasterTemplate has the outer html
<html>...<body><div class="layout">{$innerHtml}</div></body></html>
Ok. The solution that suits me the best is described here http://www.phpro.org/tutorials/Introduction-to-PHP-templating.html#9. It is easy, fast to imlement and doesn't enforce you to use a templating engine. Cool!
Add this piece of code in the content area of your master.php file...
I am using it like this and it perfectly working for me
<li>BLOG</li>
<div class="container content">
<?php
if(isset($_GET['page']))
{
$page_name = $_GET['page'];
include("/".$page_name);
}
?>
</div>

Joomla: including php-files renders jdoc-tags unusable

I'm trying to set up a Joomla-template in a way which allows me to use template parameters to choose between a number of layout variations of the template from the admin-interface. The idea is that the index.php only contains a statement which includes a php-file from a folder within the main template folder, based on the template parameters passed.
Unfortunately it seems thatmy jdoc module positions breaks whenever they are located outside the index.php file, even though it is referenced by an include-tag.
The problem is in the order that the framework builds index.php. Rather than trying to use include files, you can accomplish the same goal in a much easier and cleaner fashion. Using a combination of collapsible module positions and CSS you can manipulate the template to do basically anything you want.
You can also use template styles if you are using J1.7. A template style is basically a variation of a template that is assigned to specific menu items. Either way will work.

Categories