What's good way to load header and footer view in codeigniter? - codeigniter

I'm using codeigniter 3x. I'm working on my website. I'm using include method in my view.
Like
<?php include('templates/header.php'); ?>
<h1>Home Page</h1>
<?php include('templates/footer.php'); ?>
Is this a good way to show header and footer in codeigniter.
Thank!

You are half your way, here is how you will be able to make it more dynamic, in your views file you should have a structure like this:
views
- header.php
- footer.php
- template.php
- home.page
In header.php you should have all your header and footer content which you wants to display on all pages.
Now in your template move all your includes.
template.php
<?php $this->load->view("header.php"); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view("footer.php"); ?>
here you notice $main_content variable, it is dynamic file name which we want to load in our controller. So lets assume you have a controller like this:
public function home()
{
$data['meta_title'] = $this->lang->line('home_meta_title');
$data['meta_description'] = $this->lang->line('home_meta_description');
$data['meta_keywords'] = $this->lang->line('home_meta_keywords');
$data['main_content'] = 'home';
$this->load->view('template',$data);
}
$data['main_content'] = 'home'; is loading your home.php file, you can also load from subdirectories like 'directory/home'. You can also pass any variable like I gave you above example with dynamic meta.

Related

Strange behavior when returning view with custom Blade directive in Laravel

Currently I am working on a project where views need to be rendered through a custom Blade directive. However I came across a few (limitations?) errors I cannot solve (for a long time).
My custom Blade directive with tree different ways to output a view.
Blade::directive('lwField', function ($expression) {
// 1
return view('lw::module.field.field')->render();
// 2
return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
// 3
return Blade::compileString('#include("lw::module.field.field")');
});
The field view:
Get to the choppa!
The main view:
#extends('layout.default')
#section('main_content')
#lwField()
#endsection
The layout file:
// ... some cool html
#section ('main_content')
#show
// ... even more cool html
The following happens when I execute the three methods separately:
1 return view('lw::module.field.field')->render();
The fist time it executes it throws an error:
include(/somepath/storage/framework/views/b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php): failed to open stream: No such file or directory (View: /somepath/resources/views/page/default.blade.php)
This is true since there is only one view in my cached folder:
// 5b27802352643346357e49b847d934736c36cd07.php
// The main view with the field view
<?php $__env->startSection('main_content'); ?>
Get to the choppa!
<?php $__env->stopSection(); ?>
The second time I run this it magically works. It will generate 3 other files:
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// the main view with yielding layout view without field view
<?php $__env->startSection('main_content'); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.default', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
// e4cec91e7d4adb5dac5c63e5bfa85ba9a258f664.php
// the layout file
// ...
<?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); ?>
// ...
// ffd60653d8490007c272c527abef3a5ede092a33.php
// layout view and main view
// ...
<?php $__env->startSection('main_content'); ?>
<?php echo $__env->yieldSection(); ?>
// ...
It looks like it generates two files that are the same but one without the field view.
2 return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
3 return Blade::compileString('#include("lw::module.field.field")');
These methods will return a blanko page and generate two files in cache without showing/logging any errors.
// 5b27802352643346357e49b847d934736c36cd07.php
// Field view
Get to the choppa!
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// Main view and field view
<?php $__env->startSection('main_content'); ?>
<?php echo view("lw::module.field.field")->render(); ?>
<?php $__env->stopSection(); ?>
This method will not generate the layout file.
Does anyone have a clue on what is happening. Maybe this kind of behavior is not strange at all and this is how Blade should work.
I have been working on a project that required me to do the same thing, the problem is that making a new Blade directive is not a good practice when you want to render another view, It's because of the Cache, every Blade directive is being cached so if you want to render dynamic data it will cause you a lot of problems.
Since I have tried a lot of options and you want to render another view file and not just push html to the blade directive, I suggest you to make a new helper or even a service that you can pass there the parameters and it will return the render function that outputs clean HTML so this way you can be sure that your content will be always dynamic and won't be cached.
TL;DR
Example:
In your helpers functions file put:
function render_my_view() {
return view(''lw::module.field.field'')->render();
}
So then in yout blade file you can use:
{!! render_my_view() !!}

CodeIgniter adjusting url when passing parameters to controler

