CodeIgniter Dry Navigation - codeigniter

My boss told me make dry navigation dont use repetitive code, for navigation i am trying to extend CI_Controler and with construct load header nav, body, footer files.
My question is when i create new controller and when i try to load different view files, how to achive that???
my extended controler
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_home');
$this->load->view('view_footer');
}
}
and later when i am creating new controler how to load diffrent view files
class Pages extends MY_Controller
{
public function __construct() {
$this->load->view('includes/nav_new_view');
$
}
}

You can create a template library yourself. For example :
function template($view_name,$view_data){
//below will return html string from view name
$data['content'] = $this->load->view($view_name,$view_data,true)
//load main template view and pass the html string to main template
$this->load->view('main_template',$data);
}
In main template, just echo $content

If I understand your question, you're trying to achieve a template situation. For this, the best way is to actually call your templates view files within a primary page view. What I mean is your controller function (not the constructor, an actual class function representing a page) should call a primary view such as
$this->load->view('page1', $this->data);
and within that file, you call
$this->load->view('nav', $this->data);
then your content and then
$this->load->view('footer', $this->data);
You would then repeat the process for page 2 where in your controller's page2 function, you would call
$this->load->view('page2', $this->data);
and your page2 view file is almost identical to page1 except you use your page 2 content in that area.
You could even use a single template view file and pass it a $content variable (which obviously changes per page) and call
$this->load->view('template', $this->data);

Related

CodeIngiter: Load a view inside of another view

I'm using CodeIgniter. I want to load views inside of other views. How can I do this?
Example:
Let's say I have a "view" called "CommentWall". In CommentWall, I want a bunch of "Comment" views. I use the view for "comment" all over my site!
How can I do this? It seems CodeIgniter only allows me to load views sequentially, which is sort of strange considering I use reusable views INSIDE of other views!
Can I do a $this->load->view('comment'); inside of my view for CommentWall? Or is there some other way to have reusuable views contained inside a view?
You can do it easily, just load the main view, for example CommentWall from the controller
$this->load->view('CommentWall');
To add child views in CommentWall view you can add following line inside your CommentWall view
$this->view('Comment');
For example, if you load CommentWall view from your controller like this
$data['comments'][] = 'Comment one';
$data['comments'][] = 'Comment two';
// load the parrent view
$this->load->view('CommentWall', $data);
Now in the CommentWall (parent view), if you put this
foreach ($comments as $comment) {
$this->view('Comment', array('comment' => $comment));
}
And in your Comment (child view) if you have this
echo $comment . '<br />';
Then you should get output something like this
Comment one
Comment two
Update : Alos, check this answer.
Try
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$data->comments =$this->load->view('comment');
$this->load->vars($data);
}
And in every view try
echo $comments;
Just load the "Comment" vies as string in controller and pass it to "CommentWall" view.
You can do it like this:
//Controller:
public function load_comment_wall($param) {
$comments_view = ""; //String that holds comment views
//here load the comments for this wall as follows:
//assuming $comment_ids is array of id's of comment to be put in this wall...
foreach($comment_ids as $comment_id) {
$temp = $this->load->view("comment",array('comment_id'=>$comment_id),TRUE); //Setting last parameter to TRUE will returns the view as String
$comments_view = $comment_views.$temp;
}
$data['comments'] = $comments_view;
//load comment wall
$this->load->view('comment_wall',$data);
}
//In Comment wall View, add the following line
echo $comments;

Rendering a partial view in Zend for adding html to routing action layout

