Im using smarty localization to get language labels
{config_load file="localization.conf" section=$project->lang}
When I need some label from localization.conf I will put into smarty template file (tpl)
ex. {#label#} (where label in localization.conf = 'some text')
But now I need to use label in php file , How to do that ?
If you want to use config variables before output (assume html output) you can load config in php
$smarty->configLoad('test.conf', 'Login');
var_dump($smarty->getConfigVars());
$smarty->display('test.tpl');
Those config data loaded via PHP will be also visible in Smarty templates. More info: http://www.smarty.net/docs/en/api.get.config.vars.tpl
Related
I want to change my website main page, using php code!, I found the tpl file which I need to change, problem is that I have no experience in tpl and I want to write it with php but it won't work. I am trying to use:
{include_php file="/path/to/somefile.php"}
To use php language but it does not work. How does it work with tpl in Smarty templates using php language.
My suggestion the following is a php file
include_once("/var/www/html/smarty/libs/Smarty.class.php");
$smartyobject=new Smarty();
$varr="stackoverflow"; //php variable it can also be a array";
$smartyobject->assign("website", $varr); //assigning a variable to "website", and this is now a smarty variable.
$smartyobject->display("tplfile"); //this is the actuall file that get displayed, in this tpl file you can use "website" variable {$website}
smarty has so many in functions loops.. so there is no need include any php file. do whatever you want to in the php file save the result in a variable and display it in a tpl file.
I want to include a markdown file in my Smarty template.
In PHP I have this:
$smarty->assign('text', 'PATH_TO_FILE/text.md');
in the template I use:
{include file=$text}
but this returns an unformated block of text.
I tried using David Scherer’s markdown plugin for Smarty:
{markdown text=$text}
but this just returns the file path stored in the $text variable.
How do I get Smarty to parse an included file as markdown?
You could instead read the file and assign the contents to the variable, then use the markdown plugin.
$smarty->assign('text', file_get_contents('PATH_TO_FILE/text.md'));
Manual page for file_get_contents()
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
I've run into a funny problem. I need to use Smarty templates within a Smarty template.
Here is why. I use the same templates for various wiki websites, and each website has its own configuration. The configuration contains parts for the main template (such as changed titles and headings, etc).
Here is a simplified example. I've a file topic-list.template.html that's shared across all websites:
<div id="topics">
<h1>{$h1}</h1>
...
</div>
As you can see, this template file contains an <h1> tag that can be customized for each website.
Then for each of the websites I've a configuration file that looks like this (simplified):
$config = [
"h1-titles" => [
"topics" => "Showing Topics in {\$category}"
]
];
As you can see the configuration file contains a Smarty template.
So when I render the topic-list.template.html file, I've to render the $config['h1-titles']['topics'] first through $smarty->fetch("string":$config['h1-titles']['topics']), and then assign it to h1 Smarty variable. My simplified code looks like this:
$h1_tag = $smarty->fetch("string":$config['h1-titles']['topics']);
$smarty->assign('h1', $h1_tag);
$smarty->display('topic-list.template.html');
I wonder if I could somehow insert the $config['h1-titles']['topics'] in the topic-list.template.html file automatically and then it have all rendered in one go?
Please have a look at the docs on String Template Resources. You will immediately notice that your $smarty->fetch('string:…') approach can also be done within a template: {include file="string:…"}
I believe that {eval} tag may help you:
{eval} is used to evaluate a variable as a template. This can be used for things like embedding template tags/variables into variables or tags/variables into config file variables.
http://www.smarty.net/docs/en/language.function.eval.tpl
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 ?