Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

how to add variable in smarty template with php

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
lokoman
Smarty n00b


Joined: 19 Sep 2014
Posts: 2

PostPosted: Fri Sep 19, 2014 8:34 pm    Post subject: how to add variable in smarty template with php Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Sep 20, 2014 12:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
lokoman
Smarty n00b


Joined: 19 Sep 2014
Posts: 2

PostPosted: Sat Sep 20, 2014 4:36 pm    Post subject: Reply with quote

{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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sat Sep 20, 2014 6:44 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP