Yii Retrieve and store in a variable a renderPartial file - yii

I have a php file under protected/views/directory_controller_name with formatting like that
<p>
<?php echo $model->title;?>
</p>
...
I display the file with classic method in the controller :
$this->render('filename',array('model'=>$model));
But know, I need to send an email with the same template/layout so I want to store the render of the file in an variable like
$msgHTML = $this->renderInternal('_items', array('model'=>$model));
But it doesn't work!
How can I get render view from a file and store in a variable?
Is it possible?
I don't want to use:
$msgHTML = '<p>'.$model->title.'</p>'
...
Because the file is very long and I don't want to duplicate code!!!

Don't use the renderInternal method, use renderPartial instead. Render internal is low level method and should not be used in such context. To catch the output just set the $return parameter to true:
<?php $output = $this->renderPartial('_subView', $dataArray, true); ?>

$msgHTML = $this->renderInternal('_items', array('model'=>$model), true);
http://www.yiiframework.com/doc/api/1.1/CBaseController#renderInternal-detail

I might be missing something, but can't you just use regular render() with the return argument set to true? Then you can just use a view 'name' instead of knowing the path. (And unless my trusty stack trace logger is broken, renderFile and renderInternal take the same fully qualified path argument. At least I can see renderPartial() passing the full path to my view file to renderFile.)

you can do this with these ways
1) if you want to get the output with header and footer (i.e ) full layout then do this
//add true in the last parameter if you want a return of the output
$htmloutput=$this->render('_pdfoutput',array('data'=>'nothing'),true);
2) similarly if you don't want to get the layout files just use renderpartial in the same way
$htmloutput=$this->renderpartial('_pdfoutput',array('data'=>'nothing'),true);
you will get the html of files in the variable . use this anywhere

Related

Passing variable from view to element in cakephp

I have a view file add_rates.ctp and in that there is one select box . when i change the optiion, it will make a ajax call and fetch some values from the controller and load an element in that view.
but my problem is iam not getting the parameter values in the element.
i am getting params in rates.ctp
$params=$this->params['pass'];
it will return an array of parameters. but when i tried this in element i am not getting the value.
also i tried to set a value in the add_rates.ctp and try to access in element, that is also not working
$this->set('params',array($params));
what to do..if anybody have idea about this please reply.. i get stucked ...
If $params is the array you are trying to pass, then try this:
<?php $this->element('your-element-name', array('params' => $params)); ?>

Render a JBuilder view in html view

I've created a json view with JBuilder. But I want to preload this into a data object, so Backbone has access to the data early on without fetching for it.
How can I render the list.json.jbuilder view into my list.html.erb view?
Normally without jbuilder, I'd do something like this:
<div data-list="<%= #contents.to_json %>"></div>
render, when called from within a view, returns a string rendering of the passed template or partial; you can embed that string into your view as you like. Note though that:
You have to append your template name with the type suffix/extension. If you don't, Rails may get confused about which template file you're calling; ie: it might choose list.html.erb instead of list.json.jbuilder. If you're making this call from list.html.erb, trying to render list.html.erb results in infinite recursion and a SystemStackError. Using the :format option for render doesn't appear to work.
You have to specify the qualified path to the template; it won't find the right template for "list.json" just because list.json.jbuilder resides in the same directory as list.html.erb.
You need to pass the output of the render call through raw; otherwise, it will get escaped when it gets embedded into the view.
So, for your example, you might write this, assuming your templates were in /app/views/foo:
<div data-list="<%= raw render(:template => "foo/list.json", :locals => { :contents => #contents }) %>"></div>

How to properly deal with different minor Yii layout variations?