Ok i have records from database listed in view file, so u can see i wanna pass values to controler via href by update/grab function controler
echo $this->pagination->create_links();
br().br();
foreach ($query->result() as $q): ?>
<?php echo $q->info . br()?>
<?php endforeach; ?>
it works for first page in my pagination, when i am on some other page when i clicked on on record, instead passing parametars to controler when i clicked in keep adding url for example http://localhost/z/records/users/update/grab/3/update/grab/1/update/grab/1/update/grab/1/trtr
So error is when i have in url, when i am on second page in pagination
http://localhost/z/records/users/2
works only when i am on first page
http://localhost/z/records
is there a way to solve this proble. Will it works if i some how adjust routes??? Need help, please help me its very important
Try changing your link to an absolute URL:
<a href="/z/update/grab/<?php echo $q->id;?>/<?php echo $q->info; ?>">
Or adding a correct relative URL base to the header of your pages:
<base href="/z/" />
Codeigniter routes allow you to do this:
$route['post/(:any)/comment/(:any)'] = "posts/comments/$1/$2";
Then in the controller, the function inside my posts controller would work like this:
public function comments($one, $two) {
echo $one."-".$two;
}
so if you hit the url "/post/111/comment/222" the output would be
111-222

How to display error or success message in a template file?

I want to show message on some condition in a template file(custom module template file).I am having following code.
<?php
if(count($collection)): ?>
<?php foreach($collection as $coll): ?>
some calculations
<?php endforeach; ?>
<?php else: ?>
<?php $message = $this->__('There is no data available'); ?>
<?php echo Mage::getSingleton('core/session')->addNotice($message);?>
<?php endif;?>
But this is not working properly. The message is displayed on other pages not on the same page.
If you really need to implement that right in the template, you may use the code below:
<?php echo $this->getLayout()->createBlock('core/messages')->addNotice('My Message')->toHtml(); ?>
But the solution described by Amit Bera sounds like a better way to resolve it.
Muk,According to your code
Mage::getSingleton('core/session')->addNotice($message);
add an notice to magento.This is code set notice to session and which is reflecting on page refresh according php session ,a session variable set value is reflecting after page refresh.
If you already added this notice on other page before came to your file then ,you need to add core/session to custom module template file controllers file.
Code is $this->_initLayoutMessages('core/session');
In controller you need below code in controller.
/* load the layout from xml */
$this->loadLayout();
$this->_initLayoutMessages('core/session');
/* rendering layout */
$this->renderLayout();
Read more at inchoo blog
I found that $this->loadLayout() is what reads and clears messages from the session, therefore if you add messages before calling $this->loadLayout() then those messages should be displayed on the current page.
Example:
public function chooseFileAction() {
// a quick and dirty way to find the larger out of post_max_size and upload_max_filesize
$post_max_size = ini_get('post_max_size'); $post_max_size_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $post_max_size)));
$upload_max_filesize = ini_get('upload_max_filesize'); $upload_max_filesize_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $upload_max_filesize)));
$maxsize = $post_max_size_int < $upload_max_filesize_int ? $post_max_size : $upload_max_filesize;
// display max file size to user in a message box
$msg = 'Max file size: ' . $maxsize;
Mage::getSingleton('core/session')->addNotice($msg);
$this->loadLayout();
$this->_title($this->__('Catalog'))->_title($this->__('File Upload'));
$this->_setActiveMenu('catalog/customfileupload');
$block = $this->getLayout()->createBlock('customfileupload/adminhtml_customfileupload_choosefile');
$this->_addContent($block);
$this->renderLayout();
}
Caveat: this is tested in Magento 1.9, not 1.7.

Display Block on specific views page

Im using views to display content ,, How can i show block only in the first page in the views and im enable the ajax in the views..
I tried with PHP in the Block visibility but I dont know how to get the first page in the views...
Portfolio = the views name
<?php
$url = request_uri();
$pos = strpos($url, "page");
if ($pos === false && arg(0) =='portfolio') {
return TRUE;
}
?>
you should have different page files for your pages, like "node--product_page.tpl.php"
you can add your view block inside your page, whenever you want using this code:
<?php echo views_embed_view('YourViewName[ex:product_list]', $display_id ='yourBlockViewName[ex:block_1]'); ?>

Show Product on Search Page - Magento 1.7

Currently I have a snippet of code that will forward a user to the product page if they search for a term and only 1 product is associated with that keyword.
<?php if($this->getResultCount() == 1): ?>
<?php $prodId = $this->_productCollection->getAllIds() ?>
<?php $singleProduct = Mage::getModel('catalog/product')->load($prodId) ?>
<?php header('Location: ' . $singleProduct->getProductUrl()) ?>
<?php exit; ?>
<?php elseif($this->getResultCount()): ?>
However, what I want to do now is actually serve up the product and all its details on the results page itself if its the only one with that tag/search term INSTEAD of redirecting to the product page. Im pretty new to php so please bear with me.
Block template is bad place for this. Good place - controller.
Maybe you need rewrite controller for this functionality.
For example/app/code/core/Mage/CatalogSearch/controllers/ResultController.php
In controller your code looks like:
$this->getResponse()->setRedirect($_product->getProductUrl());

Resources