ZF2 view rendering in another view - php

I don't want to add child to the viewModel in action controller:
// action controller
public function indexAction() {
$result = new ViewModel();
$result->setTemplate('application/view/another-action');
$comments = new ViewModel();
$comments->setTemplate('application/view/child-comments');
$result->addChild($comments, 'child_comments');
return $result;
}
...
// View
<div>
<?php echo $this->child_comments ?>
</div>
I want to include view in another view:
<div>
<?php
$view = new ViewModel();
$view->setVariables($this->var);
$view->setTemplate('page_nav.phtml');
// here I want to render view
?>
</div>
Is it possible?

This is what the Partial view helper does (docs seem to be outdated, but I'll link them anyway here):
<div>
<?php echo $this->partial('page_nav', array('var' => $this->var)) ?>
<div>
Obviously, your page_nav should be known by your view resolver.

I know this is old, but you can also simply call $this->render('path/to/view-script')

Related

How to use renderSection in Codeigniter 4?

From the official documentation of Codeigniter 4 for view layouts there is a function renderSection that you can use at your templates.
The main problem though is that I couldn't figure out how this works. Please have in mind that I don't want a work-around for this, I really need to know how the renderSection works and what I am missing.
To be more precise:
At my Controller I have this code:
// TestController.php
...
// My method
...
$view = \Config\Services::renderer();
$view->setVar('output', $output);
return $view->render('my-main-view');
...
and my view looks something like this:
// my-main-view.php
...
<div class="main-container">
<main>
<?php echo $output; ?>
</main>
<?php echo $this->renderSection('sidebar'); ?>
</div>
...
So my question is:
Can I somehow add the data that I would like to have for the 'sidebar' from my controller? I would like to use the renderSection specifically to understand how it works.
Please do not add a work-around for this. I know that I could just have the:
$view->setVar('sidebar', view('my-view'));
and then at my template:
echo $sidebar;
The main reason that I am asking this is that we usually find work-arounds and we are missing the point of how to actually use the renderSection
Is there any kind of:
$view->setRenderSection('sidebar', 'my-view', ['data' => $data]);
that I can use? Am I missing something here?
After spending some time to understand how the section is working, I could finally find a solution to the render section.
The solution is not quite obvious thought. It seems that there isn't any setRenderSection so I did create my own to my controller. More specifically to the previous example we had:
$view = \Config\Services::renderer();
$view->setVar('output', $output);
return $view->render('my-main-view');
So I've added a function at my Controller with name _setRenderSection :
private function _setRenderSection($viewRenderer, $sectionName , $viewName, $data = []) {
$viewRenderer->section($sectionName);
echo view($viewName, $data);
$viewRenderer->endSection($sectionName);
}
So my final method will look like this:
$view = \Config\Services::renderer();
$view->setVar('output', $output);
$this->_setRenderSection($view, 'sidebar' ,'my-view', ['data' => '123']);
return $view->render('my-main-view');
I am also copying the full Controller just in case you would like to copy-paste it:
<?php namespace App\Controllers;
class Webpages extends BaseController
{
private function _setRenderSection($viewRenderer, $sectionName , $viewName, $data = []) {
$viewRenderer->section($sectionName);
echo view($viewName, $data);
$viewRenderer->endSection($sectionName);
}
public function about()
{
$view = \Config\Services::renderer();
$view->setVar('output', 'Hello World!');
$this->_setRenderSection($view, 'sidebar' ,'my-view', ['data' => '123']);
return $view->render('my-main-view');
}
}

Load View in View CodeIgniter

I want to load a view which come from logical statement (in controller) IN a view. Simply like this
Controller
public function index(){
$this->lib();
$this->view('main');
}
public function lib(){
if(TRUE){
// $this->view('something1') in $this->view('main')
}
else{
// $this->view('something2') in $this->view('main')
}
}
** View **
<html>
<body>
<!-- view from $this->view('main') -->
<!-- want to show view from lib() -->
</body>
</html>
How to show $this->view('something') get from lib() in $this->view('main') ? Any idea ?
The only way that I've found works is this:
Controller:
public function index() {
$data['internal_view'] = $this->load->view('stmt', [], true);
$this->load->view('main', $data);
}
"main" View:
<html>
<body>
<?php echo $internal_view; ?>
</body>
</html>
Note: the 3rd param in view allows for the view to be returned as a string rather than automatically outputted to the browser. Because of this functionality, you can assign a view as a return string from another function and use it to generate internal_view or whatever you decide to call it.
You could preserve the main view and also keep the lib conditional view and pass it to the main controller.
Controller :
public function index(){
$data['lib_view'] = $this->lib();
$this->view('main', $data);
}
public function lib(){
if(TRUE){
return $this->load->view('something1', true);
}
else{
return $this->load->view('something2', true);
}
}
'main' View :
<html>
<body>
<!-- view from $this->view('main') -->
<?php echo $lib_view ?>
</body>
</html>

ZF2 breadcrumbs are not working in child view

I have a breadcrumb in a child view, but it is not showing any pages.
When I show it in the root view, everything is working correct
controller:
public function categoriesAction()
{
$view = new ViewModel();
$heading = new ViewModel(array(
'headTitle' => 'Categorieën'
));
$heading->setTemplate('templates/head');
$view->addChild($heading, 'header')
->addChild($storecontent, 'content');
return $view;
}
templates/head.phtml
<?php echo $this->navigation()->breadcrumbs()->setPartial('Application/partials/breadcrumbs'); ?>
When I place the above code in the root view, it is working. But I want it in the head so I can use it in more actions.

FuelPHP basics, using Model result in View

I am a little confused using fuelPHP 1.7.
The controller
class Controller_Website extends Controller
{
public function action_index()
{
// http://fuelphp.com/docs/general/views.html
$data = Website::get_results();
//var_dump($data) // (data is found here);
$views = array();
$views['head'] = View::forge('common/head', $data);
$views['header'] = View::forge('common/header', $data);
$views['sidebar'] = View::forge('common/sidebar', $data);
$views['content'] = View::forge('common/content', $data);
$views['footer'] = View::forge('common/footer', $data);
// return the rendered HTML to the Request
return View::forge('website', $views)->render();
}
}
The model
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->execute();
return $result;
}
}
All well sofar. Data is queried and found in the controller. What I am trying to accomplish is to use the data in my:
(nested) view
<html>
<head>
<?php echo $head; ?>
</head>
<body>
<header>
<div class="container">
<?php echo $header; ?>
</div>
</header>
<div class="row">
<div class="container">
<div class="col-md-4">
<?php echo $sidebar; ?>
</div>
<div class="col-md-8">
<?php echo $content; ?>
</div>
</div>
</div>
<footer>
<div class="container">
<?php echo $footer; ?>
</div>
</footer>
</body>
</html>
Head view (nested):
<title><?php echo $title; ?></title>
Content view (nested):
<h1><?php echo $title; ?></h1>
<div class="welcome_user"><?php echo $text; ?></div>
And so on.
The variables in the view in this example are not available because they are not explicitly set in the controller. Do they have to be set explicitly or is passing the data object also possible? If so, how do I access this objects data in the right way? FuelPHP is lacking good examples here and I am stuck now.
How do I do it?
The view data is converted from array indexed to view variable named. So:
View::forge('something', array('param' => 'value'));
Will correspond to the following view:
<h1><?=$param?></h1>
Where things are going wrong is is that you pass the plain DB result to the view. You'd need to get the first result from the database result, like this:
class Website extends \Model
{
public static function get_results()
{
// Database interactions
$result = DB::select('menu', 'url', 'title', 'text')
->from('aaa_website')
->where('id', '=', 1035)
->and_where('visible', '1')
->as_assoc()
->execute()
->to_array();
return reset($result);
}
}
Note that I've first used ->to_array() to convert the result object to an array, then reset() to get the first result. I've also added ->as_assoc() to make sure you get an array result, ->as_object() would give you a stdClass instance.

