Passing further inside of template - php

I'm using the same template library that phil sturgeon created and I have the following layout for my control panel. I am getting this error. I ran a var_dump on the template variable inside the control panel controller and it showed the string of the control panel view but when I do the same thing inside of the content view it says there was no body index. I would like to know how I can pass the data to the content view.
Any ideas for me?
Severity: Notice
Message: Undefined index: body
Filename: partials/content.php
Line Number: 8
Control Panel Controller
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controlpanel extends Backend_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->template
->title('Control Panel')
->set_layout('controlpanel')
->set_partial('header', 'partials/header')
->set_partial('sidebar', 'partials/sidebar')
->set_partial('breadcrumbs', 'partials/breadcrumbs')
->set_partial('content', 'partials/content')
->set('user_data', $this->users_model->get($this->session->userdata('uid')))
->build('dashboard');
}
}
content.php
<div id="content">
<!-- Insert Header -->
<?php echo $template['partials']['breadcrumbs']; ?>
<div class="separator bottom"></div>
<?php echo $template['body']; ?>
</div>
I've tried looking into this and still haven't found a solution. I was hoping someone else might see something I am not.

I use his library too, and got some ploblems when passing objects through
$this->template->set('data_name', $my_object);
just cast it to array.
$this->template->set('data_name', (array) $my_object);

THe following works for me:
$this->template->set('body', $body);
Then in my view (using the PyroCMS lex parser):
{{ body }}
Alternatively, you could assign an array to the set method:
// $page is from a db query
$this->template->set('page', $page);
Then in the view:
{{ page->body }}

Related

rows from db in header, which set to display in all page

In CodeIgniter I'm try to made header with code from DB, my controller code:
public function index()
{
$this->load->model('main_model');
$data['result'] = $this->main_model->get_tipsters();
$this->load->view('template/header_view',$data);
}
And header_view:
<?php foreach($result->result() as $row): ?>
<div id="tipster"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></div>
<?php endforeach; ?>
Header work's only It self view file, but not In others pages.
I Including header like this in controllers:
$this->load->view('template/header_view');
$this->load->view("/bets/index",$data);
$this->load->view('template/footer_view');
Getting this error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
Filename: template/header_view.php
Line Number: 14
Fatal error: Call to a member function result() on a non-object in /home/user/domains/test.com/public_html/application/views/template/header_view.php on line 14
Line 14 is foreach, I have copied early.
Since your header view file is always expecting $result, you'll need to provide it for all your controller methods:
$this->load->model('main_model');
$data['result'] = $this->main_model->get_tipsters();
$data['main'] = $this->main_model->get_main_data(); //example
$this->load->view('template/header_view',$data);
$this->load->view("bets/index",$data);
$this->load->view('template/footer_view');
This can become cumbersome, so consider creating a MY_Controller file that extends CI_Controller - more about that here.
You can make a function in your Common_model
for fecthing result on your header file.
and directly get result from that function by calling it in a view file.
common_model.php
function your_function()
{
// code for fetching data for header
/// return your result here
}
call directly this function in a view file as
$rs = $this->Common_model->your_function();
Note: common_model is load by default .if disable then you need to load model in a view file.
Best way to load CI object on view element file(header_view) and load and call model method with CI object
$CI = & get_instance()
$CI->load->model('main_model');
$result = $CI->main_model->get_tipsters();
<?php foreach($result->result() as $row): ?>
<div id="tipster"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></div>
<?php endforeach; ?>
and remove code from controller

PHP Templating in Laravel 4

