|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
lokoman Smarty n00b
Joined: 19 Sep 2014 Posts: 2
|
Posted: Fri Sep 19, 2014 8:34 pm Post subject: how to add variable in smarty template with php |
|
|
greetings
I passed a very rare thing when trying to add a php script to check within a template with Smarty
Code: |
{include file=footer.html}
{php}
$str = '{/php}{$item_title|html_decode}{php}';
$str = ucwords($str);
foreach(str_word_count($str, 1) as $value){
if(strlen($value) > 3){
list($tag) = explode(" ",$value);
{/php} |
within this code in php is this: {$item_title|html_decode} returns a plaintext.
to put it the following way not get it to work
$str = '{/php}{$item_title|html_decode}{php}';
know someone I'm doing wrong? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Sep 20, 2014 12:21 pm Post subject: |
|
|
Using {php} tags is really deprecated because your code would always be dependent on the internals of the current Smarty version.
You better implement the fuctionallity in your script or impelment a plugin.
See http://www.smarty.net/docs/en/plugins.functions.tpl |
|
Back to top |
|
lokoman Smarty n00b
Joined: 19 Sep 2014 Posts: 2
|
Posted: Sat Sep 20, 2014 4:36 pm Post subject: |
|
|
{php} tag works perfectly me, what works is not to collect data Smarty.
because within a smarty function, you can not squeeze another smarty function no?
ex:
Code: |
<?php
function smarty_modifier_extract_image($string) {
preg_match_all('/<img[^>]+>/i',$string, $result);
if (!empty($result[0][0])) {
$image = $result[0][0];
} else {
$image = '{ANOTHER FUNCTION SMARTY}';
}
return $image;
}
?> |
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Sep 20, 2014 6:44 pm Post subject: |
|
|
From PHP you can access Smarty template vars through the object:
Smarty 2:
Code: | $var = $smarty->get_template_vars('varname'); |
Smarty 3:
Code: | $var = $smarty->getTemplateVars('varname'); |
You usually have the Smarty object available inside a plugin. If not make yourself a singleton, then you can access from wherever. You can also pass the results of other plugins as parameters to your plugin. Or, make an object/class that both plugins call. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|