I have Entry controller with index action that lists all entries, and of course views/scripts/index.phtml. I also have the main page index/index.phtml. How can I include entry/index.phtml in index/index.phtml so I can see the results of entries as part of the structure of the home page?
try something like this towards the end of your indexAction() in the index controller:
$this->_helper->actionStack('index', 'entry');
Alternatively, I think you may be able to to do think in the index/index.phtml script:
<?php echo $this->action('index', 'entry');?>
First example is the actionStack action helper the second is the action view helper
Good luck!
You may create view helper for this, in which you:
retrieve the data (e.g. from database)
pass the data to the view
render the view you need ($this->view->render('pathoto/scriptname.phtml')). You may also add script path using addScriptPath().
Then use this helper in those two scripts you need.
If AJAX it the root of your needs, take a look at actionContext and ajaxContext action helpers.
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html
Related
I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.
How do I use my controller when I include my view?
I'm on Laravel 3.
Right now I have my controller :
public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}
My route (which obv only work on GET /current) :
Route::get('current', 'sale#current');
My master view
#include('sale.current')
Then my sale.current view calls $sales
#foreach($sales as $sale)
Thanks!
So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.
When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.
Here is the official documentation link : https://laravel.com/docs/master/views#view-composers
Here is the more detailed version of it :
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers
This is the standard way of achieving it without any patch work.
Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:
Route::get('test', 'TestController#test');
In TestController I define the action test as follows:
public function test()
{
return View::make('test.home')->with('data', array('hello', 'world', '!'));
}
According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:
// home.blade.php
<h1>Message</h1>
#include('test.test')
and
// test.blade.php
<?php print_r($data); ?>
When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.
I hope that can help you.
I wanted the functionalities of view files to run in controller file also.
For example, I wanted $this->escapeHtml() which runs in view file alone to run in controller through some means like $this->...->escapeHtml()
Is this possible? Kindly help.
You need to get the ViewHelperManager and extract the EscapeHtml helper. This is a one example how to do it from the controller:
$viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
$escapeHtml = $viewHelperManager->get('escapeHtml'); // $escapeHtml can be called as function because of its __invoke method
$escapedVal = $escapeHtml('string');
Note that it is recommended to escape and display the output in the view scripts and not in the controller.
I'm using laravel with controllers layout. But there are some parts of my app where I don't want to use a layout (for example, when returning data to the payment gateway request, for wich I send XML data). I just want to pass data to my view and render it alone, with no need for a layout.
How can I do that? I've been trying some approaches but none worked for this. I can successfuly change what layout to render, but I can't set to render the view without a layout.
Thanks!
Edit: Let me explain it better
My default layout is set in Base_Controller. Then all my controllers extends it but in one of them I need no layout, as I told above. Maybe I need to unset the default layout or something like that, I'm not sure.
You can simply return something from your controller action to bypass the layout.
function get_xml($id) {
$user = User::find($id);
return View::make('user.xml', $user);
}
On your controller functions, you can simply return a string, which will be thrown back to the browser as-is. Alternatively, you can craft a Laravel\Response object, which will allow you to fine-tune your site's output a lot more than just returning a string.
The Response class has a few tricks up its sleeve that are not mentioned on the docs: default return, JSON, forced download.
You're more interested in the first one, which will allow you to correctly set the content-type of the response to application/xml. In addition to this, you can still use views for XML! Generate the view as you would with View::make, but instead of directly returning it, store it in a variable. To render it, call render() on it - it will return the output.
A simple way....
suppose there is a main layout
<body>
#yield('content')
</body>
This content will be where the view will be inserted.
Now,
if you want to use layout, Make the view page like this:
#layout('main')
#section('content')
blah blah your content
#endsection
If you don't want to use layout, omit the codes above.
In controller, the code will be same for both the files.
return View::make('index');
I need to render the view for controller=user action=profile from a
controller = b action =c
i.e. /b/c will render the same view as when I surf to /user/profile
How can this be achieved (except using include inside the view file) in Yii?
What code do I have to put in the controller?
To render which view is not decided by the controller or action id, you can modify it easily. Just change this line in your b controller c action:
$this->render('[path alias to your user/profile view]',array(
$model=>[your data provider]
));
You can check the manual to find how to make a path alias, here's an example:
application.views.user.profile
You can also use a "root view path" syntax to render any view file by starting with "//" like:
$this->render('//user/profile');
I think I can't see the tree in the wood.
I'm using Zend Framework, with an layout.phtml which is rendering and partial
<?php echo $this->partial('_header.phtml') ?>
My goal is to render an form from my IndexController into the "_header.phtml" with
<?php echo $this->form; ?>
How can I pass the form to the partial view?
View partials are rendered with a clean variable scope... That is, they do not inherit view variables from the calling Zend_View instance.
There's a few options available to you here:
One, simply call:
echo $this->render('_header.phtml');
instead of using a partial. This file will have access to all your view variables, so you can just assign the form to your view in your controller, like anything else.
Another way is to explicitly pass your form as a variable to the partial, like so:
echo $this->partial('_header.phtml', array('form' => $this->form));
// $this->form inside your partial will be your form
Your other option is to either use placeholders, or layout response segments. Here's an example of placeholders:
In your _header.phtml, or layout... where ever you want the form to render:
<?php echo $this->placeholder('header'); ?>
And in your controller:
$this->view->placeholder('header')->append($form);
// I'm not sure, but you _may_ want to pass in $form->render() here.
// I can't remember if implode() (which is used in placeholders internally)
// will trigger the __toString() method of an object.
This has the added bonus of not polluting your view instance with one-off variables, like the form.
Note: I'll link to the manual pages as soon as the ZF site is back up; 1.9 launch is today, so the site's getting updated currently.
Here's some relevant manual pages:
Placeholder view helper
Partial view helper