where is $content variable declared in Yii? - php

I am using Yii Framework. In the view, main.php, there is a reference to the $content block
<?php echo $content; ?>
I could not find it anywhere in the model or elsewhere in the demo project. Can someone shed light on this? Or may be this variable is never declared? I have not modified the demo project yet.

The $content value in layout files contains the rendered content of the template specified as the first attribute of the render command. (It's automatically created so I wouldn't use "content" as an additional variable name or it could cause confusion.) The variables that you pass as an additional array argument in the render statement are made available to the template you are calling, not to the layout.
If you have nested layouts, the value of $content cascades from parent to child.

All your controllers are derived from CController class. CController has a function named render which you call it for rendering your views. It works like this:
beforeRender is called.
renderPartial is called on your view file, and its output is stored in $output.
renderFile is called on the layout file, with a parameter named content like this:
$this->render(layoutFile, array('content'=>$output));
So the $content is coming from here. You can see the actual code here: Source code, and documentation here: Documentation

Found answer from Yii Documentation / Layouts,
For example, a layout may contain a header and a footer, and embed the
view in between, like this:
......header here......
<?php echo $content; ?>
......footer here......
where $content stores the rendering result of the view.
It is indeed all the text in one of the view (in my case index.php). $content basically takes the content of view. It is not declared anywhere and it is be default. As the answer said, you should not use declare/use $content in your code.

I think its being set from the controller which is calling this view.
In the controller look for something like the following
$this->render('main', array('content'=>"something here"));

Related

opencart controller and children

I have two questions:
In a controller ControllerMainIndex in opencart we do define
$this->children = array("common/footer");
Then how should I utilize it in the main/index.tpl file?
It is a little bit vague for me. We have told the controller to get footer but in the main file how should we specify their position?
Second question, I create controller ControllerMasterNewPage and then in I $this->render() (after setting the template to "master/newpage.tpl").
But how should I access this controller? I means what should be typed in the browser for this controller to be process and have output?
1) Just call <?php echo $footer ?> it will output that child content
2) http://example.com/index.php?route=path/MasterNewPage/actionName
e.g.: We have file in {root}/catalog/controller/product/category.php (class name ControllerProductCategory), call it: http://example.com/index.php?route=product/category

Where does this variable come from in Codeigniter? And are there any more?

In the default Codeigniter installation there is the "welcome" controller which has a "index" action which loads the "welcome" view. This works as expected.
However, upon inspecting the "welcome" view I can see this variable in the footer.
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
From what I understand the variable {elapsed_time} is an example of using the built in template parser with text representations instead of using PHP short tags to echo variables.
But inside the "welcome" controller the only lines in the "index" action are these.
$this->load->view('welcome');
And it doesn't pass $data['elapsed_time']='xxx'; which means that I can't figure out where the variable elapsed_time is coming from!
My question is this.
Where does elapsed_time get defined? Is it built into the template parser class (and therefore available to be used without first defining it)? If so, where is a list of these other pre-defined varaibles? I would like to know what else I have access to as had I known that elapsed_time was available to me it would have been very useful. Does anyone have a list of the template parser pre-defined variables?
Thanks in advance.
elapsed_time defined output class. this class is initialized automatically by CodeIgniter.
more info here
CI will replace the string of "{elapsed_time}" in the final output string with actual "total_execution_time ". You can check it in system/core/Output.php Line 366 of v213

Zend Framework passing a variable to all views from a controller to anything outside of $this->layout->content()

Ok, the subject makes no sense so Ill try to better describe it here.
Zend Frame work in use here. And I have run into a problem passing variables to my views, well the views included into the "top.phtml" that make up the template. What I am trying to do is implement a breadcrumb concept. The bread crumb file is included into the top.phtml before the content view file. So the breadcrumb variable isn't defined as far as the breadcrumb file is concerned.
I can print_r my array of settings for the breadcrumbs within the controllers view, no problem so it is working I know that much, just anything above that view in particular in the order of things can't get the variable. So I guess what I am looking to have answered is is there a means off passing a variable to the overall scheme of things similar in concept to
$this->view->variable_name = blah;
where something as high as the top.phtml can pick it up for use?
You may be looking for Placeholders.
Example:
Setting a placeholder value from a controller:
$this->view->placeholder('some_placeholder_name')->set('blah');
Setting a placeholder value from a view
$this->placeholder('some_placeholder_name')->set('blah');
Retrieving the placeholder value in a view script or layout:
$value = $this->placeholder('some_placeholder_name');
Placeholder content is rendered towards the end of your application execution so the value set in your controller should be available in your top level top.phtml view script.
I think this will work:
$this->layout()->breadcrumbs = ...
And then print $this->layout()->breadcrumbs in your top.phtml.
Zend Layout
After sending hours trying to get placeholder() to work with partialLoop(), I finally gave up and hacked a fix to pass vars to a partial:
$vars = (array) $this->getVars();
foreach ($this->rows as $row) {
$partialVars = array(
'row' => $row,
'vars' => $vars,
);
echo $this->partial('row.phtml', $partialVars);
}
ugly, but it worked.
+1 for everyone for giving me a clue, however none of the above worked well in my favor. However between them all, they lead me towards finding my answer which is
Zend_Registry
In my Controller I built my array and passed it to Zend_Registry like
$breadArray = array(
array("icon"=>"i_robot", "href"=>"systems/"),
array("href"=>"metrics","text"=>"Metrics")
);
Zend_Registry::set('breaded_crumbs', $breadArray);
Then in my breadcrumb.phtml which is loaded before the content and view I used
print_r(Zend_Registry::get('breaded_crumbs'));
to see if it was working, and it gave me the array's so I for anyone in the future looking to extend a variable outside of the view itself, the registry seems to be the way to go. I tried placeholder and layout, both gave me errors about not being something or another, and when I got them to work in part I wasn't getting what I was expecting.

How to run an action of controller file from .ctp file?

I want to call an action along with its .ctp file of a controller file from another .ctp file.
for e.g.
users_controller.php has an action called list_category() and I want to call it from /app/views/pages/index.ctp file. Not just call list_category but also want to show its html output(I mean also list_category.ctp should be rendered).
Thanks.
Create an element, for instance list_category.ctp.
In the element use requestAction to get the data:
<?php
$categories = $this->requestAction('/users/list_categories');
?>
<?php foreach($categories as $category): ?>
<?php // Your display code goes here ?>
<?php endforeach; ?>
In your controller make sure you return the data you want.
<?php
function list_categories() {
return $this->User->Category->find('all');
}
?>
You can reuse the code for your list_category.ctp view.
There is an overhead when using requestAction but it is often less than people believe.
Can you do that with routing? I'm not sure of the syntax off the top of my head but I think you can specify that method that the controller runs when you land on that page
It seems wrong, what is it that you're trying to accomplish? How about elements?
How about calling the controller from your main controller, then pass its results to your layout. Finally use an element to render the output there and also use the element to render the output on that other controller too. That way you don't have duplicate layouts. Just one element used by two controllers.
This is very similar to the way Rails creates its layouts when you 'bake' them. It creates an equivalent of a element to use in the add and edit layouts.
This can be done with requestAction, but be aware, that it's expensive and you should be careful with it.

What's the proper MVC way to do this....?

Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I'm actually using Kohana).
I'm new to MVC and don't want to get this wrong... so I'm wondering if i have tables:
categories, pages, notes
I create a separate controller and view for each one...? So people can go to
/category/#
/page/#
/note/#
But then lets say I want to also be able to display multiple notes per page, it would be bad to call the note view in a loop from the page view. So should I create some kind of a function that draws the notes and pass variables to that function from the note view and from a loop in the page view? Would this be the best way to go about it, if not how else should I do it...?
Thanks,
Serhiy
Yes, instead of just passing 1 entity (category, page, note) to your view, pass a list of entities. With a loop inside the view, you can display the whole list.
That view may call another one (or a function) that know how to display one entry.
I would personally have a "show" method for one item and a "list" method for multiple. In your controller you can say something like $page_data['note'] = get_note(cat_id,page_id) for the "show" method and $page_data['notes'] = get_all_notes(cat_id) for the "list" method.
Then in your view, you loop over the $page_data['notes'] and display HTML for each one. If the list view is using the same "note" HTML as the "show" view, create a template or function to spit out the HTML given a note:
// In your "list" view
foreach($n in $page_data['notes']){
print_note_html($n)
}
//In your "show" view
print_note_html($n)
The print_note_html function can be a helper method accessible by all views for Notes. Make sense?
You can loop in the View. The View is allowed can also access the model in MVC. See: http://www.phpwact.org/pattern/model_view_controller
You don't need to have a controller (or model) for each table.
In CodeIgniter I create a separate helper file where I put functions that return the markup for UI elements that may need to be included multiple times in the one view.
In your example, I would create a function to return the markup for a note.
application/helpers/view_helper.php
function note($note)
{
return '<div class="note">' .
'<h2>' . $note->title . '</h2>' .
'<p>' . $note->contents . '</p></div>';
}
I would normally auto-load this helper file. And then in the view I would do something like this.
echo note($note);
For a list of notes in a view, I would iterate the list calling this function.
<div class="note-list">
<?php foreach ($notes as $note) : ?>
<?php echo note($note); ?>
<?php endforeach; ?>
</div>
I found that including a view many times in another view was slow. Thats why I did it this way.
Edit
I just dug into the CodeIgniter Loader class and sure enough a PHP include is being done every time you call
$this->load->view('view_name');
This means that if you use this method to display a list of 20 notes, you're going to be doing 20 separate includes.

Categories