Yii Layout view files: beginContent() method default value - php

I am playing with layout in Yii, and have one question need your help.
As I looked into layout view file: "column1.php" I see the beginContent() method:
<?php $this->beginContent('//layouts/main'); ?>
As I read a guiedbook "Agile web development with Yii", it said: [quote]The view being specified
here is our main layout page 'layouts/main'. If nothing is specified, it will use the default layout
specified either at the controller level, or if not specified at the controller level, at the
application level.[/quote]
As I understand that the layout specified at the controller level is "layout/column1" (as I have checked value $this->layout). so If I leave this layout empty in the beginContent() method, then it will render "layout/column1", am I right?
If so, the new loaded layout will reapeatly load the "layout/colum1" as there's no layout value pass to beginContent() method at this new loaded layout. and I iffer that this will return an error.
But, when I leave this layout value empty. the view file still load as normal. no error occured.
I am very supprised with it.
Could someone tell me what has happened?
Thank you.

Related

The technical reason is: No template was found. View could not be resolved for action "search" in class "LoginController"

Im new to typo3 and ive been trying to develop an extension for it. When I load the plugin to the page i get and error:
Sorry, the requested view was not found.
The technical reason is: No template was found. View could not be resolved for action "search" in class "LoginController".
In the login controller i have a function searchAction and I do have a template inside
\Resources\Private\Templates\StoreInventory\Search.html
What might be the error? I followed the documentation for extension development by typo3. I even downloaded the code from GIT and tried using that but no luck.
TL;DR: It should be placed at Resources/Private/Templates/Login/Search.html
explanation:
From your question I can tell you've used the documentation on https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/4-FirstExtension/6-adding-the-template.html
It is not wrong, but you've missed a vital step. When you take a look at the path they are using you'll see that a lot is done automagically. Let's break it down.
They have a controller with an action and a template related to it in the following path
controller:
\MyVendor\StoreInventory\Controller\StoreInventoryController
action: listAction
template: EXT:store_inventory/Resources/Private/Templates/StoreInventory/List.html
If you look closely you'll see that the template path is made up from several components.
the extension (EXT:store_inventory)
the default template directory path (Resources/Private/Templates)
The Controller name without the controller suffix (StoreInventory)
the Action name without the action suffix (List)
the .html suffix
If you take that information and apply it to your case it would be:
the extension (EXT:your_extension_name)
the default template directory path (Resources/Private/Templates)
The Controller name without the controller suffix (Login)
the Action name without the action suffix (Search)
the .html suffix
So the end result would be something like
EXT:your_extension_name/Resources/Private/Templates/Login/Search.html
It is true that you can use typoscript to change this behaviour or set overrides or extended templating for instance. But I think you're working from the default, and this should be the working path for you now
First check your templateRootPaths in Configuration/TypoScript/setup.typoscript
And set accordingly.
Try placing the file at \Resources\Private\Templates\Search.html as \Resources\Private\Templates is the default path unless you have altered it.

Unable to load controllers from another controller

I'm trying to load a controller inside another controller.
$data['com_top_menu'] = $this->load->controller('account/com_top_menu');
However, this seems to not work when I'm trying to load a controller that is located in the same folder as the controller I'm loading it from.
Tried loading controllers from other folders and seem to not load as well. It seams to load only from the 'common' controllers folder.
Edit:
Actually it seems that the controller is loading. If I place an echo in the middle of the loaded controller it will show the output before the template rendered. So, it looks like the controller is loaded and just doesn't output anything through the rendered view, unless it is a controller inside the common folder.
Files are all in place, controller loads, it just doesn't output anything through the view.
Few things for to load controllers-
1st - you can only load controller from same folders (admin/ catalog).
2nd - you can load controller from any subfolder, just need to pass correct loading path.
3rd - If Opencart hasn't that file than it will not display any error, result will be null/ false.
4th - If you are defining any function name then it will call that function else will call index function so in your case index.
5th - Please use this
return $this->load->view('your.tpl', $data);
Instead of
$this->response->setOutput($this->load->view('your.tpl', $data));
6th - Please enable your debug mode from php/ admin so that you will know any error if your code is throwing. Clear your error.log and then try to load controller.
7th - If these all points are code is not working then do 1 thing - add a blank controller with index function and just add one line so that you can return it's result from view then just
echo 'here';
In your view. If OC is not returning this result it's mean you have error in Opencart files else there is error in your code.
You can say these are same in a way (i am not saying completely and don't want to hurt anyone feelings ;)) but this code
$this->load->controller('account/com_top_menu');
is equal to (based on your autoloader)
$obj = new ComTopMenu; //assuming your class name
$data['com_top_menu'] = $obj->index();
so for your solution please check
- you have file com_top_menu.php in your catalog > controller > account >
- your file class name must be ControllerAccountComTopMenu (or any uppercase or lowercase combination but without _)
- your class must have index function because in your case it calling index.

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.

Difference between view.php and _view.php in yii

What is difference between view.php and _view.php in Yii?
Where I should use from _view or view in Yii?
By render() or renderPartial() I can render both? hasn't problem in performance or anything?
They are simply filenames, but by default view.php is used with a render() and _view.php is used with a renderPartial() (in the default Yii web application).
So if we stick to this convention, any view rendered by render() will be a "normal" file name, and anything rendered with a renderPartial() will have a prefixing _underscore.
Here is the difference between render and renderPartial (from here):
render() is commonly used to render a view that corresponds to what a user sees as a "page" in your application. It first renders the view you have specified and then renders the layout for the current controller action (if applicable), placing the result of the first render into the layout. It then performs output processing (which at this time means automatically inserting any necessary <script> tags and updating dynamic content) and finally outputs the result.
renderPartial() is commonly used to render a "piece" of a page. The main difference from render() is that this method does not place the results of the render in a layout. By default it also does not perform output processing, but you can override this behavior using the $processOutput parameter.

layout parameters lost after adding view resource to application ini

I want to add a view helper path in an existing project. To do this I have added the following line to my application.ini:
resources.view[] =
And in my bootstrap file:
$this->bootstrap("view");
$view = $this->getResource("view");
$view->addHelperPath(APPLICATION_PATH . "/../library/MyPath", MyNamespace");
Now I am indeed able to add view helpers to my path, so no problem there.
However, variables that I have added to the view in my Action Helpers are suddenly no longer accessible inside my views. I can retreive them inside my layout as usual so I know they get assigned properly.
I assign a variable in my Action Helper in the postDispatch:
$view = $this->getActionController()->view;
$view->myVar = $this->var;
Then in my layout
Zend_Debug::dump( $this->myVar );
results in: (string) "myVar contents"
And in my view
Zend_Debug::dump( $this->myVar );
results in: null
Since this is an existing project I need a general solution that I can use in either my bootstrap or application.ini
You can add paths to your view from application.ini:
resources.view.helperPath.MyPrefix = "/path/to/helpers"
Hopefully this will fix the problem. I'm assuming that the way you do it, an existing view-instance is somehow overwritten, which destroy previous changes. You could try fetching the view from the frontcontroller, set the helper-path, and pass it back to the frontcontroller.
After some research this is what I found out.
Edit: Removed text concerning differences between view variables and layout variables - see comments by #pieter and #david weinraub
The postDispatch of the Action Helper fires AFTER the view is rendered, but BEFORE the layout is rendered. Therefore, any variables assigned to the view in the postDispatch of the action helper were unavailable in my views, they didn't exist yet.
I am very curious why this behaviour occurs ONLY when actively bootstrapping the view? I will leave the question unanswered, maybe someone can clarify this.

Categories