Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

How to get/assign variable into plugin

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
blockmarc
Smarty n00b


Joined: 20 Nov 2011
Posts: 2

PostPosted: Sun Nov 20, 2011 1:30 pm    Post subject: How to get/assign variable into plugin Reply with quote

Hello,
I hope I don't anger anybody. I'm not a programmer and I'm very new to this stuff...

I have made a following plugin for smarty 2.6.26:

Code:
<?php

function smarty_function_get_extern($params, $smarty)
{
$utf8 = 1;
$url = "($EXTRAFELD12)";

//get url
//$url = ($params['extrafeld12']);

$inc = file_get_contents($url);

$away = array('<html><head></head>');

$new = array("");

$result = str_replace($away, $new, $inc);

if ($utf8 == 1) {
echo utf8_decode($result);
} else {
echo $result;
}

}
?>


I put it into my template using:

Code:
{get_extern extrafeld12={$EXTRAFELD12}}


This doesn't work...

Quote:
Warning: file_get_contents(()) [function.file-get-contents]: failed to open stream: No such file or directory in F:\xampp\htdocs\multitasking\includes\classes\Smarty_2.6.26\plugins\function.get_extern.php on line 29


I understand it is because the $url is empty!

My question is: how to intergrate a smarty {$EXTRAFELD12} into this plugin? Is it possible at all? The value of this smarty comes from a DB. Is it necessary to connect to the DB and read the value? If it is so, how do i do it?

I hope You can help me. Thanks a lot in advance!!!
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Sun Nov 20, 2011 3:10 pm    Post subject: Reply with quote

$smarty is the smarty object inside the plugin, so:


$foo = $smarty->getTemplateVars('foo');
Back to top
View user's profile Send private message Visit poster's website
blockmarc
Smarty n00b


Joined: 20 Nov 2011
Posts: 2

PostPosted: Sun Nov 20, 2011 5:44 pm    Post subject: Reply with quote

the problem is, the smarty {$EXTRAFELD12} comes from outside of teh plugin!!

I need to make the smarty {$EXTRAFELD12} filling the var $url.

Do You know what I mean?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Sun Nov 20, 2011 8:01 pm    Post subject: Reply with quote

I know, so with my example:


$EXTRAFELD12 = $smarty->getTemplateVars('EXTRAFELD12');
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Sun Nov 20, 2011 9:58 pm    Post subject: Reply with quote

You can use in your template
Code:
{get_extern extrafeld12=$EXTRAFELD12}


and in your plugin
Code:
function smarty_function_get_extern($params, $smarty)
{
$utf8 = 1;

//get url
$url = ($params['extrafeld12']);

$inc = file_get_contents($url);

$away = array('<html><head></head>');

$new = array("");

$result = str_replace($away, $new, $inc);

if ($utf8 == 1) {
    return utf8_decode($result);
} else {
    return $result;
}

}


But this requires that you did assign a value the Smarty variable
by
Code:
$smarty->assign('EXTRAFELD12',$somevalue);


Also note that your plugin should not echo the result but just return it to follow the Smarty conventions.


Quote:
$EXTRAFELD12 = $smarty->getTemplateVars('EXTRAFELD12');

allows you to access the Smarty variable EXTRAFELD12 from within the plugin without passing it as attribute by the template tag.

But the above is Smarty3 API. In Smarty2 which you are using it would be
Code:
$EXTRAFELD12 = $smarty->get_template_vars('EXTRAFELD12');


You can't directly access PHP variables defined in your script from within the plugin.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins 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