Dynamic layout in Yii

I have a Yii application and in the layout I have a static sidebar. I would like to change the content of the sidebar depending on the controller action, or the view. At the moment, in the views/layouts/main.php file, I have a sidebar defined as:
<div class="sidebar">hello!</div>
When the controller is image and the action is test, I would like to display this:
<div class="sidebar">Created at <? echo $image->created_at; ?></div>
The $image object is defined in the image controller and the test action.
In short, I would like to display a sidebar when the controller is image and the action is test, however, I would not like to have a different layout entirely. How can I accomplish this?
This is work for clip. In your layout:
<div class="sidebar">
<?php if(!empty($this->clips['sidebar'])) {
echo $this->clips['sidebar'];
} else { ?>
hello
<?php } ?>
</div>
Somewhere in your view:
<?php $this->beginClip('sidebar'); ?>
Created at <? echo $image->created_at; ?>
<?php $this->endClip(); ?>
You can put a simple if-else check, using getController(), getAction() like this :
if(Yii::app()->getController()->getId()=='image' && Yii::app()->getController()->getAction()->getId()=='test'){
echo '<div class="sidebar">Created at '.$this->image->created_at.'</div>';
}
else
echo '<div class="sidebar">hello!</div>';
To access $image in a layout we need to make it a public property of the image controller itself, then in the action test where we initialize the $image, we will initialize the public $image, example:
class ImageController extends Controller{
public $image;
...
public function actionTest(){
$this->image=Example::model()->findByPk(1);// example
...
}
}
This will allow us to access image in the layout albeit using $this->image->property.
Also in the layout $this refers to the current controller, hence we can also use $this->id instead of Yii::app()->getController()->getId() to get the controller id(name which is image here) and $this->action instead of Yii::app()->getController()->getAction()->getId() to get the action id.

Categories