Laravel : Variable layout for a single view - php

I wanted to know if there's a way to use variable layout for a single view in laravel.
I have a view of the login section. I want to show the login view in a lightbox by calling it via AJAX. I was thinking of using a different layout for the login view when it is called through ajax.
Something like this :
if($_GET["from"] == "ajaxLink") {
// use layout1
} else {
// use layout2
}
This obviously doesnt work. :)
Is there any way i can do this??
Thanks.

2 ways.
1. Blade layout
Controller:
$layout = Request::ajax() ? 'layout1' : 'layout2';
$data = array('layout' => $layout);
return View::make('index', $data);
View:
#layout($layout)
//rest of the code....
2. Controller layout
public function action_index()
{
$this->layout = Request::ajax() ? 'layout1' :'layout2';
$this->layout->nest('content', 'index');
}

Related

Retrieve data from database in codeigniter

I am new to Codeigniter so I'm having some difficulties I want to retrieve data from the database and load it in the view but I couldn't figure out how.
Here is my model:
public function viewClientWaterwell(){
$userid = get_client()->userid;
$waterwells = $this->db->select('*')
->from($this->table)
->where('ww_client_id', $userid)
->get()->result_array();
return $waterwells;
}
Here is my clientController:
public function viewClient()
{
$data['waterwells'] = $this->waterwell_model->viewClientWaterwell();
$this->data($data);
$this->view('waterwells/clients/viewClient');
$this->layout();
}
I want to pass some data in a view to the client side but I can't figure out how. I think there is some problem with my model but I cannot figure out what. I'm new to this and will appreciate your feedback. Thanks!
You just need to use second argument to pass data to your template or view file
Here is example to start
Inside your controller function
public function viewClient()
{
$data = array( 'myvar' => 'I love stackoverflow' );
$this->load->view('waterwells/clients/viewClient', $data);
}
And inside your template or view file you can access them by their name.
<?php echo $myvar; ?>

Page is not Redirecting After Update Query in CodeIgniter

When i submit form, it goes to the Preferences Controller in setPreferences method.
Below is my controller.
public function index(){
$data['page_title'] = 'Preferences';
$this->load->view('admin/common/header',$data);
$this->load->view('admin/common/sidebar');
$this->load->view('admin/preferences');
$this->load->view('admin/common/footer');
}
public function setPreferences(){
$preferences = $_REQUEST;
if($this->Preferences_model->set_value($preferences)){ //model is autoloading
$this->load->view('admin/preferences');
}
}
In my model, it update the table, sussessfully, but it is not redirecting again to the Prefrences page. Now it is going to blank page like this
http://localhost:88/personalsite/Preferences/setPreferences
Here is my Model
function set_value($array){
foreach ($array as $key=>$value){
$this->db->set('value',$value);
$this->db->where('name',$key);
$this->db->update($this->table_name);
}
return $this->db->affected_rows();
}
How can i redirect it to the View http://localhost:88/personalsite/Preferences/?
I used redirect('Preferences') but it is still not redirecting.
Try this
redirect('preferences');//The function will build the URL based on your config file values(`.htaccess` and `routes.php`).
if not working then use else condition in setPreferences function like:
if($this->Preferences_model->set_value($preferences)){ //model is autoloading
$this->load->view('admin/preferences');
}else{
echo 'not redirect';
exit;
}
also check .htaccess and routes.php file (May be your path is going to override)

grocery Codeigniter loading view with template

i have recently downloaded grocery from their site and i am having a small problem in loading views with a template ..
as in controller the function of loading view look like this
function _example_output($output = null)
{
$this->load->view('groceryView.php',$output);
}
function index()
{
$this->_example_output((object)array('output' => '' , 'js_files' => array() ,'css_files' => array()));
}
what i want to try to implement is this
$data['main_content'] = 'groceryView';
$this->load->view('dashboardTemplate/template',$data);
i dont know if i do this how can i pass output ... as it gives me an error if i do this
$data['output'] = 'output';
Well you can load multiple views . You need to modify _example_output. If you have header and footer seperately you could load it like this.
function _example_output($output = null)
{
$this->load->view('header.php');
$this->load->view('groceryView.php',$output);
$this->load->view('footer.php');
}

Creating a more flexible view in Zend Framework

Given an html/javascript 'widget' which needs to have certain fields customized before use. For example, the css class ids need to be unique as the widget may appear more than once on the same page.
Let's say I want to keep the markup (js/html) of the widget stored as a template so that I can fill in the values that need to be customized during resuse.
I know that Zend Framework's views give you at least part of this functionality, but each view is generally associated with a particular controller. Given that this widget could be created from any controller, yes still needs to be able to access some properties stored in a controller (or model). Where should I put the widget markup and how then do I fill in the custom values?
Can I create a custom view that can be reused within the same page (appear more than once) as well as on other pages? If so, how do I set that up?
Sounds like you need a ViewHelper http://framework.zend.com/manual/en/zend.view.helpers.html. Create a custom helper that will fetch the data from a model and just simply output it. This way it won't depend on any controller, can be called in either the layout or in any view script. Example:
// views/helpers/Widget.php
class Zend_View_Helper_Widget extends Zend_View_Helper_Abstract
{
protected $_model = null;
protected $_view = null;
public function widget()
{
$data = $this->_getDataFromModel();
return $this->_view->partial('widget.phtml', array('data' => $data));
}
public function setView(Zend_View_Interface $view)
{
if($this->_view === null) {
$this->_view = $view;
}
return $this->_view;
}
protected function _getDataFromModel()
{
$this->_model = $this->_getModel();
return $this->_model->getDataForWidget();
}
protected function _getModel()
{
if($this->_model === null) {
$this->_model = new Model_Widget(); // or whatever it's called
}
return $this->_model;
}
The partial script:
// views/scripts/widget.phtml
<div class="widget-class"><?php echo $this->data; ?></div>
And when you need it in your views just call it like <?php echo $this->widget(); ?>
Note that I'm rendering the widget in a separate partial view script, just to avoid having html/css in the helper itself.
Hope this helps to get you started :)
Zend_View_Helper_Partial
Example:
<?php echo $this->partial('partial.phtml', array(
'css_id' => 'foobar')); ?>
To run this from any other module:
<?php echo $this->partial('partial.phtml', 'partials_module', array(
'css_id' => 'foobar')); ?>
In your partial view script (partial.html) you would then have access to $this->css_id.

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