Yii render variable not view file - php

question is: Does Yii have any method(s) to render variable with code in it?
Default:
$this->render('site/index'); where site/index is path to view file.
What I need to do is:
$content = '<div><?php echo "do something here"; ?></div>';
$this->render($content);
Output should be layout + parsed content
I tried to use $this->renderText($content); but this methods returns empty string.
I'm using Smarty extension to render view files, then $this->renderText($content); returns not parsed string: {assign ..}
Any help would be appreciated.

There are some rendering Functions in Yii you could use to get there, but not with a variable. It would be better to use renderFile(). If you want to pass data to this file use
$this->renderFile("renderfile.php",array("var1"=>"xyz","var2"=>"abc");
More render funtions described here

Related

Phpfox display html from php variable

I am trying to display some html code from a php variable created in a controller like
$this->template()->assign(array('html' => "<p>Ajith chandran</p>"));
But when I write {$html} in template file,this html code is rendering as normal text and displaying it with full code in browser.And I tried this in two other ways like
{php}echo $html;{/php} and <?php echo $html; ?>
but both are not displaying anything.
You should display this variable in Smarty using simply:
{html}
If for any reason I need to use {php} tag what I really don't recommend (it's a bad practice and in Smarty 3.1 is deprecated) you can display html value using:
{php} echo $template->getTemplateVars('html'); {/php}
Of course those both syntaxes should be used in TPL file and not in PHP file. If you want to use in PHP file any Smarty variable you could probably use in your case:
echo $this->template()->getTemplateVars('html');
What you should also consider is security of this solution. You should think about escaping your output or at least strip some tags. Now you can set<script>alert('I am a very bad script');</script> to html variable and user will see JavaScript alert. I assume you might not know what your html variable will store and Smarty by default doesn't escape variables what can make to serious problems with your site.
So you could either use for example:
{$html|escape:"html"}
{php} echo htmlspecialchars($template->getTemplateVars('html')); {/php}
to display those data safely or use global setting escape_html to do it for all Smarty variables:
In PHP
$this->template->escape_html = true;
In Smarty:
{$html}
{php} echo htmlspecialchars($template->getTemplateVars('html')); {/php}
As you see when using {php} tag even if setting escape_html to true you need to escape data in PHP otherwise you will display data as they were set.
Either set $escape_html to false in Smarty or use {$html nofilter}
See: http://www.smarty.net/docs/en/variable.escape.html.tpl

How to Display the Text contents of a text file in yii?

I am trying to display the contents of a text file inside a div in my view. I found that in php
<?php
$myfilename = "mytextfile.txt";
if(file_exists($myfilename)){
echo file_get_contents($myfilename);
}
?>
this code can be used . Is there any such methods in Yii to display the contents inside a text file?
Update
This file is stored under components folder. It cannot be included inside the views section.
readfile()
file_get_contents
You can try these two functions. :)
There are renderFile() method in CController class.
Put your text file somewhere at views/files and use that method like
$this->renderFile('/files/filename.txt');

replacement for php file_get_contents in smarty template?

I need to use PHP file_get_contents() in smarty tpl file. I can't use it in PHP and assign it to smarty template. Because the URL is generated dynamically through loop inside smarty template file. So I'm using smarty plugin function to achieve that task. But I want to know whether is there any way I can use it in template file directly instead of parsing it from plugin file.
I've attached the plugin code which I'm using to achieve this function. Please anyone let me know how to use it in smarty tpl file directly.
function smarty_function_getTitle($params)
{
if ($params['id']) {
$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$params['id']);
parse_str($content, $ytarr);
return $ytarr['title'];
}
}
I've used below code to call it in smarty template:
{getTitle id=$videoId}
Suggestions are welcome!
For those of you reading that didn't read the comments above, myself and OP are both aware that this is not how you use a template engine. He seems to have his reasons for wanting to do this directly in the template rather than a plugin or ahead of time in his code. So don't slag me for demonstrating how, please :)
Here is how you can do it in Smarty.
{"http://youtube.com/get_video_info?video_id=`$videoId`"|file_get_contents|parse_str:$result}
{$result.title}
I did the first part all in one call but if you want to be careful you can split it into multiple calls with checks. But I tested this locally and it worked fine.

str_replace (or another option) for replacing content located inside a php document

I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.
Example:
Index Page:
<?php include('templates/123/index.php'); ?>
templates/123/index.php page
<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>
I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.
I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)
My preferred method of doing stuff like this involves starting my code with:
ob_start(function($c) {
$replacements = array(
"username"=>"Kolink",
"rank"=>"Awesome"
);
return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
},$c);
});
Two steps I suggest
Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
use strtr() function to replace your placeholder i.e {username} with actual values
I think this will server your needs.

Get Template Name - Smarty

I'm dealing with website is a mess and need to find out the template file that smarty is rendering.
Is there a method on the smarty template object that I can call to get the current template file?
for example
echo $tplObj->getTemplate(); // echos "shop/templates/cart.tpl"
From the doc:
{$smarty.template}
Returns the name of the current template being processed. The following example shows the container.tpl and the included banner.tpl with {$smarty.template} within both.
<b>Main container is {$smarty.template}</b>
{include file='banner.tpl'}
will output
<b>Main page is container.tpl</b>
banner.tpl
Maybe the {debug} tag and its associated Debugging console could help, here ?

Categories