Yii2 - How to include view data in another view page? - yii

I am new to YII use yii2 basic. Actually I want to know how to include another view page from view of the same folder. Suppose I am in view home.php. I need to include post.php view page in home.php body. How will I perform this?

You should simply use render() :
<?= $this->render('post'); ?>
And if you need the same parameters :
<?= $this->render('post', $_params_); ?>

Related

Zend Framework 2 - Make content page variable accessable in layout.phtml

I want to build a fancy headline container which - you'll never guess it - contains the title of each page displayed.
In Django you can either create a variable in every view which is passed to the layout page or define a block which is filled with content on the content page.
Its not such a nice solution copy and pasting my fancy header on every page of my application just to put my page title in. I'd prefer to put the headline container in my layout page and use my $title variable to set the title within the container.
I found a solution for static variables here but I need a dynamic variable because the title is different for every page.
My question: Is there an easy way to pass the $title variable which I have on every content page to my layout.phtml?
Thanks!
There is a nice view Helper in ZF for this task. You can define what Title you want to use for your page in your view files.
In your view pages:
//index.tmpl
$this->headTitle("this is my unique title for this page");
In your Layout file :
<title><?php echo $this->headTitle() ?></title>
And more info at: http://framework.zend.com/manual/2.0/en/modules/zend.view.helpers.head-title.html
Ok, the answer is quite simple if you know what to look for. Use the placeholder helper.
//layout.phtml
<?php $this->placeholder('foo')->set("Some text for later") ?>
and
//myview.phtml
<?php
echo $this->placeholder('foo');
// outputs "Some text for later"
?>

Invoke action in Yii

This is my scenario. A home page with a upload form, user upload the images and submit, then the controller will catch the data from user, add it in the Model. Finally the image info will display in the another page.
I put the actionIndex() in the siteController to render the home page, the actionUpload() in the same file to handle the data user. So in the view, what should I put in the form action to invoke the actionUpload(). I think the flow is quite weird in Yii when I read the blog demo code, I just follow same way with the ASP.NET MVC. Suggest me the right way, plz. Thanks
Depends on how you build your form. If using CHtml, do the following:
<?= CHtml::beginForm($this->createUrl('site/upload'))?>
If you have a model that sits behind it:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'action' => $this->createUrl('site/upload'),
)); ?>
Please check the Yii documentation and examples on how to properly set up forms. You have several choices here.

separate header, content and footer in yii

I have a login form in my header section of the website. If user is logged in than insted of the login form user profile details will be shown. The question is how to separate header footer and content into different views and call them from one controller? Or maybe there is another solution...Thanks for help.
In your header view you could write something like this.
<?php if(Yii::app()->user->getId()): ?>
<?php $this->renderPartial('//world/_header_user')); ?>
<?php else: ?>
<?php $this->renderPartial('//world/_header_guest')); ?>
<?php endif; ?>
Using the Model-View-Controller (MVC) design pattern, the look of a Yii-based site is naturally controlled by the View files. These files are a combination of HTML and PHP that help to create the desired output. Specific pages in a site will use specific View files. In fact, the View files are designed to be broken down quite atomically, such that, for example, the form used to both create and edit an employee record is its own file, and that file can be included by both create.php and update.php. As with most things in OOP, implementing atomic, decoupled functionality goes a long way towards improving reusability. But the individual View files are only part of the equation for rendering a Web page. Individual view files get rendered within a layout file. And although I’ve mentioned layouts a time or two in my writings on Yii, it’s a subject that deserves its own post.
To be clear, layouts are a type of View file. Specifically, whereas other View files get placed within a directory for the corresponding Controller (i.e., the SiteController pulls from views/site), layout files go within views/layouts. But while the other View files are associated with individual Controllers (and therefore, individual pages), layouts are communal, shared by all the pages. Simply put, a layout file is the parent wrapper for the entire site’s templating system. I’ll explain
ypu can see more details
http://www.larryullman.com/2012/05/16/working-with-layouts-in-yii
The easiest way is probably to use a different layout, which you just switch on login. If not, showing partials / components based on Yii::app()->user->isGuest also works well.
Your default generated Yii application has a parent Controller in protected/components/Controller.php.
If you need to access additional parameters in layout, add public properties to Controller, set them in your child controller, and use them in your view/layout files.

Can I set different background color to views page dynamicaly

I'm new to drupal,
I'm getting users favorite color from cck color picker then I want to set it to the background of the profile page. Profile page i created using views. I tried to do it from crating view tpl file but no luck is any way to do that task please direct me
this is obviously not complete code, as you need to figure out how to call the field data. I haven't tested this, but in theory this should work. Add this to the .tpl.php file, and see if it works.
<?php
header('Content-Type: text/css');
/*replace cckmachinename and fieldmachinename with actual code*/;
$user_color = cckmachinename -> fieldmachinename
?>
some selector {
background-color: <?php echo $user_color; ?>;
}
Hope this helps!

how to make custom CListlView widget in yii

I am developing my web application in the Yii framework . I do not have enough experience in the Yii framework. I want to make the view for the index post page. The Yii provide the CListView for this, but I want to make some customization on that.
You can extend a widget with the following steps:
Copy CListView.php from /(yii root)/framework/zii/widgets to /(application root)/protected/widgets
Rename the file BineshListView.php
Open BineshListView.php. Add this before the class declaration
Yii::import("zii.widgets.CListView");
Change the first line of the class declaration to:
class BineshListView extends CListView { ...
Now, you have your own BineshListView class you can customize. To use it in a view, you can call it like you would CListView
$this->widget('application.widgets.BineshListView', array( 'data'=>$model, etc... ) );
Let me add that BineshListView will inherit all the properties and methods of CListView. Therefore, if you do not need to customize a property or method and want to use the original behavior of CListView, you can delete the property or method from BineshListView.
you no need to customize the ClistView . just simply make changes in the partial view file . which is called by the ClistView.
<?php
$this->widget('zii.widgets.ClistView',arrray(
'dataprovider'=>$your-data-provider,
'view-file'=>'custom-view-file'
));
?>
make changes in custom-view-file.
make sure the custom-view-file in same views folder for the controller.