Cake PHP adding links to tree output - php

I am trying to add links to a Tree output list.
In addition to the links I get the &nbsp's included in the output
So that it looks like this:
**My Categories
Fun
Sport
Surfing
Extreme knitting**
etc.....
I don't want that obviously, but I do want to keep the nested output relationship.
Below is code:
Controller
<?php
class CategoriesController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('output', $this->Category->generateTreeList(null, null, null, ' '));
}
}
?>
View
<?php foreach ($output as $data): ?>
<ul>
<?php echo $this->Html->link($data,
array('controller' => 'data', 'action' => 'view', $data)); ?>
</ul>
<?php endforeach; ?>
<?php unset($data); ?>

You should use a tree helper to output your tree as ul/li including links.
See http://www.dereuromark.de/2013/02/17/cakephp-and-tree-structures/
generateTreeList() - as documented - is a quick way to create a dropdown select ready list, not a tree.

Related

ZF2 add params to setPartial submenu

I use this partial to generate my submenu.
<?php foreach ($this->container as $page): ?>
<?php foreach ($page->getPages() as $child): ?>
<a href="<?php echo $child->getHref(); ?>" class="list-group-item">
<?php echo $this->translate($child->getLabel()); ?>
</a>
<?php endforeach; ?>
<?php endforeach; ?>
Which is called like this:
$this->navigation('navigation')->menu()->setPartial('partial/submenu')->render();
But when i render the menu the "$child->getHref()" renders the url without the needed "slug/id" parameter.
I tried to create the url with "$this->url()" in ZF1 you could pass the params in an array to the partial but in ZF2 that doesn't seem to work anymore.
Can anybody tell me how to add the params to the menu urls?
Thanks in advance!
PS!
I'm not referring to $this->Partial, i'm talking about $this->navigation('navigation')->menu()->setPartial('partial/submenu')->render() which apparently doesn't support a param array.
If I'm understanding your question, yes, you can pass params to partials. Example:
<?php echo $this->partial('partial.phtml', array(
'from' => 'Team Framework',
'subject' => 'view partials')); ?>
See http://framework.zend.com/manual/2.3/en/modules/zend.view.helpers.partial.html
I'm not sure this completely solves your issue, since you are not showing what the menu helper is. Is it your own view helper? Are you saying that setPartial method only accepts one argument?
All that said, have you considered Spiffy Navigation?
https://github.com/spiffyjr/spiffy-navigation
It's been sometime since this question was asked, however today I came across the same problem (using version 2.4).
If you have a segment route to be included within the menu that requires some parameters there is no way to pass these through to the navigation's view partial helper.
The change I've made allows a ViewModel instance to be passed to the menu navigation helper's setPartial() method. This view model will be the context for the navigation's partial template rendering; therefore we can use it to set the variables we need for the route creation and fetch them just like within other views using $this->variableName.
The change requires you to extend the Menu helper (or which ever navigation helper requires it).
namespace Foo\Navigation;
use Zend\Navigation\AbstractContainer;
use Zend\View\Model\ViewModel;
class Menu extends \Zend\View\Helper\Navigation\Menu
{
public function renderPartial($container = null, $partial = null)
{
if (null == $container) {
$container = $this->getContainer();
}
if ($container && $partial instanceof ViewModel) {
$partial->setVariable('container', $container);
}
return parent::renderPartial($container, $partial);
}
public function setPartial($partial)
{
if ($partial instanceof ViewModel) {
$this->partial = $partial;
} else {
parent::setPartial($partial);
}
return $this;
}
}
Because this extends the default implementation of the helper updated configuration is required in module.config.php to ensure the extend class is loaded.
'navigation_helpers' => [
'invokables' => [
'Menu' => 'Foo\Navigation\Menu',
],
],
The menu helper will then accept a view model instance.
$viewModel = new \Zend\View\Model\ViewModel;
$viewModel->setTemplate('path/to/partial/template')
->setVariable('id', $foo->getId());
echo $this->navigation()
->menu()
->setPartial($viewModel)
->render();
The only change in the actual partial script will require you to create the URL's using the URL view helper.
foreach ($container as $page) {
//...
$href = $this->url($page->getRoute(), ['id' => $this->id]);
//...
}

CakePHP and conditions