I just did a fresh install of laravel 4.1. I am not really a big fan of templating languages so I would just prefer to use regular php for the templating. However, I do like the idea of inheritance and sections that laravel provides. I have seen examples of using the sections like so:
<?php Section::start('content'); ?>
Test Content
<?php Section::stop(); ?>
However, when I try this on my install I am getting:
Class 'Section' not found
When I try doing it with the blade templates it works fine but not with a regular php template. I cannot find any documentation at all about just regular php templates, all I can find is documentation on blade. Is this not available in the new version or has it changed? Any links or help would be greatly appreciated as googling keeps coming up with results for how to use blade templates.
You are looking for View actually Illuminate/View/Environment.php:
<!-- layuts/mastrer.php -->
<body>
<?php echo View::yieldContent('content'); ?>
</body>
<!-- views/home/index.php -->
<?php View::startSection('content'); ?>
<?php echo 'Welcome ' . $name; ?>
<?php View::stopSection(); ?>
BaseController class:
protected $layout = 'layouts.master';
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
HomeController class:
public function ShowHome()
$this->layout->title = 'Laravel -> Lout';
$this->layout->content = View::make('home.index', ['name' => 'Sheikh Heera']);
}
But, if you use these functions then why not just use the blade ?

CodeIgniter - Using database connection with AJAX request

I'm using a script (applications/view/pages/home.php) which has an AJAX request whereby it gets and displays the content of another script (we'll call it scheduler.php). The scheduler file is just a file containing dynamically modified html based on a $_GET parameter passed to it from the AJAX request.
My issue is that this dynamic content comes from the database, and since the scheduler.php is being called by AJAX, it doesn't inherit the $this->db-> ability.
I get the error: Fatal error: Using $this when not in object context.
How can I fix this? I am a novice to both CodeIgniter and to AJAX. Thanks!
Edit: Scheduler.php code:
<?php
$shift = $_GET['shift'];
?>
<table>
<tr>
<th><?php echo $d->format('l') . '<br>' . $d->format('F jS'); ?></th>
</tr>
<tr>
<td>
<?php
$this->db->select('id', 'event', 'time');
$query = $this->db->get('myTable', $shift, $shift-5);
foreach ($query->result() as $row) {
echo "<a href='/schedule/{$row['id']}'>{$row['event']} at {$row['time']}</a><br>";
}
</td>
</tr>
</table>
As per discussion in comments, scheduler.php is not controller or library.
So you are calling outside the CI project. You can't use CI DB function untill you process through CI index.php file.
So just make scheduler.php as controller as below:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Scheduler extends CI_Controller {
public function index($shift = "")
{
// add your stuff here to response to ajax request.
}
}
?>
Then change ajax url to : domain.com/index.php/scheduler/index/{shift_value_here}
You can get access to the CodeIgniter "super object" through $CI =& get_instance();
After that, replace $this with $CI in scheduler.php and you will have access to the framework libraries and functions etc.
Read section: Utilizing CodeIgniter Resources within Your Library in the documentation.

Conditional extends in Blade

Is there any way to do a conditional #extends statement in the Blade templating language?
What I've tried:
#if(!Request::ajax())
#extends('dashboard.master')
#section('content')
#endif
<div class="jumbotron">
Hey!
</div>
#if(!Request::ajax())
#stop
#endif
Output
When the request was not AJAX it printed out #extends('dashboard.master'), but the AJAX request worked fine.
What I'm trying to do
Stop including the master template (which includes header and footer) for AJAX so it can easily display the requested content
#extends((( Request::ajax()) ? 'layouts.ajax' : 'layouts.default' ))
in the master layout:
#if(!Request::ajax())
//the master layout with #yield('content'). i.e. your current layout
#else
#yield('content')
#endif
This kind of logic should really be kept out of the template.
In your controller set the $layout property to be dashboard.master then instead of calling returning your view or response, terminate with just $this->layout->content = View::make('dashboard.template')
Take a look at the Laravel docs on this
You could end up with something like this
<?php
class Something extends BaseController {
$layout = 'dashboard.master';
public function getIndex()
{
$template = View::make('dashboard.template');
if(Request::ajax()) {
return $template;
}
$this->layout->content = $template;
}
}

Best way to construct a website application in code igniter

