I need to load a profile layout and then have all of the profile views load into that layout without re-loading the layout, so that I can cache a big json object in the clients browser, to allow for data to be available on subsequent views, without having to do a new HTTP request for every view in my profile controller.
Is it possible to load the layout template and then load views into it, by disabling their layout template, so that the layout template never re-loads, only the view within?
Another option would be to have an iframe inside of a layout and then load the views into the iframe, by disabling their layouts, so that just the view is rendered.
Just need to know if this is possible and what the best option would be.
Thanks in advance :)
In the views where you do not want a layout try:
$this->_helper->layout->disableLayout();
This should prevent the layout from being shown.
If, for some reason, you want the same action from the same controller called many times but load the layout once (must be a better way to organise yourself) but you could always try:
public function indexAction()
{
static $firstTime = true;
if($firstTime){
$firstTime = false;
} else {
$this->_helper->layout->disableLayout(); //disable layout
}
}
I'll try to give a better example:
Lets say you have a controller foo with 3 views: index, contact, news.
class Foo_Controller extends Zend_Controller
{
//...
public function indexAction()
{
//normal view layout loads
}
public function contactAction()
{
//normal view layout loads
}
public function newsAction()
{
//no layout for this
$this->_helper->layout->disableLayout(); //disable layout
}
}
So the views that need a layout have one. The ones that do not do not. Unless you are saying layout but really thinking of something else then Zend_View_Helpers_Layout you will have t post some code to help us help you!
Related
I'm working in a website where part of it I have the static html, so I created a layout and in it I'm inserting the static content using views. My problem is as this website has many pages I feel wrong creating an action for each url. So I implemented the controller below:
class PageController extends ControllerBase
{
public function initialize(){
$this->init();
$this->view->setLayout( 'website' );
}
public function indexAction ($url=''){
if($url == 'about')
$this->view->pick('page/about');
}
}
When I set the controller view to render $this->view->pick('page/about'); it doesn't insert the view in the template. It renders only the view.
Is there a way to render the view within the layout, and is there a better approach to what I'm doing?
Thanks for any help
To load a template you should use
$this->view->setTemplateAfter('website');
instead of $this->view->setLayout( 'website' );
By using $this->view->pick('page/about'); you are overwriting the layout set by $this->view->setLayout( 'website' );, resulting in only seeing the page/about layout.
I am new in yii, and trying to import a existing PHP site into this framework, so i want to remove default layout style of yii, just wanna show my view page.
Is there any way to do so?
like, when i load a view
$this->render('myview');
then only myview.php should be render.
I didn't found any help anywhere.
Just put $this->layout = false; in your action or a property public $layout = false; in your controller if you want it disabled controller wide.
Use the renderPartial function.
$this->renderPartial('myview', array('model'=>$model));
You can do other things, like assign the markup to a variable and do things like echo, manipulate and save it.
The renderPartial will not load the page layout.
// for disabled in whole controller actions use following method in controller
Class siteController extend controller {
Public $layout = false;
}
// disable for particular action in controller
Class siteController extend controller {
Public actionIndex (){
$this->layout = false.
}
}
I have an admin login page that I want to render without the layout. How can I render a view in Yii2 without rendering the main layout?
This can be done using renderPartial() method.
You can get more from the official documentation. Here's a link!
In your controller you can assing layout for all actions of controller or turn it off:
class AdminController extends Controller
{
// public $layout='//admin';
public $layout=false;
OR you can do it for only one action:
public function actionIndex()
{
$this->layout = false;
You can use renderPartial to exclude header and footer of layout from view file. However if you renderPartial it will not load asset files (css and js files). To load asset files without layout you can use renderAjax.
I've created Yii quiz module which I also want to use in Facebook app (Facebook side).
It can be done trough iframe, but generated page have got also menus and many other unnecessary (in this case) stuff. Is it possible in Yii to show only generated module code without rest of the website?
Try and use a different layout for that module. First you can add a layout folder and layout files to that module, say : /protected/modules/quiz/views/layouts/quizlayout.php. So this new quizlayout.php should be your layout for all the views in this module.
To do that you can set the layout property of the quizmodule in the QuizModule class's init(), like so (in QuizModule.php):
class QuizModule extends CWebModule {
public function init() {
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'quiz.models.*',
'quiz.components.*',
));
$this->layout='quizlayout';
}
//...
}
Now by default, gii generated modules' controllers are subclasses of the Controller class in component/Controller.php file. And that Controller class defines a layout, so if you have that same structure, then the above method will not work, and you'll have to override the layout within your modules' controllers. However instead of going inside each controller and adding a line, you can instead do this in the beforeControllerAction($controller, $action) function in QuizModule.php :
public function beforeControllerAction($controller, $action) {
if(parent::beforeControllerAction($controller, $action)) {
// this method is called before any module controller action is performed
// you may place customized code here
$controller->layout='quizlayout';
return true;
}
else
return false;
}
Edit:
Of course your quizlayout.php should not have code for menus, and any extra stuff, but at the very least the echo $content line should be there, as also mentioned in eskimo's answer.
In you protected/views there is a file called "main.php"
This is your main layout file, that gets rendered around any view called by $this->render
To remove the menu etc.. remove everything within the body except for the line:
<?php echo $content; ?>
Obviously leave in all the stuff in the head (.css files etc...)
I'm going to do my first site in code ignitor, a fairly basic site like this:
home
login / register
members area
protected page 1
protected page 2
protected page 3
general info section
page 1
page 2
page 3 (dynamic table of reports)
about section
page 1
page 2
blog section
listing
article page
I've gone through a couple of basic tuts and have read some of the documentation but still feel unsure on what would be the best way to structure this. Could anyone that is experienced with CI show me an example of how they' do it?
some specific Qs are:
header with nav panel will be the same on all pages. normally i'd code that as an include with if/else to show highlighted current section. I guess I'd just keep this as an include (view) and either load it first via the controller or include it in every view?
I'd envisage having a model called 'user' which will handle the login and registration, a model called 'blog' and a model called 'reports'. Does that sound right?
for static sections like about, I guess there'd be no model and i'd just have a controller with a function for each static page? i.e. about.php with page1(), page2() and all they do is load static views?
1 -> To fix this problem, I decided to use my own controller like this
Using CI 2.x, create a file under app/core called MY_Controller.php like so :
<?php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
public function loadView($view) {
$this->load->view('header');
$this->load->view($view);
$this->load->view('footer');
}
}
And then I extend this controller instead of the CI one. Be sure that your $config['subclass_prefix'] = 'MY_'; in the config.php file.
2-> yes
3-> thats about it :D
I'm a newbie here (codeigniter) but:
For headers/footers I adopted the template strategy from here (first alternative). Worked nice.
Before models, I'd plan the controllers -roughly one per each section. I made all them inherit from a MY_controller here I place common funcionalilty. And yours models seems about right to me. I think them rathar as DAOs, or "service objects" that provide access to the database and not much more. GEneral intelligence of the site (if needed) should be in a custom library, or inside the controllers.
Yes.
You should use an CI library to handle your user registration and per page authorisation.
Here's a very simple example on how you could do it. Keep in mind that CI uses the MVC pattern
class Reports extends CI_Controller {
public function __construct() {
parent::__construct();
// load database if needed
// load a model if needed
}
public function page() {
//get the page requested
$page_id = $this->uri->segments(2);
// based on the page_id do something.
$data['somedata'] = 'About us data here';
// this is an actual file loaded from the template view
$data['maincontent'] = 'my_page';
$this->load->view('template',$data);
}
}
class About extends CI_Controller {
public function __construct() {
parent::__construct();
// load database if needed for this page
}
public function page() {
// same here
//get the page requested
$page_id = $this->uri->segments(2);
// based on the page_id do something.
$data['somedata'] = 'About us data here';
// this is an actual file loaded from the template view
$data['main_content'] = 'my_about_page';
$this->load->view('template',$data);
}
}
in the template file
$this->load->view('template/header');
$this->load->view('template/nav');
$this->load->view($main_content);
$this->load->view('template/footer');