How can I remove just the footer from a specific view in ZF2. I have tried
$View->setTerminal(true);
return $view;
but it makes the links in the top nav bar inactive. Thanks
You could change your base layout for that particular action.
for example, your main layout may be like this example:
layout.phtml
<?php echo $this->doctype(); ?>
<html lang="en">
<head>
<?php echo $this->headTitle($this->translate('TITLE'))->setSeparator(' - ')->setAutoEscape(false) ?>
</head>
<body>
....
<?php echo $this->partial('footer') ?>
</body>
</html>
You can simple make a duplicate layout, but without the footer partial included (or how ever you are including the footer partial/view etc)
you would then tell your action to use a different base layout:
Controller.php
public function testAction()
{
/**
* Now we use the base with no footer
*/
$this->layout('layout/no-footer-layout');
// identical to below, a shortcut
//$this->layout()->setTemplate('layout/no-footer-layout');
return new ViewModel(array(/** etc **/));
}
Related
I want to show same content for different URL. For example:
website.com/controllername/country/state1/dist1/
website.com/controllername/country/state2/dist2/
I want these two URL to show same content.Even though the some content will vary based on URL (like name of state and district). I don't want to create separate content/page for every URL. I want to know if its possible to do so in Codeigniter? and is it possible to show a URL like the above in Codeigniter?
Yes it is possible with Codeigniter routing,
In your application/config/routes.php paste the below code and change the controller and function name you used,
$route['([a-zA-Z0-9---_%])+/([a-zA-Z0-9---_%])+/([a-zA-Z0-9---_%])'] = 'your_controller_name/your_function_name/$1/$1/$1';
Unlike other frameworks CodeIgniter does not have a global template system. Each Controller controls it's own output independent of the system and views are FIFO unless otherwise specified.
For instance if we have a global header:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
<html>
<head>
<title><?=$title?></title>
<!-- Javascript -->
<?=$javascript ?>
<!-- Stylesheets -->
<?=$css ?>
</head>
<body>
<div id="header">
<!-- Logos, menus, etc... -->
</div>
<div id="content">
and a global footer:
</div>
<div id="footer">
<!-- Copyright, sitemap, links, etc... -->
</div>
</body>
</html>
then our controller would have to look like
class Welcome extends Controller {
function index() {
$data['title'] = 'My title';
// Javascript, CSS, etc...
$this->load->view('header', $data);
$data = array();
// Content view data
$this->load->view('my_content_view', $data);
$data = array();
// Copyright, sitemap, links, etc...
$this->load->view('footer', $data);
}
}
Yes you can set same page with different url setting on routing.
Refer below link for more info:
https://www.codeigniter.com/userguide3/general/routing.html
$route['controllername/country/state1/dist1/'] = 'catalog/product_lookup';
$route['controllername/country/state1/dist2/'] = 'catalog/product_lookup';
Improving the krishnaraj answer...
you need
website.com/controllername/method1/st/dist/
website.com/controllername/method2/st/dist/
//i´ve changed some variables for better understanding, and the "country" would
//be the main change
so at your controller you just
public function method1($st1,$st2){
$this->load->view('header');
$this->load->model('data_processing');
$data['data'] = $this->data_processing->any_model_function($st1,$st2);
$this->load->view('page_you_want',$data);
//here you choose the same view for both methods
$this->load->view('footer');
}
public function method2($st1,$st2){
$this->load->view('header');
$this->load->model('data_processing');
$data['data'] = $this->data_processing->any_model_function($st1,$st2);
$this->load->view('page_you_want',$data);
//here you choose the same view
$this->load->view('footer');
}
In the case of route configuration explained by Kavin Smk, noticed just a mistake, instead of
'your_controller_name/your_function_name/$1/$1/$1';
it shoud be
'your_controller_name/your_function_name/$1/$2/$3';
Well, there is always a lot of ways to do the same, choose yours...
Good luck!
My URL is:
http://localhost/CodeIgniterCms/admin/dashboard/otherpages/123
where,
Codeigniter Cms
- admin
-- dashboard (the controller)
--- otherpages (Method)
Controller code
public function otherpages($somedata) {
$this->render('admin/second_view',$somedata);
}
Code in second_view.php
<pre>
<div class="container">
<?php echo $somedata;?>
</div>
</pre>
But it is throwing error
Unable to load the requested file: admin/123.php
Try this within your otherpages function:
$data['somedata'] = $somedata; <br/>
$this->load->view('admin/second_view',$data);
If you look to laravel official documentation http://laravel.com/docs/4.2/templates
It says that giving this layout:
<!-- Stored in app/views/layouts/master.blade.php -->
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
Extended by this view
#extends('layouts.master')
#section('sidebar')
<p>This is appended to the master sidebar.</p>
#stop
#section('content')
<p>This is my body content.</p>
#stop
Will append to the section sidebar. But actually if you try is it doesn't append, it just override the content from the extended template.
I heard about others blade function like #append, #prepend, #parent... no one seems to work.
Beside, this example in the official doc which doesn't work, I find that the blade documentation is very poor. There's nothing about blade function like #parent for instance.
The example in the documentation from Laravel website does indeed seem to be flawed, but I think it's a markdown parsing problem on the website, the same docs on github show the correct code:
In any case #parent does indeed work. The example in the docs should look like this:
#extends('layouts.master')
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#stop
#section('content')
<p>This is my body content.</p>
#stop
A quick look in the Illuminate/View/Factory.php confirms what #parent does:
/**
* Append content to a given section.
*
* #param string $section
* #param string $content
* #return void
*/
protected function extendSection($section, $content)
{
if (isset($this->sections[$section]))
{
$content = str_replace('#parent', $content, $this->sections[$section]);
}
$this->sections[$section] = $content;
}
You can simply use #append...
#extends('layouts.master')
#section('sidebar')
<p>This is appended to the master sidebar.</p>
#append
#section('content')
<p>This is my body content.</p>
#stop
See here.
To understand how this works...
The compileStatements() method in the BladeCompiler calls the method compileAppend(), as you can see here:
/**
* Compile Blade Statements that start with "#"
*
* #param string $value
* #return mixed
*/
protected function compileStatements($value)
{
$callback = function($match)
{
if (method_exists($this, $method = 'compile'.ucfirst($match[1])))
{
$match[0] = $this->$method(array_get($match, 3));
}
return isset($match[3]) ? $match[0] : $match[0].$match[2];
};
return preg_replace_callback('/\B#(\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $callback, $value);
}
In turn, that inserts a call to appendSection() which looks like this:
/**
* Stop injecting content into a section and append it.
*
* #return string
*/
public function appendSection()
{
$last = array_pop($this->sectionStack);
if (isset($this->sections[$last]))
{
$this->sections[$last] .= ob_get_clean();
}
else
{
$this->sections[$last] = ob_get_clean();
}
return $last;
}
as mentioned before, I used #parent and it works fine for me. May be an example for extended title will helps:
master.blade.php
#section('title')
My Blog
#stop
<!doctype html>
<html>
<head>
#include('includes.head')
</head>
<body>
<div class="container-fluid">
<div id="main" class="row">
#yield('content')
</div>
</div>
</body>
</html>
includes/head.blade.php
<meta charset="utf-8">
<title>#yield('title')</title>
post.blade.php
#extends('master')
#section('title')
#parent
| {{$post->title }}
#stop
#section('content')
// Post Body here ..
#stop
Therefore, The Title will be rendered to be like this:
My Blog | My Post Title
Actually, this will render something like:
<title>
My Blog
| My Post Title
</title>
so you can use the section second parameter to set the values:
includes/head.blade.php
...
#section('title', 'My Blog')
...
post.blade.php
...
#section('title', '#parent | ' . $post->ar_name )
...
And this will render:
<title>My Blog | My Post Title</title>
So you will get rid of the lines inside the title,
Hope that's helps.
Note:
This is used for Laravel 5.2, Not quite sure but as I remember, it works for Laravel 4 too.
I have an application without controllers and read about controller layouts in laravel 4 documentation and this other article too, but I don't know where to start for implement it within routes (version 4), how can I do that?
Error received: InvalidArgumentException, View [master] not found.
app/routes.php
<?php
View::name('layouts.master', 'layout');
$layout = View::of('layout');
Route::get('users/create', array('as' => 'users.create', function() use($layout) {
//#TODO: load view using 'layouts.master',
// desirable: append 'users.create' and 'users.menu' views to sidebar and content sections.
//return View::make('users.create');
return $layout->nest('content', 'master');
}));
?>
app/views/layouts/master.blade.php
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
app/views/users/create.blade.php
{{ Form::open() }}
{{ Form::text('name') }}
{{ Form::submit('submit') }}
{{ Form::close() }}
app/views/users/menu.blade.php
<!-- This is appended to the master sidebar -->
<p>Create user</p>
Update: I modified example code to clarify what I want to do. Check app/routes.php and its comments
The code in your routes file is trying to nest the master layout within itself, which isn't really what you want. You're getting the error because 'master' would look for app/views/master.blade.php. That's easily fixed by changing it to 'layouts.master', but I wouldn't like to think what might happen...
The root cause of the issue you're having is the difference between "yielding" views from a Blade template, and nesting them from a route. When you nest a route, you need to echo it rather than using the #yield tag.
// File: app/routes.php
View::name('layouts.master', 'layout');
$layout = View::of('layout');
Route::get('users/create', array('as' => 'users.create', function() use ($layout)
{
return $layout
->nest('content', 'users.create')
->nest('sidebar', 'users.menu');
}));
/*
|--------------------------------------------------------------------------
| View Composer
|--------------------------------------------------------------------------
|
| Code in this method will be applied to all views that use the master
| layout. We use that to our advantage by injecting an "empty" sidebar
| when none is set when returning the view. It will error otherwise.
|
*/
View::composer('layouts.master', function($view)
{
if (!array_key_exists('sidebar', $view->getData()))
{
$view->with('sidebar', '');
}
});
// File: app/views/layouts/master.blade.php
<html>
<body>
#section('sidebar')
This is the master sidebar
{{ $sidebar }}
#show
<div class="container">
{{ $content }}
</div>
</body>
</html>
Laravel's View composers are a powerful tool. If you have any data (eg logged-in user info) used by all views that share the same template(s), you can use the composers to save injecting the data every time you load the view.
You could also use the #parent tag to append content, assuming you;re using blade for templating. E.g. (in the view)
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#stop
You don't need to use nesting views if you're using blade.
app/views/users/create.blade.php
You need to extend the master.blade
#extends('layouts.master')
#section('content')
// form stuff here
#stop
Now, all you need to do is call create.blade
return View::make('users.create')
Just throwing this out there as a possible solution using controller routing (whereas you can set the template from within the controller).
app/routes.php
Route::controller('something', 'SomethingController');
app/controllers/SomethingController.php
class SomethingController extends BaseController {
protected $layout = "templates.main"; // denotes views/templates/main.blade.php
public function getIndex() { // the "landing" page for "/something" or "/something/index"
$this->layout->content = View::make('something.index')->with("myVar", "Hello, world!"); // load in views/something/index.blade.php INTO main.blade.php
}
public function getTest() { // for "/something/test"
$this->layout->content = View::make('something.index')->nest("widget", "something.widget", array("myVar" => "Hello, World!"));
}
}
app/views/templates/main.blade.php
#include('templates.partials.header')
#yield('something')
#yield('content')
#include('templates.partials.footer')
app/views/something/widget.blade.php
I'm a widget. {{ $myVar }}
app/views/something/index.blade.php
#section('something')
I will go in the 'something' yield in main.blade.php
#stop
#section('content')
I will go in the 'content' yield in main.blade.php.
{{ $myVar }}
{{ $widget }}
#stop
?>
Now you can test http://myserver/something and http://myserver/something/test to see the differences. Note: not tested but as a rough example.
I'm sure my question is pretty straight forward, and I've been looking for an answer to this, but I can't seem to make it work. I want to do something like this:
<?xml version="1.0" encoding="UTF-8"?>
<configdata>
<dashboard>
<label>Dashboard</label>
<controller>dashboard</controller>
<action>index</action>
<module>global</module>
</dashboard>
<bills>
<label>Bills</label>
<pages>
<create-bill>
<label>Create New Bill</label>
<controller>bill</controller>
<action>create</action>
<module>global</module>
</create-bill>
</pages>
</bills>
</configdata>
Please note that in the <bills> section, I want to have a category with just a label, that way when I add styling later, I can hover over "Bills" and "Create New Bill" and other links will be shown, but clicking on "Bills" shouldn't do anything because it's just a category header.
I hope that makes sense.
You must specify a type for your page otherwise Zend_Navigation will throw an exception. In cases like yours I always use a Zend_Navigation_Page_Uri as page type and specify its uri to #. To apply this to your config file you could do this
<bills>
<label>Bills</label>
<uri>#</uri>
<pages>
<create-bill>
<label>Create New Bill</label>
<controller>bill</controller>
<action>create</action>
<module>global</module>
</create-bill>
</pages>
</bills>
The generated markup still contains a link but it will not point anywhere.
Moreover, since you need to bind some javascript to it in order to show the menu, you could even disable it by returning false in the click handler for that links.
In order to attach javascript callbacks (or some css) to that kind of link you may find useful to attach a class to those links. Within the same configuration file, you could with this code
<bills>
<label>Bills</label>
<uri>#</uri>
<class>fakelink</class>
<pages>
<create-bill>
<label>Create New Bill</label>
<controller>bill</controller>
<action>create</action>
<module>global</module>
</create-bill>
</pages>
</bills>
In this case the generated markup would be
<li class="fakelink>
Bills
<ul>submenu here</ul>
</li>
and you could easily select that kind of links with a javascript library. For example with jQuery you could do this:
$(function() { $('.fakelinks > a').click(function () { return false; }); });
There is actually another way to solve this.
You can set custom properties on all Zend_Navigation_Page just by defining extra configuration options in your xml/array/...
Then by using a dedicated partial to render your menu/breadcrumbs you can perfectly skip rendering the <a> tag or render a completely different markup based on these properties.
<!-- ... -->
<page1>
<label>Page 1</label>
<uri>page1</uri>
<link>false</link> <!-- Custom property -->
<pages>
<page1_1>
<label>Page 1.1</label>
<uri>page1/page1_1</uri>
</page1_1>
<page1_2>
<label>Page 1.2</label>
<uri>page1/page1_2</uri>
</page1_2>
<page1_3>
<label>Page 1.3</label>
<uri>page1/page1_3</uri>
</page1_3>
</pages>
</page1>
<!-- ... -->
Note: I'm using the breadcrumbs example here but most of the Navigation View_Helpers have a setPartial() method, including the menu helper.
Then in your view script or layout you just specify that your breadcrumbs helper needs to use a partial.
<?php
echo $this->navigation()->breadcrumbs()->setPartial('my_breadcrumbs.phtml');
And in your partial you loop over the pages in year breadcrumb trail and check the custom properties for each page.
<?php
foreach($this->pages as $page)
{
$properties = $page->getCustomProperties();
// Check if we need to render the link tag
if($properties['link'] !== false){
echo '<a href="' . $page->getHref() . '">';
}
// Render the label
echo $page->getLabel();
// And check if we need to render the closing tag
if($properties['link'] !== false){
echo '</a>';
}
}
Note: By using a partial you'll lose some default functionality of the Breadcrumb View_Helper like setLinkLast, setSeparator, ... but these shouldn't pose too much of a problem.
If you're happy for your category labels to be spans then just specifying an empty URI will do the job. Zend_View_Helper_Navigation_Menu::htmlify() is what renders a Zend_Navigation_Page:
/**
* Returns an HTML string containing an 'a' element for the given page if
* the page's href is not empty, and a 'span' element if it is empty
*
* Overrides {#link Zend_View_Helper_Navigation_Abstract::htmlify()}.
*
* #param Zend_Navigation_Page $page page to generate HTML for
* #return string HTML string for the given page
*/
public function htmlify(Zend_Navigation_Page $page)
Example output:
<ul class="navigation">
<li>
<span id="menu-staff">Staff</span>
<ul>
<li>
Book Holiday
</li>
<li>
View Booked and Remaining Holiday
</li>
</ul>
</li>
</ul>
I went about it in a different way:
nav:
User:
label: Account Services
uri: #fakeUri
Then in my template code:
<?php foreach ($navSettings as $navSetting): ?>
<?php if ('#fakeUri' === $navSetting->getUri()): ?>
<?php echo $navSetting->getLabel() ?>
<?php else: ?>
<a href="<?php echo $navSetting->getUri() ?>"><?php echo $navSetting->getLabel()) ?>
<?php endif ?>
<?php endforeach ?>
<page_bills>
<label>Bills</label>
<type>uri</type>
<pages>
</pages>
</page_bills>