using layout in themes from inside module Yii PHP - php

In Yii application I used own theme(demirTheme) have modules(retail, corporate), and layouts(main, layout_retail, layout_corporate).
When rendering one page(ex index) I want my app to render page, related layout (retail vs corporate) and then main accordingly. How can I achieve this?
I read layout tutorials and questions no help. They only mention about changing layout path, setting default layout for module, and so on.
I tried
to put all layouts in themes layouts folder,
to put main layout in themes layouts folder and other two in modules layouts folder respectively by their module,
to put all layouts in protected/view/layouts folder,
to put all layouts in modules and one copy of main layout for each module
But can't get it work. I tried changing layout path and other settings.
Is there any way? Have you done layout rendering like this? Any suggestions welcome.

add public $layout='//layouts/layout_retail'; in the controller you want to apply the layout to (so in every controller in the retail module for example). The views should be in "protected/view/layouts"
http://www.yiiframework.com/doc/api/1.1/CController#layout-detail
I am also sure you can put the layouts in the modules map. I however use the above methode for our admin module.
http://www.yiiframework.com/doc/api/1.1/CWebModule#layout-detail
EDIT:
You retail or corporate layout should be then like the code underneath. So the retail or corporate layout would be inside the main layout. Clearly it should contain more then just this, but the content of you layouts should be within $this->beginContent('//layouts/main'); and $this->endContent();
<?php $this->beginContent('//layouts/main'); ?> //main
<div id="content">
<?php echo $content; ?> //viewOfRetail
</div>
<?php $this->endContent(); ?>

For the newcomers: in many help forums of the Internet, when someone asks about theming a module, everyone suggests a path alias to the themes folder, and I disagree with those other forums. I think this is wrong, because it implies modules to be splitted, and modules are supposed to be a black-box that can be used across projects. The advice given in such forums would only be valid if a theme is shared among several modules. If someone wants to "package" a theme inside a module, she can:
-add an init function to the controller of the module
-inside that init, use the class attribute layout and a path alias, like this, supose a module whose id is "Sample":
then you add, to SampleController.php:
public function init() {
//BELOW: it will use the layouts/main.php inside the module.
$this->layouts = "sample.views.layouts.main";
}
Yo can check about path alias here:
http://www.yiiframework.com/doc/guide/1.1/en/basics.namespace

Related

October CMS Component Load on Layout