How do people construct websites with cake/CI ect... for easy maintenance on the html?
I can put each of the sections in its own view file and make the website that way:
<div id="header"></div> <!-- header_view.php -->
<div id="content"> <!-- header_view.php -->
<div id="left_column"></div> <!-- page_x_view.php -->
<div id="center_column"></div> <!-- page_x_view.php -->
</div>
<div id="footer"></div> <!-- footer_view.php -->
But each page_x_view.php file would contain
<div id="left_column"><!-- Content --></div>
<div id="center_column"><!-- Content --></div>
And I'm duplicating these items through each of the files, so if I need to change the column structure then it is not easy.
Hopefully I am clear.
I have a controller caled MY_Controller which has a method that renders the complete page. I extend all my controllers from this main controller. HOw this helps? My main controllers takes a view and embedds it in the main content area of page and assembles a complete page. This controller takes header, footer, sidebar views and does all the mambo jumbo. Its very easy to develop such a system in CI. Some this call two step or multiple views. So if some random day I have to change layout of my page I just need to look at MY_Controller.
Cake on the other side uses layouts. I have done just one project in CakePHP so am not that expert but you can achieve the same effect in any framework. Here is how I do it in CI
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
log_message('debug', 'Controller Library '.__CLASS__ . ' ('. __FILE__ .') loaded.');
$this->properties['viewPath'] = $this->config->item('viewPath');
$this->setPageMetaData();
$this->setFavIcon();
}
public function render($viewData = null, $data=null)
{
$data = array(
'headerLayout' => $this->printHeaderLayout(array_merge($this->properties, (isset($data['headerLayout'])?$data['headerLayout']:array()))),
'leftLayout' => $this->printLeftLayout(array_merge($this->properties, (isset($data['leftLayout'])?$data['leftLayout']:array()))),
'rightLayout' => $this->printRightLayout(array_merge($this->properties, (isset($data['rightLayout'])?$data['rightLayout']:array()))),
'footerLayout' => $this->printFooterLayout(array_merge($this->properties, (isset($data['footerLayout'])?$data['footerLayout']:array()))),
'containerLayout' => $viewData,
);
this->load->view($this->properties['viewPath'].'layout/layout.php', $data);
}
public function setPageMetaData($pageMetaData=null)
{
$this->properties['pageTitle'] = isset($pageMetaData['pageTitle'])? $pageMetaData['pageTitle'] : $this->config->item('pageTitle');
$this->properties['pageKeywords'] = isset($pageMetaData['pageKeywords'])? $pageMetaData['pageKeywords'] : $this->config->item('pageKeywords');
$this->properties['pageDescription'] = isset($pageMetaData['pageDescription'])? $pageMetaData['pageDescription'] : $this->config->item('pageDescription');
}
public function setFavIcon($favIcon=null)
{
$this->properties['favIcon'] = (null !== $favIcon) ? $favIcon : $this->config->item('favIcon');
}
public function printHeaderLayout($data=null)
{
return ($this->load->view($this->properties['viewPath'].'layout/header', $data, true));
}
public function printFooterLayout($data=null)
{
return( $this->load->view($this->properties['viewPath'].'layout/footer', $data, true));
}
public function printLeftLayout($data=null)
{
return($this->load->view($this->properties['viewPath'].'layout/left', $data, true));
}
public function printRightLayout($data=null)
{
return($this->load->view($this->properties['viewPath'].'layout/right', $data, true));
}
}
Do note that this is not the exact code. I had to modify it for you, so do not blindly user it. If you know CI you will understand that I have setup paths to view in a config file. This helps me in setting up two totally different themes and use same controller. I can also add authentication layer which will based on user authentication/cookies can show a login or logout link in header. This is a template which I keep change and I extend all my controllers from MY_Controller and use in my controllers I simply do
$viewDataForForm = $this->load->view($this->properties['viewPath'].'homepage/some-form', array(), true);
$viewDataForContent = $this->load->view($this->properties['viewPath'].'homepage/some-content', array(), true);
$this->render($viewDataForForm.$viewDataForContent);
HTH!
Codeigniter and CakePHP take advantage of the Model View Controller configuration. They separate the database queries and data processing from the views. This provides an easy to use and easy to maintain way of coding. Multiply controllers can use the same view which helps cut down on the amount of code written and the complexity. Methods in the models can be reused which reduces bugs and amount of code written. And controllers provide and easy to follow way of reading and writing code. I am not sure if I answered your question but comment on my answer if you need and more explanation.

Categories