MVC includes/partials (Codeigniter) - forms

I've got an issue at the moment where I have two methods and two views. Each method passes it's Db data to a view, as expected with the MVC pattern.
My issue arises because I want one of the views to be a partial, which is included in the other view.
So it looks something like this:
First view - i.e. a form
Second view - fragment of form, i.e. list of cities
I want the second view to be it's own unique file, how can I include this in the first form, at any location in the file?
** had to edit as I got the first bit wrong. There is only one controller, with two methods passing data to two views.

I don't know if I understand you correctly, but CodeIgniter has the ability to return a view as data. In the Controller it would look like this:
$data = array();
$data['partial'] = $this->load->view('partial_view', $data, true); // the 3rd parameter let's the view return as data
$this->load->view('whole_view', $data);
The whole_view file would look something like this:
<html>
<head></head>
<body>
<h1>View 1</h1>
<?php echo $partial; ?>
</body>
</html>
The logic, that loads the data for the partial_view could be extracted to a model or so.
Hope this helps!

You can include the load like this:
view_one.php
<html>
<head></head>
<body>
<h1>View 1</h1>
<?php echo $this->load->view('view_two'); ?>
</body>
</html>

Related

custom tag in HTML and parser with php

i have a problem and need your help.
i want use my custom tag in my script code like [tag] and analyze all html code then parser codes and Replace these tags with php code or my output world and echo my output
a simple code :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>[MY_PAGE_TITLE]</title>
</head>
<header></header>
<div class="left">[MY_WEBSITE_LEFT_SIDEBAR_INFORMATION]</div>
<article>[MY_WEBSITE_ARTICLE]</article>
<div class="right">[MY_WEBSITE_RIGHT_SIDEBAR_INFORMATION]</div>
<footer></footer>
<body>
</body>
</html>
my first tag [MY_PAGE_TITLE] should be replaced with one world like "my website"
i want use this for my language website.
i get this word from a array like :
mylangarray [
MY_PAGE_TITLE="my website"
]
but for my any other tag i want load some module to left or right sidebar or load some article from my DB
How can I do this?
i solved my problem.
if you have this problem, you can use this method.
step1: you define your custom tag in your main view file and then in the server side with PHP ob_start function run output buffer then require or require_once or include or include_once your file.
now with define a variable and ob_get_contents function in PHP, store all file code in a variable. like this:
$html = ob_get_contents();
now you have all view file in a variable.
you can clean your buffer with ob_end_clean function.
example:
ob_start();
require_once 'mainViewFile.php';
$html = ob_get_contents();
ob_end_clean();
step2: you can use str_replace funcation for replace your defined custom tag with your new data.
$leftSideBarInformation = 'We stored left bar information in this variable!';
$html = str_replace('[MY_WEBSITE_LEFT_SIDEBAR_INFORMATION]', $leftSideBarInformation, $html);
if you have array of custom tag you can use a foreach loop.

apply class active to anchor tag in Zend_Navigation

i am using partials to decorate Zend_Navigation to fetch desired output. everything is working good except i am having difficulty adding class="active" in <a href..> tag. here is my layout partial sidebar.phtml
<ul id="menu" class="nav">
<?php foreach($this->container as $page): ?>
<li class="<?php echo $page->class; ?>"><span><?php echo $page->label; ?></span></li>
<?php endforeach; ?>
</ul>
in the <a href="<?php echo $page->getHref(); ?>"> i want to add class="active" for the current page.
i tried some solutions which i found after searching. but nothing worked for me.
most of the solutions talk about doing it in controller for example
$page = $this->view->navigation()->findOneByLabel('Your Label');
if ( $page ) {
$page->setActive();
}
i haven't tested this yet. as i am using multiple navigations in the layout from one single navigation.xml file. i was wondering if is there a way i could do it in partials itself instead of in controllers or other helpers?
thank you
It's easy. In the view partial you can check which page is active:
if ($page->isActive()) { ... }
From ZF documentation:
Note: Note that when using the route property in a page, you should also specify the default params that the route defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine if the page is active. The reason for this is that there is currently no way to get the default params from a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a Zend_Controller_Router_Interface object.

Zend reusable widgets / plugins / miniapplications?

