i'm trying to assign $this->render() result to a method (this method renders google map's infoWindow/baloon).
I'm using method like this to create this infoWindow:
$infoWindow->setContent(<here goes the template>);
but passing it like this:
$infoWindow->setContent($this->render('WmapFrontBundle:Place:infoWindow.html.twig'));
don't work at all. What's the proper way to assign template to a variable or pass it's content to a method ?
Use renderView(), it returns the rendered template only.
render() returns a Response object (with the rendered template, headers, etc).
Related
I'm using RainTPL and I would like to know if it is possible to change the value of any variable after the draw () method
Today my meta tags are all static and I would like to make them dynamic, but my header.html template is called in my class's constructor method, and the data I would like to use in the meta tags is returned after that, in my routes.
In my constructor (Page.php) I have the following code:
$this->tpl->draw("page/header");
In my routes I have the following code:
$app->get("/products", function(){
$products = Products::listAllProducts();
$page = new Page();
$page->setTpl("products/list", [
"productsList"=>$products
]);
});
I can pass the entire list of products to the list.html template, but I cannot pass some information to the header.html template because it has already been loaded in the constructor method.
So my meta tags are all static. Is there any way to change these values after the draw method?
Short answer no.
You could however include the header directly in your template. You will then be able to set variables in it at the same time as your main template. It could be an optional variable to not break compatibility with the rest of your application.
I didn't deal with render method yet !!
is it for blade template ?
I have to pass dynamic data in blade.php file dynamically.
Given that you've tagged the question with Blade, I'll assume you mean render inside Laravel's View class.
Illuminate\View\View::render() returns the string contents of the view. It is also used inside the class' __toString() method which allows you to echo a View object.
// example.blade.php
Hello, World!
// SomeController.php
$view = view('example');
echo $view->render(); // Hello, World!
echo $view; // Hello, World!
Laravel typically handles this for you, I.e. calls render or uses the object as a string when necessary.
Blade's #include('viewname') directive will load the view file and call the render method behind the scenes for example.
You may use it yourself when you want to get the compiled view to perform some subsequent action. Occasionally I have called render explicitly rather than to string if the view itself is causing an exception and in PHP explains
Fatal error: Method a::__toString() must not throw an exception in /index.php on line 12
Calling render() in the above case gives a more useful error message.
Render(), when applied to a view, will generate the corresponding raw html and store the result in a variable.
Typical reasons for which I use render are:
When converting pages to pdf (ex. using dompdf, pass this into loadhtml()), returning HTML content to ajax calls
You can get php blade file with passing dynamic value in a string form
Like this
Blade
<link rel="apple-touch-icon" sizes="60x60" href="{{$url}}/assets/images/favicon/apple-icon-60x60.png">
Controller
$html = view('User::html-file',['url'=>'https://stackoverflow.com'])->render();
O/P
<link rel="apple-touch-icon" sizes="60x60" href="https://stackoverflow.com/assets/images/favicon/apple-icon-60x60.png">\r\n
0pposed to ->render() when using DomPDF, you can also use ->toHtml():
$pdf->loadHtml(\view('folderX.bladeY', $data)->toHtml());
Is there a way to someone have an after filter for the View::make? What im trying todo is to run the content from View::make that is returned, through an HTML minifier.
I already have App::after that minifies the final html doc. But see, im putting the View::make response into a json object (for ajax requests) and i need the response to be minified.
After filters generally work after the response has been sent to the user.
So to minify the HTML that the user will see need to be processed before it is sent.
But before filters will not work here either. As they are called before the controller method is processed.
So you will have to your process to be used within your controller, a possible solution could be to use a helper function with your minify code, or as a function within your BaseController, which is accessible to all your controllers which called the helper function.
You can do the following within your controller;
$view = View::make('view.path', $data)->render()
This will render and process the view into the HTML the user will see.
You can then pass this to the function you have to minify the HTML and insert it into the JSON response.
I need your help.
I've just found out that the class \Illuminate\View\Environment has a protected array $sections and a method to get it getSections() but it returns an empty array.
Here's how I tried to get it:
$view = View::make('pages');
$env = $view->getEnvironment();
$env->make('pages');
print($env->getSections());
And the result is Array( )
Where did I go wrong? Any suggestion will be appreciated.
That's because when you call $env->make('pages'), the view isn't immediately processed. It's only processed when the render method on the $view object is called, which is done automatically when you return it as a response, via the __toString() method. The problem, though is that just after the page is rendered, still inside the render method, the $sections variable is cleared, using the flushSections method. So you actually never have access to it.
You could try to fool it if you call the incrementRender method before making the view, then make and render the view, get the sections and then finally decrementRender() and flushSections(), but this could bring unexpected results. Something like this:
// Fool it
$env->incrementRender();
$env->make('pages')->render();
$sections = $env->getSections();
// Clear it
$env->decrementRender();
$env->flushSections();
I have in my view a partial containing a partialLoop.
But when I run the page I have the following error message:
Call to a member function countComments() on a non-object in ...'_loop.phtml'
This is how I call my partial from within my view:
echo $this->partial('_post.phtml',$this->post);
where $this->post is a DB retrieved row
This is my partial's content:
MY simplified Partial!
echo $post->countComments();//the count number is correctly output..
echo $this->partialLoop('_loop.phtml',$this->object);
This is my partialLoop's content:
echo $this->object->countComments();//no output!
In the bootstrap I have set:
$view->partial()->setObjectKey('object');
$view->partialLoop()->setObjectKey('object');
Is this the right way to call partialLoops from within partials??
P.s. I var_dumped $this->object inside my partial and it is a PostRow OBJECT.I var dumped $this->object into _loop.phtml and I have 5 NULLS (standing for id,title,text,author,datetime fields of my post)
thanks
Luca
I think that the reason is that when you pass $this->post into partial view helper like this:
$this->partial('_post.phtml',$this->post);
partial view helper will execute its toArray() method. Hence, your $this->object is an array and you are passing an array to your partialLoop. So, in your partialLoop you are trying to execute countComments() on an array representing your row post object, rather than actual row object.
To avoid this, I would recommend passing variables to partial and partialLoop view helpers using array notation, e.g:
$this->partial('_post.phtml',array('post' => $this->post));
Hope this helps.
This error is caused by the default behaviour of the partial and partialLoop view helpers as Marcin said above.
Although it is confusing the manual does explain this here
Object implementing toArray() method. If an object is passed an has a
toArray() method, the results of toArray() will be assigned to the
view object as view variables.
The solution is to explicitly tell the partial to pass the object. As the manual explains:
// Tell partial to pass objects as 'model' variable
$view->partial()->setObjectKey('model');
// Tell partial to pass objects from partialLoop as 'model' variable
// in final partial view script:
$view->partialLoop()->setObjectKey('model');
This technique is particularly useful when passing
Zend_Db_Table_Rowsets to partialLoop(), as you then have full access
to your row objects within the view scripts, allowing you to call
methods on them (such as retrieving values from parent or dependent
rows).