Php code not rendering in Views loaded in CodeIgniter - php

I have a controller which has following function
public function listartistes () {
$this->set_language();
$this->data['title'] = "Create Your Own Show Today";
$this->data['meta_title'] = "Create Your Own Show Today";
$this->data['meta_desc'] = "Create Your Own Show Today";
$this->data['content'] = 'artiste/list';
$this->data['artistes'] = $this->artistes_m->getAllArtistes();
$this->load->vars($this->data);
$this->load->view('template');
}
As you can see the controller is loading the template view and inside template view we load the 'content' file like this this
<div class="container-full">
<?=$this->load->view($content);?>
</div>
So far so good . but when I try to put any PHP code in list which is inside
/artiste/list its not showing on my browser only blank screen

The partial pattern in CodeIgniter allows consecutive views to be loaded, not embedded ones. So this means that you might need a partial for header, content and footer for example instead of an outer template and inner [content] template.
So while you may have to adjust what markup and data is in which view, you will probably be best off putting
$this->load->view($content); on the line after $this->load->view('template'); in the controller. I don't think that CodeIgniter is capable of loading views from within views.

Related

What's the best way to handle form data in wordpress in the backend?

Right now I'm simply sending the data to my page template but I don't think it's good to have all that logic together with the markup with the page, especially since I want to have multiple forms on the page which would lead to even more code that isn't really relevant to the view of the page. In another cms (concrete) I have set up routes that point to a custom controller to handle the form data, can I do something similar or is there a different approach for this in wordpress?
I would start by separating controller logic from the html template. Look at Timber for example: https://github.com/timber/timber
Then your page template will look more like a controller and you could avoid messy code from handling everything in one file.
Your page template could look like this:
<?php
$data = Timber::get_context();
$data['page'] = $page;
if (isset($_POST['whatever']))
{
$data['whatever'] = 'It works!';
Timber::render('views/whatever-posted.twig', $data);
}
else
{
Timber::render('views/landing.twig', $data);
}
IMO it is much cleaner code than mixing up PHP/HTML

codeigniter parse content in template

Can someone help me to explain in which way is the best to organize views in codeigniter and make some template where header and footer will be same but content to change for every page? I use parse to send data from controller to view but don't know how to organize view in described way.
Also how to avoid php logic in view (if)?
There are two ways if your are not using a template library:
1. Request your partial views in your content view:
<?php $this->load->view('header') ?>
My content
<?php $this->load->view('footer') ?>
2. Create a layout view, pass the content as variable:
<html>
<body>
<?php echo $content ?>
</body>
In this case, in your controller:
// The true parameter means it will load the view, but just pass back as a string
$content = $this->load->view('mycontent', array(), TRUE);
// Display the layout with your content
$this->load->view('layout', compact('content'));
As asking about the logic, don't get too strict with it. if, for, foreach are okay in a template. Forexample if a user is logged in, show a menu.
This might help you regarding about the templating.
Controller:
public function index() {
$data = array(
'blog_title' => 'Home',
'blog_content' => 'Content goes here..',
'footer_data' => 'footer data here..'
);
$this->load->view(templates/header, $data);
$this->load->view(templates/content, $data);
$this->load->view(templates/footer, $data);
}
Note: imagine that templates is a directory inside your views folder. Inside your templates must be the header, content and footer.php files.

How to display common pages in CodeIgniter

I'm new to CodeIgniter and I'm very confuse on how to load my header.php to all the other pages I made. I want my header to be included on every page I made without manually putting them so that every time I make changes to the header I won't edit the entire page. Instead I'll just make changes in the header.php. Thank You.
create a common page call layout.php
inside that
<?php $this->load->view('includes/header'); ?> //site header
<?php $this->load->view($main_content); ?> //comes from controller.
<?php $this->load->view('includes/footer'); ?> //site footer
and in controller
function privacy_policy()
{
$data['main_content'] = 'pages/privacy_policy';
$this->load->view('layout', $data);
}
and main_content should direct to page
so its
reduce your time
reduce your code
easy to understand
etc....
You can create a folder for example -Layout under view folder and add your common pages like header,footer in it.
And in your controller on calling you can do so-
function index() {
$this->data['survey'] = $this->survey_model->get_survey();
$this->data['main_content'] = $this->controller . '/show';
$this->load->view('layouts/main_content', $this->data);
}
In this case show is my view page to be displayed,main_content is a page in my layout where header and footer is mentioned.
There are many ways but the one I prefer the most is as below :
in your Controller File
SomeController.php
function someFunction() {
$this->data['main_page'] = 'someview';
$this->load->view('layouts/main_template', $this->data);
}
In your main_template.php view
<?php
$this->load->view('layouts/header'); // your common header file
// your dynamic page file you can choose which page to load by changing the value of $main_page in controller.
$this->load->view($main_page);
$this->load->view('layouts/footer'); // your common footer file
?>

