How to implement a sidebar in Zend Framework - model-view-controller

How do I implement a sidebar in Zend Framework?
I know that I can use a placeholder or something similar in Zend_layout, but how do I automatically generate the code for the sidebar in my controllers without having to call a sidebar class within every controller?
My setup is as follows
Application
- modules
- blog
- other modules
I only want the sidebar for my blog module.
I have found this http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-layout-sidebar-etc-2677.html but I do not understand the last part "just inject your layout, register it with the front controller ..."

You could just have a action and view in one of your controllers which renders the sidebar.
from the layout for the blog module you just call:
<? echo $this->action('action','controller','module',array('optionalparams'=>1); ?>
on the position where you want to have it. So one call to one action.

Rather than use the action stack and the action() view helper, you could render a "partial view script" that includes your sidebar elements.
# in your layout.phtml
<div id="sidebar">
<?php echo $this->render('blog/_sidebar.phtml'); /*relative to your view scripts directory*/ ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
The render() view helper is used to render the content of another view script. It has the same scope as all your other view scripts, so if there are any variable assigned to the view, they will be available to your partial. So in the example above, the categories variable was set in the controller.
There is another view helper called the partial() view helper. This function is a little more expensive since it creates its own variable scope. In other words, none of your current view variables will be available. You will have a clean slate to work with, which means you must pass in any variables you need:
# in your layout.phtml
<div id="sidebar">
<?php echo $this->partial('blog/_sidebar.phtml', array('categories2'=>$this->categories)); ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories2 as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
I don't find myself using partial() very often since it is more expensive, and I rarely need to create a separate context.
As far as setting up the variables for use in the sidebar partial ($this->categories in this example), I have used a number of different methods depending on the particular problem. If it's specific to a controller action, I will write the code and assign it in the view script:
# controller
public function somethingAction()
{
$this->view->categories = $this->_getCategoriesForThisParticularAction();
// other controller code
}
If my code is more generic to all the actions of the controller, I will utilize the controller's preDispatch() function. If it's more generic to multiple controllers, I will put the code in the init() of my base controller (a controller the most of my controllers extend).
Sometimes I do not even put the code in my controller. If it's simple enough, I just stick the code in the partial. If it's a little more complex, I will move it to a view helper. This may break the MVC pattern, but I think it really depends on the particular case in order to determine the best placement.

If you are using Zend_Layout, just add the sidebar with the Action viewhelper as Rufinus said.
in your layout script:
<div id="sidebar">
<?php echo $this->action('action', 'controller', 'module', array('optionalparams'=>1)); ?>
</div>
<div id="content">
<?php echo $this->layout()->content; ?>
</div>
This should meet the requirements posted in your question.

Related

Strange behavior when returning view with custom Blade directive in Laravel

Currently I am working on a project where views need to be rendered through a custom Blade directive. However I came across a few (limitations?) errors I cannot solve (for a long time).
My custom Blade directive with tree different ways to output a view.
Blade::directive('lwField', function ($expression) {
// 1
return view('lw::module.field.field')->render();
// 2
return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
// 3
return Blade::compileString('#include("lw::module.field.field")');
});
The field view:
Get to the choppa!
The main view:
#extends('layout.default')
#section('main_content')
#lwField()
#endsection
The layout file:
// ... some cool html
#section ('main_content')
#show
// ... even more cool html
The following happens when I execute the three methods separately:
1 return view('lw::module.field.field')->render();
The fist time it executes it throws an error:
include(/somepath/storage/framework/views/b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php): failed to open stream: No such file or directory (View: /somepath/resources/views/page/default.blade.php)
This is true since there is only one view in my cached folder:
// 5b27802352643346357e49b847d934736c36cd07.php
// The main view with the field view
<?php $__env->startSection('main_content'); ?>
Get to the choppa!
<?php $__env->stopSection(); ?>
The second time I run this it magically works. It will generate 3 other files:
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// the main view with yielding layout view without field view
<?php $__env->startSection('main_content'); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.default', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
// e4cec91e7d4adb5dac5c63e5bfa85ba9a258f664.php
// the layout file
// ...
<?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); ?>
// ...
// ffd60653d8490007c272c527abef3a5ede092a33.php
// layout view and main view
// ...
<?php $__env->startSection('main_content'); ?>
<?php echo $__env->yieldSection(); ?>
// ...
It looks like it generates two files that are the same but one without the field view.
2 return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
3 return Blade::compileString('#include("lw::module.field.field")');
These methods will return a blanko page and generate two files in cache without showing/logging any errors.
// 5b27802352643346357e49b847d934736c36cd07.php
// Field view
Get to the choppa!
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// Main view and field view
<?php $__env->startSection('main_content'); ?>
<?php echo view("lw::module.field.field")->render(); ?>
<?php $__env->stopSection(); ?>
This method will not generate the layout file.
Does anyone have a clue on what is happening. Maybe this kind of behavior is not strange at all and this is how Blade should work.
I have been working on a project that required me to do the same thing, the problem is that making a new Blade directive is not a good practice when you want to render another view, It's because of the Cache, every Blade directive is being cached so if you want to render dynamic data it will cause you a lot of problems.
Since I have tried a lot of options and you want to render another view file and not just push html to the blade directive, I suggest you to make a new helper or even a service that you can pass there the parameters and it will return the render function that outputs clean HTML so this way you can be sure that your content will be always dynamic and won't be cached.
TL;DR
Example:
In your helpers functions file put:
function render_my_view() {
return view(''lw::module.field.field'')->render();
}
So then in yout blade file you can use:
{!! render_my_view() !!}

