Partial view inside a View? - php

In Zend Framework, is it possible to include (call) a Partial view from within a View?
Cheers,

From the Docs for Partial View Helper:
The Partial view helper is used to render a specified template within its own variable scope. The primary use is for reusable template fragments with which you do not need to worry about variable name clashes. Additionally, they allow you to specify partial view scripts from specific modules.
You would then call it from your view script using the following:
<?php echo $this->partial('partial.phtml', array(
'from' => 'Team Framework',
'subject' => 'view partials')); ?>
If you dont need the separate variable scope, you can simply call render('script.tpl') or include.

you can do that with the Action View Helper
<?php echo $this->action('list','comment'); ?>
Where list is the action and comment is the controller
More details http://framework.zend.com/manual/en/zend.view.helpers.html

Related

Symfony: passing variables in a template without rendering it

I have a base twig template that is being extended by all the user twig templates, I have a main navigation in the base template that contains the profpic of the user, I want to pass the profpic variable to base template while rendering the index template of user via same controller.. how do I do this?
Please help..
You want make that ?
$html = $this->renderView('AccueilBundle:Reservation:billet.html.twig', array(
'reservation' => $reservation));

Laravel 4 Blade #include variable

I was trying to do include with Laravel blade, but the problem is it can't pass the variable. Here's my example code:
file_include.blade.php
<?php
$myvar = "some text";
main.blade.php
#include('file_include')
{{$myvar}}
When I run the file, it return the error "Undefined variable: myvar". So, how can I pass the variable from the include file to the main file?
Thank you.
Why would you pass it from the include to the calling template? If you need it in the calling template, create it there, then pass it into the included template like this:
#include('view.name', array('some'=>'data'))
Above code snippet from http://laravel.com/docs/templates
Unfortunately Laravel Blade engine doesn't support what you expected!.But a little traditional way you can achieve this!
SOLUTION 1 - without Laravel Blade Engine
Step a:
from
file_include.blade.php
to
file_include.php
Step b:
main.blade.php
<?php
include('app/views/file_include.php')
?>
{{$myvar}}
SOLUTION 2 with Laravel Blade Engine
routes.php
$data = array(
'data1' => "one",
'data2' => "two",
);
View::share('data', $data);
Access $data array from Any View like this
{{ $data['data1'] }}
Output
one
Blade is a Template Engine for Laravel. So try passing the value from the controller or you may define it in the routes.php for testing purposes.
#include is used to include sub-views.
I think you must understand the variable scope in Laravel Blade template. Including a template using #include will inherit all variables from its parent view(the view where it was defined). But I guess you can't use your defined variables in your child view at the parent scope. If you want your variable be available to the parent try use View::share($variableName, $variableValue) it will be available to all views as expected.
In this scenarion $myvar would be available only on the local scope of the include call.
Why don't you send the variable directly from the controller?
I suggest you do a classic PHP require if you really want to change your variable (then it's automatically by reference)

what is headMeta() in zend?

I am a beginner of Zend framework. I am just practicing with few tutorial projects. In some project I have found the below codes in layout.phtml but I don't understand what is the purpose of these codes.
<?php echo $this->headMeta(); ?>
<?php echo $this->headTitle(); ?>
Please explain the above two lines.
Thanks
Enamul
Both helpers are explained in detail in the ZF reference guide on View Helpers:
HeadMeta Helper
The HTML element is used to provide meta information about your HTML document -- typically keywords, document character set, caching pragmas, etc. Meta tags may be either of the 'http-equiv' or 'name' types, must contain a 'content' attribute, and can also have either of the 'lang' or 'scheme' modifier attributes.
See http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headmeta
HeadTitle Helper
The HTML element is used to provide a title for an HTML document. The HeadTitle helper allows you to programmatically create and store the title for later retrieval and output.
See http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headtitle
Both of them are placeholder helpers:
The Placeholder view helper is used to persist content between view scripts and view instances. It also offers some useful features such as aggregating content, capturing view script content for later use, and adding pre- and post-text to content (and custom separators for aggregated content).
The main idea is to have a container, which you can fill with data and then echo at some later point in your view template, e.g. with the headMeta helper you can configure various meta keywords to be inserted in your website and with the title helper you can configure the title element of the page. when you echo the helpers, they will echo their collected data all at once in a formatted way.
Please refer to the reference guide for further information.
I suggest you to start accepting some questions first before asking
<?php echo $this->headTitle(); ?> //This will be in your layout/phtml file,giving the title
<?php echo $this->headMeta(); ?> // Giving any meta info
The purpose of adding this is say you have two controllers FooController and BarController .You want to give title foo to the webpage while executing foo controller
Class FooController extends Zend_Controller_Action {
public function init(){
$this->view->headTitle('FOO');
}
}
In the same way you can give different title for another controller also
Class BarController extends Zend_Controller_Action {
public function init(){
$this->view->headTitle('BAR');
}
}
Same applies for Meta also
Its is helper class:
HeadMeta Helper
The HTML element is used to provide meta information about your HTML document -- typically keywords, document character set, caching pragmas, etc. Meta tags may be either of the 'http-equiv' or 'name' types, must contain a 'content' attribute, and can also have either of the 'lang' or 'scheme' modifier attributes.
HeadTitle Helper

What is Layout and what is View in ZF? When and whose variables should I use and why?

I can't understand when to use Layout's variables and when to use View's variables to get page segments on the page. Here is the picture form their Layout package tutorial ($this means the View instance everywhere):
Why Navigation, Content and Sidebar segments are got as Layout variables?
$this->layout()->nav;
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
$this->headTitle(); // I know that this is a placeholder view helper.
// But this segment of the page logically belongs to Layout.
// and it has to be called smth like view->layout->placeholder
And why Header and Footer are from some partial method of the View but not Layout's properties?
$this->partial('header.phtml');
I've tried to change them and both ways work fine:
echo $this->nav; // I assigned navigation segment script to the View and it works;
I tried to assign Footer segment script to the Layout and it also works:
$layout->footer = $footer;
echo $this->layout()->footer; // it also works, it's displayed on the page
Any of the ways may be applied to any variable on the page. For example in Navigation segment I have a lot of variables to display and I can output them using both ways - one variable as Layout's property, another one sa View's property.
So what is the rule to use them right way? When should I use View's variables and when Layout's ones?
I agree that this isn't very clear from the documentation, and I don't think $this->layout()->nav is explained at all. A few points that might help:
$this->layout() is actually a call to the layout view helper, which returns the current instance of Zend_Layout.
Zend_Layout registers its own placeholder helper (with the key 'Zend_Layout'), and by default creates a 'content' variable in this.
the Zend_Layout class has a magic __get() method which proxies any member variable calls over to its registered placeholder container. So calling $this->layout()->content is another way of writing $this->placeholder('Zend_Layout')->content
the Zend_Layout class also has a magic __set() method that proxies stored data to the placeholder class. So $layout->footer = 'foo' is the same as calling $this->placeholder('Zend_Layout')->footer = 'foo' in the view
With that in mind:
Why Navigation, Content and Sidebar segments are got as Layout variables?
As these are accessing data stored in Zend_Layout's placeholder. You could also use $this->placeholder('Zend_Layout')->content
But HeadTitle, HeadScript, HeadStylesheet are got straightly from View?
These are view helpers.
And why Header and Footer are from some partial method of the View but not Layout's properties?
This is the standard way of accessing content from other templates.
In general, assume that using the view object is the correct way to access the data. Use the layout object/helper only if you know the data is in the layout placeholder.
The advantage of using placeholders over partials is that you can access and modify them in several different places, including in the view itself. For example say you had a sidebar which is stored in a partial. If you were to store this in the Zend_Layout placeholder instead (for example in a controller plugin), you can then override this for certain actions in the controller:
public function someAction()
{
$this->view->layout()->sidebar = 'Some other sidebar content';
}
or in the view script itself:
<?php $this->layout()->sidebar = 'Content for this page only'; ?>

Zend_Layout with partial view - set value through Index Controller

I think I can't see the tree in the wood.
I'm using Zend Framework, with an layout.phtml which is rendering and partial
<?php echo $this->partial('_header.phtml') ?>
My goal is to render an form from my IndexController into the "_header.phtml" with
<?php echo $this->form; ?>
How can I pass the form to the partial view?
View partials are rendered with a clean variable scope... That is, they do not inherit view variables from the calling Zend_View instance.
There's a few options available to you here:
One, simply call:
echo $this->render('_header.phtml');
instead of using a partial. This file will have access to all your view variables, so you can just assign the form to your view in your controller, like anything else.
Another way is to explicitly pass your form as a variable to the partial, like so:
echo $this->partial('_header.phtml', array('form' => $this->form));
// $this->form inside your partial will be your form
Your other option is to either use placeholders, or layout response segments. Here's an example of placeholders:
In your _header.phtml, or layout... where ever you want the form to render:
<?php echo $this->placeholder('header'); ?>
And in your controller:
$this->view->placeholder('header')->append($form);
// I'm not sure, but you _may_ want to pass in $form->render() here.
// I can't remember if implode() (which is used in placeholders internally)
// will trigger the __toString() method of an object.
This has the added bonus of not polluting your view instance with one-off variables, like the form.
Note: I'll link to the manual pages as soon as the ZF site is back up; 1.9 launch is today, so the site's getting updated currently.
Here's some relevant manual pages:
Placeholder view helper
Partial view helper

Categories