noobie smarty / custom php question...

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
eggy

noobie smarty / custom php question...

Post by eggy »

hey fellas-
quick noob question- i have a decent grasp of php, but need to get smart on how smarty works... i've checked out the article at:
http://www.s9y.org/78.html. but am having trouble getting things to work when i try to use custom php funtioncs.

just want to make sure i have the basics correct:

1- in the config.inc.php file for my theme, i can define php functions as i normally do.
2- in the config.inc.php, call $serendipity['smarty']->register_function....
3- install smarty parsing plugin
4- then i can use smarty calls- for example, in the example given in 78.html, i should be able to just call "{$my_custom_function}" from my entry, right?

if not, what am i missing? thanks!
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: noobie smarty / custom php question...

Post by garvinhicking »

Yes, you can run functions like these. But you call your functions with {myfunction} only, not {$myfunction}. And your PHP function declaration needs to be alerted a bit, since it uses a $params and $smarty variable.

But of course you can use a proxy function to access your real function like this:

Code: Select all

config.inc.php
<?php
function myFunction($do, $this, $here) {
}

function smarty_myFunction($params, &$smarty) {
  return myFucntion($params['do'], $params['this'], $params['here']);
}

$serendipity['smarty']->register_function('myFunction', 'smarty_myFunction');
?>

Smarty template:
{myFunction do=1 this=1 here=1}
Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
eggy

Post by eggy »

thanks garvin-

got the php stuff working exactly as i want. now, i need to get me some javascript working. =D

it appears that writing your own javascript functions will not work in entries(but basic javascript does) - is this correct? any suggested workarounds?
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

You can write javascript in your entries! But if you've enabled the NL2BR Markup plugin, that interfers with writing code, so that you would need to write your javascript all in one line!

Also remember that Smarty needs to escape { and } with {ldelim} and {rdelim}!

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
eggy

Post by eggy »

well... i can write javascript, but if i try to include a function in my javascript, it seems to bomb- even if i include just a empty function.
eggy

Post by eggy »

oops, hit submit by accident...

as an example, if within the javascript i just add:

function loadXMLDoc(url) {
}

I get the error:
Fatal error: Smarty error: [in smartymarkupplugin:1655783225 line 5]: syntax error: unrecognized tag: (Smarty_Compiler.class.php, line 436) in /home/content/e/d/s/edscholl/html/blog/bundled-libs/Smarty/libs/Smarty.class.php on line 1088
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Eggy, please read what I posted. :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
eggy

Post by eggy »

sorry garvin, had a little bit of a brain fart. i read your comments about escaping the delimters and it didn't even register as being applicable. :oops:
eggy

Post by eggy »

alright... more questions...

1: using the example provided, if I simply call
{myFunction do=1 this=2 here=3}
it seems to work in that my page prints out what's expected (i'm simply having the php execute: print "hi ! $do $this $here<br />";

but when i save the function it gives an error:

Fatal error: Smarty error: [in smartymarkupplugin:478814949 line 2]: [plugin] unknown tag - 'myFunction' (core.load_plugins.php, line 118) in /home/content/e/d/s/edscholl/html/blog/bundled-libs/Smarty/libs/Smarty.class.php on line 1088

2: is there any special syntax required for calling the smarty function from within javascript?

thanks!
eggt

Post by eggt »

To clarify #2 above, i want to conditionally call a smarty function from w/in javascript. If I have {myFunction do=1 this=2 here=3} within a conditional function within my page, it still always gets called.

thanks
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

First, you must register your custom functions within the config.inc.php template file, as mentioned in the Technical Docs on www.s9y.org about "Special Smarty Functions".

About number 2: Smarty get's executed before javascript. It's just like PHP: You cannot call PHP functions from within javascript. You must make additional HTTP page requests if you only want to conditionally include content; or use AJAX features. But in most cases, you should try to do the conditional stuff within PHP/Smarty, not within javascript.

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
eggy

Post by eggy »

garvinhicking wrote:First, you must register your custom functions within the config.inc.php template file, as mentioned in the Technical Docs on www.s9y.org about "Special Smarty Functions".

About number 2: Smarty get's executed before javascript. It's just like PHP: You cannot call PHP functions from within javascript. You must make additional HTTP page requests if you only want to conditionally include content; or use AJAX features. But in most cases, you should try to do the conditional stuff within PHP/Smarty, not within javascript.

Best regards,
Garvin
thanks for the prompt reply as always garvin- i am registering the custom functions in the config.inc.php; that's where all my php code is living... when i save an entry, i'm getting the errors above. but the functions work fine in the entries... should i just ignore the errors?

about #2- that's what i figured, i was just hoping there would be a easy cheat to get it to be called w/in the javascript. =D
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Eggy: Great, in fact you hit a bug there :)

The 'config.inc.php' file is not called when previewing/saving an entry.

So you can edit your include/genpage.inc.php file and remove those lines:

Code: Select all

// For advanced usage, we allow template authors to create a file 'config.inc.php' where they can
// setup custom smarty variables, modifiers etc. to use in their templates.
@include_once $serendipity['smarty']->config_dir . '/config.inc.php';
Instead place them at the end, just before "return true" of your serendipity_smarty_init() function found in the include/functions_smarty.inc.php file. Then it should all work.

Please tell me if that works out for you, so I'll commit it to our repository!

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
eggy

Post by eggy »

yup, that works. thanks!
Post Reply