October CMS - sorting records - example of the partial for the toolbar icons?

I'm excited that the October CMS recently added back-end functionality for sorting records in the list view. But I'm having some trouble getting it to work. The documentation is here. I've followed the direction like so:
In my controller, I implemented the ReorderController:
<?PHP namespace BTruchan\Team\Controllers;
use Backend;
use BackendMenu;
use BackendAuth;
use Backend\Classes\Controller;
use System\Classes\SettingsManager;
class Members extends \Backend\Classes\Controller
{
public $implement = [
'Backend.Behaviors.FormController',
'Backend.Behaviors.ListController',
'Backend.Behaviors.ReorderController'
];
public $formConfig = 'config_form.yaml';
public $listConfig = 'config_list.yaml';
public $reorderConfig = 'config_reorder.yaml';
public $requiredPermissions = ['btruchan.team.manage'];
public function __construct()
{
parent::__construct();
BackendMenu::setContext('BTruchan.Team', 'team');
}
public function index()
{
$this->makeLists();
$this->makeView('reorder');
}
}
?>
I've created the reorder view file (reorder.htm) which contains:
<?= $this->reorderRender() ?>
My config_reorder.yaml file contains:
# ===================================
# Reorder Behavior Config
# ===================================
# Reorder Title
title: Reorder Members
# Attribute name
nameFrom: name
# Model Class name
modelClass: BTruchan\Team\Models\Members
# Toolbar widget configuration
#toolbar:
# Partial for toolbar buttons
# buttons: reorder_toolbar
You'll notice that the reorder_toolbar partial is commented out. That's because I really don't know what's supposed to go in that toolbar. I haven't been able to find any documentation that shows the contents for the _reorder_toolbar.htm file.
Unsurprisingly, with the code commented out, it throws an error:
Undefined variable: reorderToolbarWidget
Some additional information:
It was suggested that I read up on list toolbars here.
So I added the following toolbar partial (named _reorder_toolbar.htm):
<div data-control="toolbar">
<a
href="<?= Backend::url('btruchan/team/members/create') ?>"
class="btn btn-primary oc-icon-plus">
New Team Member
</a>
<button
class="btn btn-default oc-icon-trash-o"
disabled="disabled"
onclick="$(this).data('request-data', {
checked: $('.control-list').listWidget('getChecked')
})"
data-request="onDelete"
data-request-confirm="Delete Team Member: Are you sure?"
data-trigger-action="enable"
data-trigger=".control-list input[type=checkbox]"
data-trigger-condition="checked"
data-request-success="$(this).prop('disabled', false)"
data-stripe-load-indicator>
Delete
</button>
</div>
But I'm still getting an error:
Undefined variable: reorderToolbarWidget
/var/www/terrasearch/public/modules/backend/Behaviors/reordercontroller/partials/_container.htm
line 1
The code, in October CMS, which that error message is referring is:
<?php if ($reorderToolbarWidget): ?>
<!-- Reorder Toolbar -->
<div id="<?= $this->getId('reorderToolbar') ?>" class="reorder-toolbar">
<?= $reorderToolbarWidget->render() ?>
</div>
<?php endif ?>
<!-- Reorder List -->
<?= Form::open() ?>
<div
id="reorderTreeList"
class="control-treelist"
data-control="treelist"
I've tried to trace this error down. It seems like, in \public\modules\backend\behaviors\ReorderController.php, the reorder() function is not being called, which means that the prepareVars() function is also not being called. This prevents the following code from being executed:
$this->vars['reorderToolbarWidget'] = $this->toolbarWidget;
ReorderController.php:: makeToolbarWidget() is being called and seems to be OK. I've checked $this->toolbarWidget, and it seems to contain perfectly good data. (It isn't NULL).
The ReorderController is a behavior, so it's meant to be called as a controller destination (e.g. example.com/backend/btruchan/team/members/reorder). It's not coded to be called as a view the way you have it in your index function.
In the ReorderController source, the reorder function is the only method that calls the prepareVars protected function which is the only place that the reorderToolbarWidget is defined for the page. That prepareVars function isn't available from the host controller.
So, rather than try to create a view with $this->makeView('reorder');, create a toolbar button in the _list_toolbar.htm partial that points to the reorder destination url. For example:
<div data-control="toolbar">
New Member
Reorder Members
</div>
When you click on the "Reorder Members" button, you'll be directed to a new page with the records that can be reordered.
You can use the _reorder_toolbar.htm partial to add anything you want at the top of the reorder page. Or, not use it at all.

