Load a view inside another view - codeigniter

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.

Related

How to render html on a given view brefore RenderBody() from the layout is triggered

I'm working on an ASP.NET MVC3 project. I'm using Twitter Bootstrap for my styling (not sure if that's important). The problem I have is that my Index.cshtml view of the Home controller has slightly different layout from the other pages (additional image navigation at the top which I don't show once the user select where he wants to go) but this is causing problems so I remove this part from the Index view to another partial view _ImageNavigation.cshtml and what I want to do is render the content of this partial view when Home/Index.cshtml is opened and I want to render it before the #RenderBody() also independently from it so I get the page the way I want it.
Right now in my _Layout.cshtml I have:
<div id="main">
<div class="container-fluid">
<div class="row">
<div class="col-md-10">#RenderBody() </div>
<div class="col-md-2">
//some static content
</div>
</div>
</div>
So I have two ideas first - adding #RenderPage("~/Views/Shared/_ImageNavigation.cshtml") right before #RenderBody() like :
<div class="row">
#RenderPage("~/Views/Shared/_ImageNavigation.cshtml")
<div class="col-md-10">#RenderBody() </div>
which produces the effect I want, but as you may guess _ImageNavigation is rendered on every page which is not what I want. I want it only on my Home/Index.cshtml so I guess the maybe some kind of check could be made to see what view is loading and render _ImageNavigation only if it's the correct view. Something like :
if (LoadingView == Home/Index.cshtml)
{
#RenderPage("~/Views/Shared/_ImageNavigation.cshtml")
}
Of course the above is just pseudo code, I don't know if it's possible and how to make such a check. And also I wonder if there is a way to do it in the page itself. I tried to put #RenderPage("~/Views/Shared/_ImageNavigation.cshtml") directly in my Home/Index.cshtml but obviously this way the page is rendered as if the code is written directly in the View and not loaded explicitly.
Maybe there's other way. This seems like pretty standard problem but I don't seem to find a proper solution.
When you have a smaller number of exceptions I like to use Sections. Here is a very simlified example:
Layout:
#if (IsSectionDefined("Nav"))
{
RenderSection("Nav")
}
else
{
<nav>default nav</nav>
}
#RenderBody()
Page with alternative nav:
#section Nav
{
<nav>My alternate nav</nav>
}
<div>This is the body for RenderBody</div>
You can read more on Defining Default Content For A Razor Layout Section - Phil Haacked.

blade template, #yield at extended view

I would like to define a section at the layout or at an include, such as:
/views/master.blade.php
#section('mysection')
content here
#stop
#yield('content')
And be able to yield it at the view inheriting the layout:
/views/home.blade.php
#extends('master')
#section('content')
#yield('mysection')
#stop
From what I've tested this doesn't work. Is there another way of doing this?
Some sections are not supposed to always render and also not in the same place as they are on the layout, so the yield method would do what I need.
You can try to modify the master.blade.php as:
/views/master.blade.php
#section('mysection')
content here
#show
#yield('content')

How to grab content from section within blade template

I have written a training application with each page/slide of the training workbook as a seperate blade template file named as "page1.blade.php", "page2.blade.php" and so on. Each of these files has content of the kind:
#extends('en/frontend/layouts/training_modulename')
{{-- Page title --}}
#section('title')
Page Title
#parent
#stop
{{-- Page content --}}
#section('pageContent')
<div class="pageContentContainer">
<h2>Page Title</h2>
...
</div>
#stop
This works really well when being viewed page by page within the browser. However I also wish to automatically compile all pages into a PDF document. This is being done via dompdf which works amazingly well when I pass each pages html to it manually. However I wish to condense the #section('pageContent') section of each page into one large section which extends a different layout for passing to dompdf.
Given the above context my question is this:
Is there a method in Laravel's blade parser which would allow me to pass it a blade file and just get the rendered html from a particular section? The below pseudo-code demonstrates what I would like to be able to do.
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage = Blade::render($page);
$title = $renderedPage->title;
$pageContent = $renderedPage->pageContent;
}
Instead of doing the normal return of view
return View::make('page');
You can instead store the view in a string
$view = View::make('page');
So then you can do your code something like this (not tested - but you get the idea):
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage[] = view::make($page);
}

How do I get dompdf to ignore conditional content?

I have a CakePHP script that prints a report with large blocks of conditional content like so:
<?php if(in_array('Medical', $filter) || in_array('All', $filter)){ ?>
<div class="title_col_cell" align="left">Medical</div>
<div class="title_col_cell1" align="right">Visits</div>
<div class="title_col_cell1" align="right">Unduplicated Clients</div>
<div class="title_col_cell1" align="right">Prescription Assistance</div>
<div class="title_col_cell1" align="right">Vaccinations</div>
<?php } ?>
When I create a print view of this report, dompdf seems to be rendering the conditional content as blank space. Is there a way to get dompdf not to render that stuff? There are too many possible combinations for me to make a separate view for each. How can I code this differently?
Thanks!
Anything that's displayed on the screen if you view the document will get to dompdf generated doc. If you can't see it in html document - then it will be hidden. View your document just before you pass it to dompdf in a standard browser to see whats going in there.
DOMPDF pdf will create pdf only for the code which has been sent to it. It's better you put the whole code in a string and then display it on screen to check whether all required html is there or not, before sending to dompdf object. For example :
$str="<div class="title_col_cell" align="left">Medical</div>
<div class="title_col_cell1" align="right">Visits</div>
<div class="title_col_cell1" align="right">Unduplicated Clients</div>
<div class="title_col_cell1" align="right">Prescription Assistance</div>
<div class="title_col_cell1" align="right">Vaccinations</div>";
$dompdf = new DOMPDF();
$dompdf->load_html($str);
$dompdf->set_paper('letter', 'landscape');
$dompdf->render();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
exit(0);

How to implement a sidebar in Zend Framework

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.

Resources