I'm using Smarty_parser.php and it works well when when I use the parser by itself or if I run the parser then a view call. For example:
public function act() {
#$this->load->library('smarty_parser');
$data = array('Someinfo');
$this->smarty_parser->parse('contentTmpls/act.tpl', $data);
// Load Footer
$this->load->view('Templates/footer');
}
but not if I do:
public function act() {
#$this->load->library('smarty_parser');
$this->load->view('Templates/header');
$data = array('Someinfo');
$this->smarty_parser->parse('contentTmpls/act.tpl', $data);
// Load Footer
$this->load->view('Templates/footer');
}
The header view call seems to disappear and does not output anything. I was wondering if anybody has run into this problem or have seen a fix.
I'm not sure if this is kosher but here's the answer.
I need to switch out a piece of code:
From
if ($return == FALSE)
{
$CI->output->final_output = $template;
}
To this
if ($return == FALSE)
{
$CI->output->append_output($template);
}
Seems to be working fine. Now I just need to see if anybody is maintaining this code to add the patch.
Related
I know that you can use extract() to achieve this however my circumstances are as followed:
I am building a very small basic MVC framework for personal projects and I have this in my controller:
public function index(){
$data = [
'title' => 'Welcome'
];
$this->view('pages/index', $data);
}
As you can see this passes the data array into the view and you can echo it like:
echo $data['title'];
But I want to echo it like echo $title; I know that extract() can do this but that means I have to manually put extract($data); at the top of every page which isnt the end of the world but I was just curious if there was a way it could be done automatically? I have tried to use extract by putting it inside the view function but that did not work. I have also tried to use extract by putting it in the header file that is required_once in index.php (thus making the header file a static header thats always required) but neither has worked so any advice would be great.
Here is the code for the view function as requested:
public function view($view, $data = []){
if(file_exists('../../views/'.$view.'.php')){
require_once '../../views/'.$view.'.php';
} else {
die('View does not exist');
}
}
Simple that is it ,use compact and extract function
index method
public function index(){
$title='Welcome';
$this->view('pages/index', compact('title'));
}
Wiew method
public function view($view, $data = []){
extract($data);
if(file_exists('../../views/'.$view.'.php')){
require_once '../../views/'.$view.'.php';
} else {
die('View does not exist');
}
}
In html
<h1> hello <?php echo $title; ?></h1>
Here is where I went wrong, I put extract in the view method before and it didn't work.
However the advice here was to put it in the view function and I now understand that I put the extract function after require_once '../../views/'.$view.'.php'; I just put extract before that line of code and it is now working!
I have this following code in the Address controller
public function index()
{
if($this->session->userdata('isLogin')) {
$this->load->driver('cache');
$this->load->model('MemberModel');
if(!$this->cache->get('province') == false) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces(); // it will return json object
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
$userdata = $this->MemberModel->getProfile($this->session->userdata('userid'));
$data['user'] = $userdata;
$this->display_member_area('member/address', $data);
}
else {
redirect(base_url());
}
}
When I want to get the data using:
var_dump($this->cache->get('province'));
the result I get always shows
bool(false)
but when I tried to do this instead
var_dump($data['provinces']) // it's show me json object, that I want
Can anyone please show me where I'am doing wrong?
Thanks in advance
Remove false from the if condition and use this code.
if(!$this->cache->get('province')) {
$this->load->model('ShippingModel');
$data['provinces'] = $this->ShippingModel->the_provinces();
$this->cache->save('province', $data['provinces'], 300);
} else {
$data['provinces'] = $this->cache->get('province');
}
Make sure you have the correct credential on your config at:
https://github.com/bcit-ci/CodeIgniter/blob/develop/application/config/memcached.php
it will be at you application/config/memcached.php file. Hostname is the ip you setup your memcached instance. You could provide multiple server if you wish.
reference on installing memcached
I am trying to make a CRUD Module in Codeigniter HMVC but I seem to be missing something in the process. Here is what I am facing.
I have a News Module which has a Manage function
function manage(){
$grid = Modules::run('Crud/renderGrid', 'News' , 'News management');
}
Render Grid Function
function renderGrid($module , $page_title){
$data['page_title'] = $page_title; //Dynamic
$data['module'] = $module;
$data['view_module'] = 'Crud';
$data['displayfields'] = Modules::run($module.'/get_displayfields');
$data['key'] = Modules::run($module.'/get_key');
$data['rows'] = Modules::run($module.'/get' , $data['key']);
$data['view_file'] = 'manage';
$this->load->module('dashboard');
$this->dashboard->show_dashboard($data);
}
Here, the show_dashboard function just loads up a template layout with a desired view in it.
function show_dashboard($data = NULL){
if($data == NULL){
$data['view_file'] = "manage";
$data['page_title'] = 'Sigma Web Solutions';
}
$this->load->module('templates');
$this->templates->admin($data);
}
Templates->admin
function admin($data){
$this->load->view('admin' , $data);
}
The View (omitting the header n Footer)
<?php
if (!isset($view_file)) {
$view_file = "";
}
if (!isset($view_module)) {
$module = $this->uri->segment(1);
}
if (($view_module!="") && ($view_file!="")) {
$path = $view_module."/".$view_file;
$this->load->view($path);
}
?>
Now, when I try the url news/manage, it gives me a blank page with no source code in it. But when I try something like
crud/renderGrid/news/sometitle/ it works just fine.
Kindly point out what did I miss here. Thanks.
Working Solution:
Thanks to wolf I added a route
$route['managenews']= 'crud/renderGrid/news/News';
And it works like charm. But why do I need a route here? Shouldn't it just work. And this means for every module I need to have 4 entries in my route file for the CRUD system to work. can anyone suggest a better method?
I am getting error page not found when I implementation URL using slug.
This is code in controller
function view($slug)
{
$this->data['halaman_item'] = $this->mhalaman->get_profil($slug);
if (empty($this->data['halaman_item'])) {
show_404();
}
$this->data['title'] = $this->data['halaman_item']['judul']; $this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('mahasiswa/profil/sejarah', $this->data, true);
$this->load->view('template/wrapper/mahasiswa/wrapper_content',$this->data);
}
Model
function get_profil($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get($this->tbl_halaman);
return $query->result_array();
}
$query = $this->db->get_where($this->tbl_halaman, array('slug'=>$slug));
return $query->row_array();
}
Route
$route['profil/view'] = "profil/view";
$route['profil/(:any)'] = "profil/view/$1";
$route['profil'] = "profil";
When I run this link tkd/index.php/mahasiswa/profil/sejarah is 404 page is not found. may be you know where is the probelm.
Help me what to do. Thank you.
Well, without knowing how much of the rest of your app works...the first obvious thing is making sure $this->data['halaman_item'] actually has data in it...because if not, it's calling show_404 before anything happens.
i am doing some practice on the codeigniter to retrieve the data from database and i am success to do this.but the problem arise when i want to fetch data of a specific field.
to retrieve the specific value i am using the following URL on my local host:
localhost/codeigniter/index.php/news/view/city-news
where news is controller,view is method of controller and city-news is argument.
Here is my code of controller:
public function view($slug)
{
$data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
this method calls the method get_news($slug) of model news_model.here is the code of this method:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
echo $slug;//here is i m also getting the slug value.
$query = $this->db->get_where('news', array('slug' => $slug));//i think this is not working properly
print_r($query->row_array());die;//now i am getting values here.
return $query->row_array();
}
but still my view shows "404 page not found". my view code is:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
?>
Now please tell me where i am going wrong.
your error is you are setting $data['news'] and testing $data['news_item'] :
$data['news'] = $this->news_model->get_news($slug);//here i am getting the slug value.
if (empty($data['news_item'])) // <=== HERE IS THE ERROR
{
show_404();
}
have you tried with simple where query as follows
$data = $this->db->where('slug', $slug)->from('news');
you must get something in $data. Means at least mysql object. Its been another part that whether rows are available in database or not.
secondly i am worried about the true and false. Means as per i know Boolean store as INT(1) means either 0 or 1. You needs to check with that as well.
you can debug your query with
$this->db->last_query();
Hope this helps.
Here is the cleaned code of the controller:
public function view($slug)
{
$news = $this->news_model->get_news($slug);
if (empty($news))
{
show_404();
}
$data['title'] =$news['title'];
$data['text'] = $news['text'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
this method calls the method get_news($slug) of model news_model:
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
echo $slug;
$query = $this->db->get_where('news', array('slug' => $slug));
print_r($query->row_array());die;//now i am getting values here.
return $query->row_array();
}
<?php echo '<h2>'.$title.'</h2>'; echo
$text; ?>
A small error:
Here
if (empty($data['news_item'])) // <=== put "news" here
{
show_404();
}
and at your view try to get data as
echo $news;
die() is a function use exit instead.
set this at the beginning of script
ini_set('display_errors', 1);
error_reporting(E_ALL);
also use var_dump($query); to check if it return any result if not exception or fatal error was raised when calling query
if array is empty it means that no news with this slug is in db. You should set some conditions when particular news is not found
also what is the value of this slug, does it contain any special chars etc.?