Im new to October CMS
I read the documentation and its states that components can be used inside a layout on the PHP Section (https://octobercms.com/docs/cms/layouts)
i want to create a component that will be used as configuration file for my theme, declaring global variables, that will be used on all pages, but will also be used for all layouts i will create. but i cant find how to do it via code. Like include a file. i want this to used this parameters inside the PHP Section of the layout and the pages.
If components is not the best way, can you ppl sugest me what is the best way
I did before search a lot to find some way that I can share variables to all my layouts and pages but couldn't find anything.
So I tried my own trick and it worked.
In your frontend you must have header.htm partial. so in the code section in it write a onStart() function and set your global variables so you can access them from any layout or page which includes the header partial.
For example in your header.htm code section:
function onStart() {
$this['my_var'] = ['name' => 'Ahmed', 'age' => 17];
}
That way you can access my_var variable wherever you want in all your layouts and pages which header.htm partial is included.

Style admin page in Yii

Im here again with a question about yii framework.
I've got a page under views/myviewname/admin.php.
I've got a page under views/myotherviewname/admin.php.
Now i want to give those pages another style. But how do i do that?
I've created a page under themes/classis/views/myviewname/admin.php and in that file i got this:
<?php /* #var $this Controller */ ?>
<?php echo $content; ?>
But i get an error. Because $content is not defined.
How do i style those pages? Would be nice if i can style all admin pages at once.
First of all, this is undeniable that $content variable will be known as undefined, since it can only be used in Layouts, not Views.
As you probably know, if you already have set a theme for your application(in main config file by 'theme'=>'myTheme'), Yii looks for that into themes/myTheme and all views will be rendered in themes/myTheme/views/x/y.php instead of views/x/y.php. Also, your layouts will be overridden by layouts located into themes/myTheme/layouts.
Now, lets assume that we want to create 2 themes:
DarkTheme
LightTheme
We should create structures like below:
+themes
+darkTheme
+views
+layouts
+main.php
+myLayout1.php
+myLayout2.php
+myController
+myView1.php
+lightTheme
+views
+layouts
+main.php
+myLayout1.php
+myLayout2.php
+myController
+myView1.php
We have a main.php which holds our base theme structure(skeleton), and 2 layouts named myLayout1.php and myLayout2.php respectively. Also we already defined a default layout into our base controller(Usually Controller.php) like below:
public $layout='//layouts/myLayout1';
Now, we have a main layout which shows everything inside myLayout1 by default. We can change layout in out action like below:
$this->layout="myLayout2";
Also we can change application theme like below:
Yii::app()->theme="lightTheme";
Note: Theme name is case-sensitive. If you attempt to activate a theme that does not exist, Yii::app()->theme will return null.
Above codes can be written into beforeAction() method or every action. Please note that, if you render myView1($this->render('myView1')) and if the theme is set to darkTheme, Yii will render themes/darkTheme/views/myController/myView1.php instead of views/myConteoller/myView1.php.
To be more clear, $content will be used in layouts. Also, this is remarkable that, $content will be replaced by everything inside a view. So if you want to modify the whole page's schema, you must modify main.php layout. In front, if you want to modify the style of a view's content, you need to modify your layout.

Running two versions of a site from same Yii code

I have two versions of my project. For one i use a different CSS and index page and for another i use different. Rest of the things that is controller, models and components are same. The only difference is in view(one or two files) and CSS.
Is there any way to manage this? Like when the URL is URL1 then use CSS1/View1 folder and when url is URL# use CSS2/view2 folder. I have gone through the modules section of Yii but i don't think they are what i need here.
So now I started to use themes. My folder structure is like:
WebRoot
- assests
- css
- images
- protected
- themes
- theme1
-views
-site
-layout
-template
- theme1
-theme2
-views
-site
-layout
-template
In my controller I have done this:
public function init() {
if (SITE_TITLE == 'xxxxx')
Yii::app()->theme = 'theme1';
else
Yii::app()->theme = 'theme2';
parent::init();
}
Which sets theme correctly. but i keep getting file not found as renderer is looking in protected.
I think, you need use themes. Here is documentation: http://www.yiiframework.com/doc/guide/1.1/en/topics.theming
UPDATED after discussion
Trouble in ETwigViewRenderer and it working with themes
If you want to change entire layout, perhaps this is a good way to do:
Setting Layout in Yii
In case you want only to change css they why don't you rely on request uri or domain name?
Yii::app()->getBaseUrl(true)

Multiple template in codeigniter

In one of my application I integrated a codeigniter template using HOOKS method... Its working pretty well ... The hooks/ template will call in Controllers Constructor ..
the 'default.php' is located in views folder ...
But I need 2 templates for my proj .. Can any one help me how to handle this ?
Please help
Look for the 'Themes' library... each theme has its own folder inside the views folder...and its called like this : 1st line is template 2nd line is the page
$this->themes->set_theme('theme');
$this->themes->set_template('template')
then in your controller its invoked using: $this->themes->view('view');
simple insert a {page_content} tag into your template where you want to insert the page code.
It may still be available on the CI wiki.

Magento module - overridden a controller, adding templates

I am currently working on a Magento extension, and I have overridden a core controller, which works fine.
I have now added a new action to my controller. Problem is that whenever I call the action a blank page is produced. If I echo something it is displayed correctly.
Therefore I dug into the core of the Customer module and controllers. I saw there that methods like indexAction() implement the layout this way:
<?php
public function indexAction()
{
$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->_initLayoutMessages('catalog/session');
$this->getLayout()->getBlock('content')->append(
$this->getLayout()->createBlock('customer/account_dashboard')
);
$this->getLayout()->getBlock('head')->setTitle($this->__('My Account'));
$this->renderLayout();
}
I transferred this to my own action and the layout is now rendered correctly. Now for the question:
No matter what I enter into the ->createBlock('...') call, nothing is rendered into the content area.
How do I specify the location of my own block to be rendered as the content of the page while still decorating it with the layout?
I tried fiddling with the xml files in /design/frontend/base/default/layout/myaddon.xml but couldn't really make it work.
Covering the entirety of the Magento layout system in a single StackOverflow post is a little much, but you should be able to achieve what you want with the following.
$block = $this->getLayout()->createBlock('Mage_Core_Block_Text');
$block->setText('<h1>This is a Test</h1>');
$this->getLayout()->getBlock('content')->append($block);
Starting from the above, you should be able to build up what you need. The idea is you're creating your own blocks, and then append them to existing blocks in the layout. Ideally, you're creating your own block classes to instantiate (rather than Mage_Core_Block_Text), and using their internal template mechanism to load in phtml files (separating HTML generation from code generation).
If you're interested in learning the internals of how the layout system works, you could do a lot worse than to start with an article I wrote on the subject.

Categories