I am rendering a page with a lot of frames (XHR contentpanes via dojo). This is done through a request to IndexController which sets up regions 'header,left,right,center,footer' with the exception, that center is not filled in with contents. This in turn is set by calling PaneController in menu.onclick. Caveat; search engines indexing service does not get center region contents.. I wish to bypass AJAX loading of center, if user enters via /index/index.
Relevant snippets from IndexController:
class IndexController extends Zend_Controller_Action {
public function indexAction() {
$this->indexModel = $this->view->indexModel = new Application_Model_Index();
// Goal is to render "/pane/main/" action and capture the HTML
$this->view->mainPane = (string) $this->renderPaneMain();
return $this->render();
}
public function renderPaneMain() {
// ActionStack ?
// action() ?
return $HTML;
}
}
Relevant stuff in Pane
class PaneController extends Zend_Controller_Action {
public function preDispatch() {
// will only return a contentpane, dont render layout
if ($this->getRequest()->isXmlHttpRequest()) {
$this->_helper->layout()->disableLayout();
$this->view->doLayout = true;
}
}
public function mainAction() {
this.render("main.phtml");
}
public function init() {
$this->panesModel = new Application_Model_Panes();
$variant = $this->getRequest()->getParam('variant', '');
// routing variables need to be set, how?
if (empty($variant))
$this->_redirect('/');
}
}
Basically, i need the PaneController to _not render the global layout but call its .phtml view file, once it has been setup with relevant model entries and such.
Any ideas as to how I can achieve this in its most efficient form?
Very well, ill attach the workaround im using here
The forms and the fork-logic i have moved to the model that is coexisting with PanesController. For the IndexController, which will present the default Pane as inline HTML without AJAX - there is a couple of duplicated initializations going on.
So, IndexModel extends the PanesModel - without initializing it. In my index.phtml view (for Index action) i have following code to render the inline html from a pane.
in index controller
$this->view->model = new IndexModel(); // extends PanesModel
$this->view->model->setDefaultProperties($variant, $pagination, ...);
in index view:
$this->partial("panes/main/main.phtml", array("model", $this->model);
and from pane view:
<?php if($this->model->goThisDirection()): ?>
Switch 1 HTML contents
<?php endif; ?>
Caveat: I also had to not render any form of layout within the pane (dojox contentpanes allows for <script> and <style> tags) - and this ofc ripples to any other pane action of mine.

CodeIgniter - Autoload for views?

is there any way to load a view (for header or footer) on every function (in a controller)? I have a couple of if/else statements there and it would be a pain to change it all when I'll need to.
Yes, you can load the view in the __construct function at the top of your controller. Take
a look at the PHP manual on Constructors
function __construct()
{
parent::__construct();
$this->load>-view('your_view');
}
If the header and footer are going to be constant and required components for the visual part of your site, but you may want to load a different content portion between your header and footer, then you can make a function that will take an argument.
private function doViews($argument)
{
$this->load->view('header');
$this->load->view($argument);
$this->load->view('footer');
return NULL;
}
You may want to have an array of available views inside the doViews function in order to do proper validation that the file exists. Then you simply call the function in each method in your controller like this:
$this->doViews('main_content');
You should try using a Template library like this: https://github.com/philsturgeon/codeigniter-template
Then all you need to is put this in the controller (can be in __construct or within a method)
$this->template->set_partial('header', 'layouts/header');
$this->template->set_partial('footer', 'layouts/footer');
$this->template->set_partial('sidebar', 'layouts/sidebar');
Then to send data like you do with a view:
$this->template->build('create', $this->data);
you could create your main_view ... as a master page that already has a structure:
main_view.php
$this->load>-view('header');
<?php //get content here
?>
$this->load>-view('footer');
If you want to change something in the header or footer (through a statement) you can add content:
function __construct()
{
parent::__construct();
$data['footer'] = ($a == 'foo') ? 'footer one' : 'footer two';
$data_to_main = $this->load->view('template/footer', $data, TRUE);
$data_to_main = 'others';
$this->load>-view('main_view', $data_to_main);
}

add iframe while calling controller to view in codeigniter

call controller to view i use this code..
i want to add "template/home/home" page in to iframe tag while calling controller to view.
how can i do?
class main_con extends Controller
{
function main_con()
{
parent::Controller();
// $this->freakauth_light->check('user');
$this->_container = $this->config->item('FAL_template_dir') . 'template/container';
$this->load->library('FAL_front', 'fal_front');
}
function index()
{
$data['redirect_page'] = 'template/home/home';
$this->load->vars($data);
$this->load->view($this->_container);
}
}
thank you in advance...
I'm not quite sure if I understand your problem but:
The iframe is in your view template right? So what you need to do is just print the content of your variable in the src attrib of the iframe.

Magento: how to add a link via code to a block of a custom module?

I'm trying to add a link to one of my blocks to a specific action of one of my controllers. Looking through the class docs and googling didn't resolve something useful. (maybe I just used the wrong search queries).
My Controller has two actions:
indexAction() and exportAction()
Now, in one of my blocks I wand to add link to exportAction(). I've found the method addLink() but this doesn't work.
Maybe anyone knows how to do that ? Or could point me to the right resources on the net ?
Regards, Alex
Block Example:
<?php
class Polyvision_Tempest_Block_Adminhtml_View extends Mage_Adminhtml_Block_Template
{
public function __construct()
{
parent::__construct();
}
protected function _toHtml()
{
$html = "whatever";
return $html;
}
}
?>
Your question isn't clear/complete.
A block renders HTML, either through a phtml template or through PHP code. To add an HTML link, you just render a html anchor tag with an href
//via PHP
protected function _toHtml()
{
$html = 'My Link';
return $html;
}
//via phtml template
#your block
class Polyvision_Tempest_Block_Adminhtml_View extends Mage_Adminhtml_Block_Template
{
protected function _construct()
{
$this->setTemplate('path/to/from/template/folder/as/basetemplate.phtml');
}
}
#your template
My Link';
The addLink method is a special method that only applies to certain types of blocks. When you call it it add links information to the block's data properties. Then, it's _toHtml method or phtml template has been written such that it loops over the stored data to output links. It doesn't apply to general blocks, which is what makes your question confusing.
Hope that helps!

Resources