So in my controller MenuController.php I have the following code:
class MenuController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('menus', $this->Menu->find('all'));
$userSpecific = $this->Menu->find('all', array(
'conditions' => array('Menu.user_id' => '20')
));
}
}
and in my view, I am doing the following:
<?php foreach ($menus as $menu): ?>
<?php echo $menu['Menu']['id']; ?>
<?php echo $menu['Menu']['user_id']; ?>
<?php endforeach; ?>
update
To better understand this in my browser I changed my view to the following:
<?php foreach ($menus as $menu): ?>
<p>Menu id <?php echo $menu['Menu']['id']; ?> is for user: <?php echo $menu['Menu']['user_id']; ?></p>
<?php endforeach; ?>
end update
Now in the view, it is currently using the $this and returning all values from the database table, How do I change the view to use $userSpecific rather than $this I managed to get this far (making the $userSpecific part) by using the cookbook but I could only find the controller side and not the view side. I'm sorry if it's a bad question, just trying to learn.
You need to send the data to the view from the controller at the end of the index() function.
You can do it like this:
$this->set('userSpecific', $userSpecific);
or like this (my preferred way)
$this->set(compact('userSpecific');
Once you've done this, you can then modify your view to show the user specific fields as shown:
<?php foreach ($userSpecific as $menu): ?>
<?php echo $menu['Menu']['id']; ?>
<?php echo $menu['Menu']['user_id']; ?>
<?php endforeach; ?>

YII CListView How to add id to itemwrapper

In CListView there is a property called "itemsCssClass" which basically adds HTML class attribute to the Itemwrapper.
What if I like to add an ID or any other htmlOptions how will I do it on that wrapper..?
My code is :
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->search(),
'viewData'=>array('x'=>''),
'itemView'=>'_classifieds',
'id'=>'boa_ads',
'itemsCssClass'=>'test'
));
This code will produce this HTML:
<div id="boa_ads" class="list-view">
<div class="summary">Displaying 1-4 of 4 results.</div>
<div class="items"><!-- HERE ID LIKE TO ADD AN ID-->
----ITEMS GOES HERE ----
</div>
</div>
</div>
Thanks for your help in advance
From the Yii's source:
public function renderItems()
{
echo CHtml::openTag($this->itemsTagName,array('class'=>$this->itemsCssClass))."\n";
.....
echo CHtml::closeTag($this->itemsTagName);
}
I just see class attribute is passed to the itemsTagName so you probably have to extend CListView to do it.
You can create a CCustomListView class (inside application/widgets folder) which extends from CListView and overwrite renderItems() function. For example:
<?php
Yii::import("zii.widgets.CListView");
class CCustomListView extends CListView
{
public $itemsHtmlOptions;
/**
* Renders the data item list.
*/
public function renderItems()
{
echo CHtml::openTag($this->itemsTagName, array_merge(array('class'=>$this->itemsCssClass), $this->itemsHtmlOptions))."\n";
$data=$this->dataProvider->getData();
if(($n=count($data))>0)
{
$owner=$this->getOwner();
$viewFile=$owner->getViewFile($this->itemView);
$j=0;
foreach($data as $i=>$item)
{
$data=$this->viewData;
$data['index']=$i;
$data['data']=$item;
$data['widget']=$this;
$owner->renderFile($viewFile,$data);
if($j++ < $n-1)
echo $this->separator;
}
}
else
$this->renderEmptyText();
echo CHtml::closeTag($this->itemsTagName);
}
}
In your view, you can use it like:
<?php $this->widget('application.widgets.CCustomListView', array(
'dataProvider'=> 'your_data_provider',
'itemsHtmlOptions' => array('style' => 'color:blue', 'id' => 'your_id'),
'itemView'=>'your_item_view',
'template'=>'your_template',
)); ?>
So the style which in itemsHtmlOptions will be applied for the listview.
This link is also useful for you: How to extend CListView in order to remove extra yii added markup?

Codeigniter HMVC - Two modules passing the same variable name to their views and causing conflict

I have two modules in my installation. Both modules' controllers pass a variable called
$data['content']
to their views. Also, the first module's view runs the second module via
<?php echo Modules::run('module2'); ?>
and after that is supposed to display values from its $data['content'] variable. Unfortunately, that's when the first module's $data content is substituted with the second module's $data. This is pretty inconvenient for me, so I would like to know if there is a way to "protect" the $content variables and keep them only within their associated modules?
I would like to avoid renaming $data['content'] if possible. I've found a not-so-perfect solution in using
$data(__CLASS__)
but I am curious if it is possible not to change $data['content'].
class Some extends MX_Controller{
public function __construct(){parent::__construct();}
public function index(){
$this->load->view('template', array( //Primary template
'content' => 'some_index' // index view
));
}
public function _module_1(){
$this->load->view('module_1_view', array( //Module View : NO primary template
'' => '' <= no need to load view here, only data
));
}
public function _module_2(){
$this->load->view('module_2_view', array( //Module View : NO primary template
'' => '' <= no need to load view here, only data
));
}
}
-
template.php
<html>
<?php $this->load->view($content); ?>
</html>
-
some_index.php
<html>
//call modules
<?php echo Modules::run('some/_module_1'); ?>
<?php echo Modules::run('some/_module_2'); ?>
//call module from another class
<?php echo Modules::run('another_class/_module_1'); ?>
</html>