I have this header that it's fixed across all pages except, the logo. The logo varies a little in color, regarding the page where the user are.
Should we set that on the corresponded controller and call it on the layout.php page ?
On controller
public $param = 'logoimagename';
On layout
echo $this->param
I've heard that Yii by design doesn't favor this, is there any better way ?
I would implement it using an helper function with a signature like this:
function getLogoName($controller, $action, ...){
$logo = Yii::app()->params['default_logo'];
$logo_rules = Yii::app()->params['logo_rules'];
// check if controller and action match any of the logo rules and get the logo name if found; use the default one otherwise
return $logo;
}
The default_logo and the logo_rules are parameters you have to set up in the config files.
In the view files, you could simply write:
echo .... getLogoName($this->id, $this->action->id, ...);

How to generate PDF using sfTCPDFPlugin in symfony 1.4?

I want to get a pdf of post details , so I installed sfTCPDFPlugin. I'm a new to it so can any one help me with this?
How to generate PDF using sfTCPDFPilugin in symfony 1.4?
The main problem you seems to have is to get the HTML output from your template to put it into the generated PDF. For that, you can use getPartial or getPresentationFor.
getPartial will render a partial
getPresentationFor will render a module/action
I recommend you to use the getPartial. Create a partial with the content you want to extract (and by the way, you will be able to use the same partial elsewhere if you need the same information to be viewable as html).
Assuming you create a partial called _my_partial.php, you can do that:
public function executePdf()
{
$config = sfTCPDFPluginConfigHandler::loadConfig();
/* put all your PDF configuration */
$html = $this->getPartial(
'my_partial',
// put in this array all variables you want to give to the partial
array('posts' => $posts)
);
$pdf->writeHTMLCell(
$w=0,
$h=0,
$x='',
$y='',
$html,
$border=0,
$ln=1,
$fill=0,
$reseth=true,
$align='',
$autopadding=true
);
$pdf->Output('example_001.pdf', 'I');
throw new sfStopException();
}
Refer link for step by step to generate PDF using sfTCPDFPlugin.
http://www.symfony-project.org/plugins/sfTCPDFPlugin/1_6_3?tab=plugin_readme

Drupal: How to get deeplink to comment?

I need to build and print a deeplink to any given comment. So that the user can directly access the specific comment with just clicking a link. I could not find a native drupal function to get this so i build it my own.
My solution
<?php
global $base_url;
$base = drupal_lookup_path('alias',"node/".$node->nid);
$path = $base_url.'/'.$base.'#comment-'.$comment->cid;
$link_options = array('html'=> $html);
$commentlink = l($date, $path, $link_options);
?>
To print the link you only have to call <?php print $commentlink;?>. But i'm pretty sure there is better and much more drupal like way to solve the problem.
The Better Way
Mikeker did it :) As he suggested here are the solution.
<?php
$commentlink = l(
$date,
"node/$comment->nid",
array("fragment" => "comment-$comment->cid"));
?>
Note the little difference bettween Mikeker and my version. array("fragment" => "comment-$comment->cid")); and array("query" => "comment-$comment->cid"));
The query param will add an ? to the url. So your path looks like
//…query
http://example.com/path/to/node?comment-2
In opposite to my solution (fragment):
//…fragment
http://example.com/path/to/node#comment-2
Note:
Do not include the leading '#' character to a fragment identifier. It will be added by drupal.
That's basically the way to do it. Comment permalinks are in the form of:
node/<nid>#comment-<cid>
Where <nid> and <cid> are the node and comment IDs, respectively. You can save yourself a step by not doing calling drupal_lookup_path() -- l() or url() do it for you. The shortened routine would look like:
$commentlink = l(
$date, // Text of the link
"node/$node->nid", // path to node, l() handles aliases
array('query' => "comment/$comment->cid"), // fragment to specific comment
);
In case anyone was wondering, the Drupal 7 way (at least, appears to be) this:
<a href='http://YOURSITE.com/comment/CID#comment-CID'>link text</a>
For example:
print "<a href='/comment/$comment->cid#comment-$comment->cid'>text here</a>";
And this would be placed in, perhaps, a comment.tpl.php file.