Codeigniter session does not work after including Controller in view

I was working on a site in which on right side I am displaying some fixed type of dynamic content like event calendar, login, register etc. right side bar name is right_view.php
So first I was doing like this that I was sending parameters in every function of controller's and then in my view I was accessing right side parameters by calling right view like this
<?php $this->load->view('right_view');?>
then after login I can get my username that is stored in session.
After that I thought it is not a good approach to send parameters in every functions I just make a controller named right.php and in this controller I am passing parameters to right_view.php and after that in my view I changed my code for callig righr_view like this
<?php include(base_url().'right');?>
It display right content as I do above but one changed happen that I cannot access any of session stored variable in right side bar.
Is session does not work after including controller in view?
You're basically wanting to do a template system but going about it the wrong way. And for the record no I don't think you can (or at least should) be loading controllers into views like that.
What you want is a template file something like this:
<?php
$this->load->view('templates/header', $title);
$this->load->view('templates/sidebar',$sidebar_content);
$this->load->view('pages/'.$main_content);
$this->load->view('templates/footer');
?>
Call that template.php
Then in your controller you'd do something like this:
public function welcome()
{
$data['main_content'] = 'welcome';
$data['title']='Welcome to REfficient.com!';
$data['sidebar_content'] = 'sidebar/no_sidebar';
$data['additionalHeadInfo'] ='';
$this->load->view('templates/template',$data);
}
So if you look at the template file the first line is loading the header and including the title variable to insert into the page (header, sidebar, maincontent and footer are all their own separate PHP pages) and so on.
Now what I did (since my layout was very similar to yours) is my main sidebar file has an if statement that says if logged in show x, if not show login form.
Note - the additionalHeadInfo variable is so I can have includes like jQueryUI or something on an individual page without loading it on pages that don't need it.

codeigniter sidebar

i have a little problem that i dont know what to do with.
i have a template with
header
content
sidebar
footer
on all my page the only thing that need to change is the content, so how can i pass data to my sidebar/footer
do i need to make a controller? or do i need to make a library and load it in my template.php file?
i use this template system
http://williamsconcepts.com/ci/codeigniter/libraries/template/index.html
I am not sure about your specific Template Library, but I do know that generally this is done by nesting views inside other views, and as long as the data is loaded into the initial view, it propagates to nested views as well.
Example without Template library
Controller function
function index() {
$data['some_var'] = "some value";
$data['another_var'] = "another value";
$this->load->view('first_view',$data);
}
first_view
<? $this->load->view('header') ?>
<h1>Content</h1>
<? $this->load->view('sidebar') ?>
<? $this->load->view('footer') ?>
In this instance, the $data that is loaded into first_view propagates to header,sidebar,and footer.
So you can use $some_var or $another_var in any of these views.
UPDATE
Another way you can load data in to your views globally is with this function
$this->load-vars($data);
Where $data is your view data, this statement just before you load your template should allow all of this data to be accessed in any view loaded by the template. While this is a shotgun approach, it is the suggested way to do this by your chosen template library.
If you will only ever change content, then there is no need to set up regions for your header, sidebar or footer - just add their contents to your main template file.
However if you will infrequently need to change the content of these regions, I would create "default" views for these regions, and load in each controller constructor, like thus:
$this->template->write_view('header', 'default/header');
$this->template->write_view('sidebar', 'default/sidebar');
$this->template->write_view('footer', 'default/footer');
You can then either extend these default region views or overwrite them on a per method basis (refer to the documentation of your library to find out how).

Categories