Best Practices: What's the Best Way for Constructing Headers and Footers?

What's the best way for constructing headers, and footers? Should you call it all from the controller, or include it from the view file? I'm using CodeIgniter, and I'm wanting to know what's the best practice for this. Loading all the included view files from the controller, like this?
class Page extends Controller {
function index()
{
$data['page_title'] = 'Your title';
$this->load->view('header');
$this->load->view('menu');
$this->load->view('content', $data);
$this->load->view('footer');
}
}
or calling the single view file, and calling the header and footer views from there:
//controller file
class Page extends Controller {
function index()
{
$data['page_title'] = 'Your title';
$this->load->view('content', $data);
}
}
//view file
<?php $this->load->view('header'); ?>
<p>The data from the controller</p>
<?php $this->load->view('footer'); ?>
I've seen it done both ways, but want to choose now before I go too far down a path.
Actually, after researching this quite a bit myself, I came to the conclusion that the best practice for including headers and footers in MVC is a third option - namely extending a base controller. That will give you a little more flexibility than the text's suggestion, particularly if you're building a very modular layout (not just header and footer, also sidebar panels, non-static menus, etc.).
First, define a Base_controller class, in which you create methods that append your page elements (header, footer, etc.) to an output string:
class Base_controller extends Controller
{
var $_output = '';
function _standard_header($data=null)
{
if (empty($data))
$data = ...; // set default data for standard header here
$this->_output .= $this->load->view('header', $data, true);
}
function _admin_header($data=null)
{
if (empty($data))
$data = ...; // set default data for expanded header here
$this->_output .= $this->load->view('admin_header', $data, true);
}
function _standard_page($data)
{
$this->_standard_header();
$this->_output .=
$this->load->view('standard_content', $data, true);
echo $this->_output; // note: place the echo statement in a
// separate function for added flexibility
}
function _page_with_admin_header($data)
{
$this->_admin_header($data);
$this->_output .=
$this->load->view('standard_content', $data, true);
echo $this->_output;
}
}
Then, in your page controllers, simply extend the base class and call your functions to build the page.
class Page_controller extends Base_controller
{
function index()
{
$data = ...; // Set content data here
$this->_standard_page($data);
}
function admin()
{
$data = ...; // Set content and header data here
$this->_page_with_admin_header($data);
}
}
Using a base controller, you can achieve very clean code in your individual page controllers AND have separate views for elements on the page (allowing code reuse in both views and controllers). All you need to do is define your common page 'sections' (what you might be tempted to call 'fragments') as functions in your base controller.
And if the base controller should start to grow uncontrollably (which can happen on large sites), you can rearrange some of its less-general functions by placing them in subclasses and letting the corresponding page controllers extend those instead of the original base controller.
Enjoy!
You could also try it this way -- define a default view template, which then pulls in the content based on a variable ('content' in my example) passed by the controller.
In your controller:
$data['content'] = 'your_controller/index';
// more code...
$this->load->vars($data);
$this->load->view('layouts/default');
Then define a default layout for all pages e.g. views/layouts/default.php
// doctype, header html etc.
<div id="content">
<?= $this->load->view($content) ?>
</div>
// footer html etc.
Then your views can just contain the pure content e.g. views/your_controller/index.php might contain just the variables passed from the controller/data array
<?= $archives_table ?>
<?= $pagination ?>
// etc.
More details on the CI wiki/FAQ -- (Q. How do I embed views within views? Nested templates?...)
I think the first way you are doing it is cleaner. Simply from a point of view of knowledge that is going to be rendered. Rather than having to enter the view file to find the rest.
It's bad practice to call views inside of other views. This could be a form of controller view mixing. The view function in CI allows you to pass a third parameter that causes it to return that view's output as a string. You can use this to create a compound view.
For example:
class Page extends Controller {
function index() {
$data['page_title'] = 'Your title';
$this->load->view('default_layout', array(
'header' => $this->load->view('header' , array(), true),
'menu' => $this->load->view('menu' , array(), true),
'content' => $this->load->view('content', $data , true),
'footer' => $this->load->view('footer' , array(), true),
));
}
}
default_layout.php
<? echo $header, $menu, $content, $footer; ?>
You may want to combine your header and footer to make a template like this.
class Page extends Controller {
function index() {
$data['page_title'] = 'Your title';
$this->load->view('default_template', array(
'menu' => $this->load->view('menu' , array(), true),
'content' => $this->load->view('content', $data , true),
));
}
}
default_template.php
<html><head></head><body><span>Some Header HTML</span> // this is your header html
<? echo $menu, $content; ?>
<span>some footer HTML</span></body></html> // this is your footer html

Categories