I'm new to Zend framework and trying to get some insights about code re-usability. I definitely know about modules but there seems to be a bit of uncertainty about what functionality should go into modules and what not.
What I'm trying to accomplish:
1) to have reusable mini programs/widgets/plugins (whatever you may call them) that one can simply plug into any site be doing this in layout or view:
<?php echo $this->contactform;?>
or this in the view:
<?php echo $this->layout()->blog;?>
I'd call them extension. so basically sort of what you'd see in Joomla/ WordPress/Concrete5 templates.
2) All code that is related to that specific extension should be in it's separate directory.
3) We should be able to output extensions only for certain modules/controllers where they are required. they shouldn't be rendered needlessly if it won't be displayed.
4) each extension may output multiple content areas on the page.
Do you have a nicely laid out structure / approach that you use?
It sounds like you need study up on view helpers. View helpers can be a simple as returning the App Version number or as complicated as adding html to multiple place holders. For example:
layout.phtml:
<h1><?php echo $this->placeholder('title'); ?>
<div class="sidebar">
<?php echo $this->placeholder('sidebar'); ?>
</div>
<div class="content">
<?php echo $this->layout()->content; ?>
</div>
in your view script foo.phtml for example:
<?php
$this->placeholder('title')->set('Hello World!');
$this->placeholder('sidebar')->set('Hello World!');
?>
<h1>Bar Bar!</h1>
Now if you want to be able to reuse that over and over again you can do this:
<?php
class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
$this->view->placeholder('title')->set('Hello World!');
$this->view->placeholder('sidebar')->set('Hello World!');
return '<h1>Bar Bar!</h1>';
}
}
Now, replace the code in your foo.pthml with:
<?php
echo $this->myHelper();
Both examples of foo.phtml output:
Hello World!
Hello World!
Bar Bar!
Of course this is very simplified example, but I hope this helps point you in the right direction. Happy Hacking!

How do I include header and footer with layout in Zend Framework?

I would like to let Zend_Layout include header.phtml and footer.phtml with [layouy name].phtml.
How do I do that? I tried to read codes in Zend_Layout, Zend_Layout_Controller_Plugin_Layout. I still can't figure out it..
You could include your header and footer files from within your layout.phtml file. Here's an example:
<div id="header"><?= $this->render('layouts/header.phtml') ?></div>
<div id="nav"><?= $this->render('layouts/nav.phtml') ?></div>
<div id="content"><?= $this->layout()->content ?></div>
<div id="footer"><?= $this->render('layouts/footer.phtml') ?></div>
cballou's answer is likely what you want, but I thought I'd throw this in there for good measure. If you'd like to render separate header and footer view scripts in different parts of your site, you can do it from within each controller like so:
Zend_Loader::loadClass('Zend_View');
$header = new Zend_View();
//Set header variables here
$this->view->header = $header->render('header.phtml');
Then use $this->header to pull the rendered header from within your layout. Likewise with the footer.
Just another way:
This will go in the controller:
$this->view->header = "header.phtml";
This will go in the view:
include($this->header);
Even if we do not use the controller (but only in the view) we can use:
include("header.phtml");
I realize this question posed is 4 yrs old but for those who happen upon this and don't realize there's a better way to do this with the latest ZF2, here's the 'better way' - Zend Framework 2 - How to include partial from library

Zend Framework: headTitle()->append() issue

Has anyone run into this problem...
In my layout.phtml I have:
<head>
<?= $this->headTitle('Control Application - ') ?>
</head>
then in index.phtml I have:
<? $this->headTitle()->append('Client List'); ?>
I expect that, when I go to my index action, the title should be 'Control Application - Client List' but instead I have 'Client ListControl Application - '
What is going on? How can I fix this?
Default behaviour of the headTitle() is to append to the stack. Before calling headTitle() in layout.phtml, your stack is:
Clientlist
Then, you call headTitle with the first argument and no second argument (which makes it default to APPEND), resulting in the following stack:
ClientListControl Application -
The solution, in layout.phtml:
<?php
$this->headTitle()->prepend('Control Application -');
echo $this->headTitle();
?>
Additionally, you can use the setPrefix method in your layout as such:
<head>
<?= $this->headTitle()->setPrefix('Control Application') ?>
</head>
And in your controllers/actions/etc use the standard append/prepend:
<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->append('Client List');
?>
I don't actually use headTitle, but do use ZF, and I had a quick look on the mailing list, this might solve the problem:
<head>
<?= $this->headTitle('Control Application') ?>
</head>
Then:
<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->prepend('Client List');
?>
This happens because the layout is the last script to be executed. So you actually do the append BEFORE the set of the title, so that there's nothing to append to yet.
Set the main title (Control Application) in a Controller. For example I always do it in the predispatch action of a initPlugin so that it is execute before any other Controller Action, and I can append or prepend at will.
To use such a plugin just define a new Class extending Zend_Controller_Plugin_Abstract and define a function preDispatch(Zend_Controller_Request_Abstract $request) where you can put all your common-to-the-whole-site code, and to register the plugin just put it into the controllerFront of your bootstrap: $controller->registerPlugin(new InitPlugin());