Load a view inside another view

I've been using django for some time and I decided to start a new project but this time in Codeigniter, I used to extend the template file in my views and put content inside the {% block content %} block but it seens to be different in CodeIgniter.
In CodeIgniter I have something like:
<?php
$this->load->view('header');
$this->load->view('form_add_customer');
$this->load->view('footer');
?>
But is there a way to have an unique file with header, content and footer like this?
<html>
<head><title>Test</title></head>
<body>
<div id="header">Welcome</div>
<div id="content">
</div>
<div id="footer"> 2013 - Tectcom Telecom</div>
</body>
</html>
And put a view file with a form inside the content div?
Update your html (layout) file like this:
<div id="content"><?php $this->load->view($content) ?></div>
In your controller, call the view like this:
$view_data = array();
$view_data['content'] = 'form_add_customer';
$this->load->view('path/to/layout', $view_data);
I've used the "Most Simple Template Library for CodeIgniter" in the past with success for smaller projects. I believe it'll provide with the functionality that you require allowing you to have placeholders in a 'template' which you can update in your controller logic.

joomla category list override to add id to each list item

I am trying to add a custom style to each list item within Joomla!s Category List output which gives me the following html
<div class="blog">
<div class="cat-children">
<ul>
<li class="first">
<span class="item-title">HYT
</span>
</li>
<li></li>
</ul>
</div>
</div>
I think what I need to do is add something like:
<li id="myID<?php echo $this->item->catid; ?> ">
The trouble is I can't find which file to override. I have looked in /templates/mytemplate/html/com_content/category/ as well as /components/com_content/views/category/tmpl yet none of the files seem to have a an unordered list within them that relates to cat-chidren.
So my first question is which file should I edit? And my second is what is the best syntax of this->item->[correct'Method'?] (is method the correct term or variable, I'm a little shaky on this!) to use so that each list item will have an id="myID[nameofarticle/subcatagory]"
You'll see cat-children in /components/com_content/views/category/tmpl/default.php
The ul is in another loaded subtemplate, loadTemplate('children'); ?> , i.e.
/components/com_content/views/category/tmpl/default_children.php
If you want to modify the li class you could stick something like this at line 26 (of your override not core file - but fine to just test on a core file)
<?php $class = ' class="cmyId' . $this->escape($child->title) . '"';?>
That would make each li appear as
So this
<li<?php echo $class; ?>>
<?php $class = ''; ?>
becomes this
<?php $class = ' class="cmyId' . $this->escape($child->title) . '"';?>
<li<?php echo $class; ?>>
<?php //$class = ''; ?>
Have tested it out on a 2.5 installation.
You should override several files stored in components/com_content/views/
Depending on the list you want to edit, you should then look in the folders:
- article
- category
- categories
- featured
In each of these folders you'll see a subfolder called 'tmpl', inside which there is a 'default.php' file. That's what you're looking for.
If you want to override the files remember that the best practice is to place the alternative files in your template's folder, building a similar path as the one in which the original file is (e.g. for the article folder: templates/YOURTEMPLATEFOLDER/html/com_content/article/default.php - NO tmpl folder needed, nor views folder).
An alternative and, in my opinion, easier way could be setting different templates for each category, and then assigning to each the list styles you prefer.
Or, even easier, you could simply edit your index.php file in the template's folder so that it echoes a specific css stylesheet depending on the $catId.
As your HTML code shows you are using Category Blog view, each article instance in the category is being rendered by blog_children.php file, as it shows /components/com_content/views/category/tmpl/blog.php
<?php echo $this->loadTemplate('children'); ?>
So /components/com_content/views/category/tmpl/blog_children.php is the file you need to edit or override in template html directory.
Then you can apply custom styling adding an id or class for each article with $child->id.

How to theme views fields in Drupal 7 correctly

I need to theme views in Drupal 7. There is a content type 'Book' and I need to list 5 books and theme them in special manner(preview image, title and author).
When I override views-view-field.tpl.php and print raw SQL result, I see that all fields are displayed. This code
echo "<pre>";
print_r($row);
echo "</pre>";
gives
[entity] => stdClass Object
(
[title] => ...
....
[nid] => 34
...
[body] => Array
...
But I don't want pass [body] from database to php side, because it can be huge and cause a performance issue. I haven't selected [body] in view settings.
Is there a way to pass only certain fields to views-view-field.tpl.php?
Thanks in advance.
The variables available are written in the documentation inside the sites/all/modules/views/theme folder's files.
Usually, the variable you need to look at and modify on a views-view-fields.tpl.php template is $fields
I use the devel module (http://drupal.org/project/devel) to view the variables available:
<?php
//after enabling the devel module...
dpm($fields);
// This will print a Kuomo display on the page with the array's vars
?>
Generally, on a view of nodes,
<?php print $fields['title']->content; ?>
will print the node title. For fields, try
<?php print $fields['field_FIELDNAME']->content; ?>
If you have the memory, you can capture ALL vars available on the template in the Kuomo with
<?php dpm(get_defined_vars()); ?>
Make sure you clear your cache before you try to view the vars.
If what you want to do is theme a certain field you can create a template for that specific field like this one: views-view-field--field-nameofmyfield.tpl.php place it in your theme folder and rescan the templates in the Theme:information part of the View configuration.
For that to work you have to have the field added to Fields in the View.
To sort through your information in a theme use this:
<?php dpm ($rows); ?> // View all the information in the view
<?php foreach ($rows as $row_count => $row): ?>
<?php print $row['title'];
<?php print $row['nid'];
<?php endforeach; ?>
If you want to change of theme of view then Change views-view-fields.tpl.php like this:
<div class="pagecontent">
<div class="colleft">
<?php if($fields['field_file']->content){ ?><div class="views-field-file"><?php print $fields['field_file']->content; ?></div><?php } ?>
</div>
<div class="colright">
<div class="views-field-title"><?php print $fields['title']->content; ?></div>
<div class="views-field-body"><?php print $fields['body']->content; ?></div>
<div class="views-field-view-node"><?php print $fields['view_node']->content; ?></div>
</div>
</div>

Resources