Trying to use smarty in blog entry

Random stuff about serendipity. Discussion, Questions, Paraphernalia.
Post Reply
leowolf
Posts: 4
Joined: Thu Mar 30, 2006 5:52 am
Location: PA, USA

Trying to use smarty in blog entry

Post by leowolf »

I'm trying to use Smarty in a blog entry. (I want to create an "index page" to some of the entries and I want to pull out the entry titles for this index page.)
I've got the Smarty plugin installed and enabled. It is set up as the top Markup Plugin in the list. I'm running Serendipity 0.9.1 and PHP 5.0.4.

When I couldn't get my own function to work, I tried the sample function on this page:
http://www.s9y.org/78.html#A4 (pasted in below)
<?php
$serendipity['smarty']->register_function('my_custom_function', 'my_custom_function');

function my_custom_function($params, &$smarty) {
return 'I customized this: ' . $params['stuff'];
}
?>

The section below that one makes it sound like you can call this function from within an entry.
http://www.s9y.org/78.html#A5

so in one of my entries, I added the code
{my_custom_function stuff="Cool!"}
as this page
http://www.s9y.org/11.html#A31
advises you to do in a template file

I expected to see "I customized this: Cool!" on the page, but instead I saw nothing. Even before that, I had trouble getting the entry with the {} code on it to save. (Serendipity never told me it was saved.)

What am I doing wrong??

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

Re: Trying to use smarty in blog entry

Post by garvinhicking »

Where did you add your PHP code? In the config.inc.php of your template?

Did you enable the use of the smarty markup plugin in your body/extended body in the plugin config?

Which other event plugins are you using? Are you using the entryproperties cache option`?

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/
leowolf
Posts: 4
Joined: Thu Mar 30, 2006 5:52 am
Location: PA, USA

Config for smarty

Post by leowolf »

Yes to the first question; forgot to say that. My code is in a config.inc.php file in the templates/villages2 folder, which is the template I'm using.

The smarty plug-in is enabled and I've got Yes selected for the Body and Extended text (but not for the Comment or HTML nugget).

Other plug-ins are:
Markup: Smarty Parsing
Markup: Serendipity
Markup: Emoticate
Markup: NL2BR
Browser Compatibility
Spam Protector
Send entries via E-Mail
Extended properties for entries

And YES, I do have the entry properties cache option set to yes. That was the problem-- Thanks!!
Smarty code does work now.

P.S. Do you know why, when I save my entry with the {smarty} text included, I never get the message "Your entry has been saved"? It's just a little disconcerting, as I'm still thinking for a while... "is it done saving yet?"

FYI: site is http://journeys.tenthousandvillages.us
Last edited by leowolf on Thu Mar 30, 2006 3:16 pm, edited 3 times in total.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Config for smarty

Post by garvinhicking »

Hi!

Okay, great. Sadly I have not played that much with entryproperties caching in connection with the smarty markup plugin! But I do think that in serendipity 1.0 some changes have been made that could affect this properly working.

About the missing saving text, it might be a problem that the config.inc.php could not be executed for the entry save. Did you look into your PHP/Apache error log to look out for errors? Maybe you could right-click on the area where the "Entry saved" text usually shows up and view the HTML source of the iframe? Maybe inside that file something is wrong, or dieing in the middle of it?

Does previewing an entry with {smarty} text work on your install?

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/
leowolf
Posts: 4
Joined: Thu Mar 30, 2006 5:52 am
Location: PA, USA

Database call from Smarty

Post by leowolf »

But my own custom code still has problems.
<?php
$serendipity['smarty']->register_function('my_entry_title', 'my_entry_title');

function my_entry_title($params, &$smarty) {
$title = serendipity_db_query ("SELECT title FROM {$serendipity['dbPrefix']}entries WHERE id = $params['id']");
return $title;
}
?>
When I call that code like this
{my_entry_title id="39"}
and save the entry, go back to the Weblog, nothing comes up. Apparently I have an error somewhere that it can't parse the thing.

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

Re: Database call from Smarty

Post by garvinhicking »

You should try this code instead:

Code: Select all

<?php
$serendipity['smarty']->register_function('my_entry_title', 'my_entry_title');

function my_entry_title($params, &$smarty) {
  $title = serendipity_db_query	("SELECT title FROM {$serendipity['dbPrefix']}entries WHERE id = " . (int)$params['id'], 'assoc', true);
  return $title['title'];
}
?> 
HTH,
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/
leowolf
Posts: 4
Joined: Thu Mar 30, 2006 5:52 am
Location: PA, USA

Finally...

Post by leowolf »

Thanks for your help-- very much appreciated.
It wasn't quite right; can't tell you why, but you got me on the right road and here's where I finally ended up. Not as general as your solution, but it works for this site. (My table prefix is blog.)

Here's the code in config.inc.php:
-------------------------------------------------------------
<?php
$serendipity['smarty']->register_function('my_entry_title', 'my_entry_title');

function my_entry_title($params, &$smarty) {
$mytitle = serendipity_db_query ("SELECT title FROM blog_entries WHERE id = " . $params['id'] . " AND isdraft = 'false'");
if (is_array($mytitle)) {
return $mytitle[0][0];
}
return "No Title";
}
?>
----------------------------------------------------------
and here's how I called the function from my blog entry:
{my_entry_title id=42}
which gives the title for blog #42.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Finally...

Post by garvinhicking »

Okay, I now found the error: Your my_entry_title() function was missing a "global $serendipity" statement! So the prefix was always empty :))

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/
Post Reply