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.
Related
I have a class
class ModuleController extends Controller {
private static $allowed_actions = array(
'index'
);
public function index(SS_HTTPRequest $request) {
return $this->renderWith(array("Module/HomePage", "Page"));
}
}
And two templates, one is Page.ss and another is Layout/Module/Homepage.ss. In Page.ss I have a bunch of html and $Layout. Looking through other areas online I would have expected the page to render according to Page.ss and the layout variable to render with Layout/Module/HomePage.ss however it just renders with Page.ss. How can I get my page to render with the base template of Page.ss and render the $Layout with my controller defined layout?
EDIT
So if I put the Homepage.ss up one level (i.e. no 'Module' directory) this code works. Is it possible to have directories in the Layout directory as I think this folder will get messy overtime without some structure. It also seems strange that the folder path works when setting the template but not the layout.
EDIT 2
Looks like This is a core problem I've just discovered a different way and looks to be addressed in SS4 so looks like this can't be done in SS3.
I have a sidebar in my site that receive some information from db and I can't use controller for retrieve data because I have different controller and same sidebar. How can I print this data in view page.
when I wrote in P.h.P code in the view it shows an error that it cant define variables.
How could I do this?
When you find that you need the same code in many different controllers a "custom library" (class) is the perfect choice. Documentation for creating your own libraries is found HERE.
Controllers should be using models to get data from the database. Custom libraries can also use models just like controllers. Here is a very basic custom library called Sidebar. It depends on a model (sidebar_model) that will not be shown. The purpose of the Sidebar library is to return the variables need by the sidebar_view file.
File: application/libraries/Sidebar.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Sidebar
{
protected $CI; // Read the documentation link to see why this is needed.
public function __construct()
{
$this->CI = & get_instance();
$this->CI->load->database(); //only needed if not already done
$this->CI->load->model('sidebar_model');
}
public function get_sidebar_data()
{
return $this->CI->sidebar_model->get_sidebar();
}
}
The library method get_sidebar_data() returns the variables for the view.
Here is a controller that uses the custom library. It will use the custom library and a view file (not shown) containing HTML for the sidebar.
File: application/controllers/Main.php
class Main extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('sidebar'); //can also be autoloaded
}
public function index()
{
$data['sidebar'] = $this->sidebar->get_sidebar();
$this->load
->view('banner')
->view('sidebar_view', $data)
->view('main_view')
->view('footer_view');
}
}
Any other controller that needs to show the sidebar would use this same pattern.
This controller loads four different view files and is using "method chaining" which is encouraged. Method chaining executes a tiny bit faster. But the best reason for using it? Less typing.
The method chaning could also be type like this:
$this->load->view('banner')->view('sidebar_view', $data)->view('main_view')->view('footer_view');
But, IMO, putting each ->view() on a separate line makes it easier to read.
You can create a helper for your common tasks. Then create a function for your sidebar and call it where you need it. Check this link for more details about creating helper
You can also create a library for it. Although it will not a very good choice.
create sidebar (view page) and Call model directly inside that sidebar (view page).
secondly call sidebar (view page) directly inside all other view pages.
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 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!