Help me - my head hurts! (Smarty)
Help me - my head hurts! (Smarty)
A quick THANK YOU to Garvin and all the users who have made s9y a HUGE success and a fantastic piece of work.
I used Serendipity for my blog (defunct) very happily for many years and this is my first post in these forums.
NOW TO THE POINT...
I have given myself a rather painful headache after reading through these forums and particularly the Smarty documentation.
I am having a terrible time trying to figure out how to ACCESS INDIVIDUAL FIELDS from (?) deeply nested arrays.
For example, I would like to access (and dynamically output on the front page or elsewhere) CustomField1 and CustomField2 from the latest entry in each category.
How can this be done with Smarty? (I could do it in PHP with an SQL query etc, but I digress...)
Do I need to write a custom PHP function/SQL query and then send an array to Smarty? Or can I take advantage of the arrays already available...
Thanks in advance for any help on this.[/code]
I used Serendipity for my blog (defunct) very happily for many years and this is my first post in these forums.
NOW TO THE POINT...
I have given myself a rather painful headache after reading through these forums and particularly the Smarty documentation.
I am having a terrible time trying to figure out how to ACCESS INDIVIDUAL FIELDS from (?) deeply nested arrays.
For example, I would like to access (and dynamically output on the front page or elsewhere) CustomField1 and CustomField2 from the latest entry in each category.
How can this be done with Smarty? (I could do it in PHP with an SQL query etc, but I digress...)
Do I need to write a custom PHP function/SQL query and then send an array to Smarty? Or can I take advantage of the arrays already available...
Thanks in advance for any help on this.[/code]
-
garvinhicking
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: Help me - my head hurts! (Smarty)
Hi!
For the first thing, you can only access arrays that are assigned to the current page.Usually, when you get a blogpage your array only either contains the list of 15 entriesfor the current page, or on a single page an entrywith only one entry.
That means, you could use that array to print out custom field names,butonly of those entries that are displayed on the currentpage. I believe, this is not what you want to do.
Thus,you have to use/create your own helper function to access the entry data.
That is done by editing/Creating a config.inc.php file inside your current template directory. It would look something like this:
This simply accesses the s9y API function to fetch one entry of a given category. Inside your index.tpl or other templats you could then use:
An other way would be to utilize the serendipity_fetchPrintEntries smarty function (see http://s9y.org/40.html) and then you could use a distinct templateto print out the result of fetching the recent article of a category.
There you wouuld need to access your given entry by looping through ALL entries like it is done in entries.tpl by default. Since this is an array with only one entry, you would havesomething like:
HTH,
Garvin
For the first thing, you can only access arrays that are assigned to the current page.Usually, when you get a blogpage your array only either contains the list of 15 entriesfor the current page, or on a single page an entrywith only one entry.
That means, you could use that array to print out custom field names,butonly of those entries that are displayed on the currentpage. I believe, this is not what you want to do.
Thus,you have to use/create your own helper function to access the entry data.
That is done by editing/Creating a config.inc.php file inside your current template directory. It would look something like this:
Code: Select all
function smarty_showCF($params, &$smarty) {
$oldcat = $serendipity['GET']['category'];
$serendipity['GET']['category'] = $params['catid'];
$entry = serendipipty_fetchEntries(null, true, 1);
$serendipity['GET']['category'] = $oldcat;
echo $entry[0]['properties']['ep_custom1'];
}
$serendipity['smarty']->register_function('smarty_showCF','smarty_showCF');
Code: Select all
Custom Field in Category #1:{smarty_showCF catid="1"}
There you wouuld need to access your given entry by looping through ALL entries like it is done in entries.tpl by default. Since this is an array with only one entry, you would havesomething like:
Code: Select all
{foreach from=$entries item="dategroup"}
{foreach from=$dategroup item="entry"}
CustomFIeld: {$entry.properties.ep_custom}
{/foreach}
{/foreach}
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/
# 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/
Garvin,
How did I know it would be YOU to first respond???
You are indeed amazing.
Thank you for being so thorough.
My initial instinct would be to use the custom API function as this is pretty much what I want to do for now.
But you have also opened my eyes to the power of serendipity_fetchPrintEntries and how to best utilize it for certain ends. For example, if I wanted to fetch x number of entries from each category the first method might work perfectly, but this probably won't account for STICKY POSTS. For that one might be better off working with a separate template/serendipity_fetchEntries....... but this is only me theorizing....... will have to test this all out now that I am awake again.....
But thank you also for already providing a good example of a loop I would need. This is a big part of my big headache yesterday. I hadn't realized that DATEGROUP was a part of the ENTRIES array. I had thought they were two distint arrays...
Anyways, I can't thank you enough at this point. I will have a go with these approaches and update here on any progress (or lack-thereof).
Cheers!
How did I know it would be YOU to first respond???
Thank you for being so thorough.
My initial instinct would be to use the custom API function as this is pretty much what I want to do for now.
But you have also opened my eyes to the power of serendipity_fetchPrintEntries and how to best utilize it for certain ends. For example, if I wanted to fetch x number of entries from each category the first method might work perfectly, but this probably won't account for STICKY POSTS. For that one might be better off working with a separate template/serendipity_fetchEntries....... but this is only me theorizing....... will have to test this all out now that I am awake again.....
But thank you also for already providing a good example of a loop I would need. This is a big part of my big headache yesterday. I hadn't realized that DATEGROUP was a part of the ENTRIES array. I had thought they were two distint arrays...
Anyways, I can't thank you enough at this point. I will have a go with these approaches and update here on any progress (or lack-thereof).
Cheers!
Whoops! Didn't think I'd get stumped this fast....but....
I'm getting this error on page load (application stop):
In MANAGE STYLES it tells me this (if this helps):
I will continue to search the forums for clues about registering a function to smarty as I supect it must be something with the code in config.inc.php above. Incidentally, I did not include <?php tags in config.inc.php as I assume it is unneeded...
In the meantime if you can provide any guidance I'll keep checking back...
I'm getting this error on page load (application stop):
Code: Select all
Fatal error: Smarty error: [in C:/www/templates/mytemplate/index.tpl line 151]: syntax error: unrecognized tag 'smarty_showCF' (Smarty_Compiler.class.php, line 590) in C:\www\bundled-libs\Smarty\libs\Smarty.class.php on line 1092In MANAGE STYLES it tells me this (if this helps):
Code: Select all
Theme/Style options
function smarty_showCF($params, &$smarty) { $oldcat = $serendipity['GET']['category']; $serendipity['GET']['category'] = $params['catid']; $entry = serendipity_fetchEntries(null, true, 1); $serendipity['GET']['category'] = $oldcat; echo $entry[0]['properties']['ep_CustomField1']; } $serendipity['smarty']->register_function('smarty_showCF','smarty_showCF');
This theme/style has no specific options. To see how your template can specify options, read the Technical Documentation on www.s9y.org about "Configuration of Theme options".In the meantime if you can provide any guidance I'll keep checking back...
Okay,
I don't believe the function is working as written. I tried with several category id's and I'm just getting blank output:
Then I thought I would try some other propertie such as:
and I'm still just getting blank output...
hmm...
I don't believe the function is working as written. I tried with several category id's and I'm just getting blank output:
Code: Select all
Custom Field in Category #0:
Custom Field in Category #1:
Custom Field in Category #2:
Custom Field in Category #3:Code: Select all
echo $entry[0]['properties']['ep_access'];hmm...
-
garvinhicking
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Hi!
Sadly I had not the time to check the function code itself. Can you try a print_r() arount the return of $entry in the function? It might be that I did not address the array properly and that it's nested on one more level?
Regards,
Garvin
Sadly I had not the time to check the function code itself. Can you try a print_r() arount the return of $entry in the function? It might be that I did not address the array properly and that it's nested on one more level?
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/
# 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/
Well I've been taking a closer look at the tutorial by Judebert...
And I am happy to discover that one can access nested arrays with the . syntax.
Therefore, {$entry.categories.0.category_name} works!
I should be able to create a loop with this to access different categories, include/exclude categories, etc.
The fields I want can be accessed at {$entry.properties.ep_CustomField1}.
Pretty sweet. Again, I THINK this is what I want... now I'll have to continue to do some testing, dummy data stuff, etc.....
And I am happy to discover that one can access nested arrays with the . syntax.
Therefore, {$entry.categories.0.category_name} works!
I should be able to create a loop with this to access different categories, include/exclude categories, etc.
The fields I want can be accessed at {$entry.properties.ep_CustomField1}.
Pretty sweet. Again, I THINK this is what I want... now I'll have to continue to do some testing, dummy data stuff, etc.....
Garvin,
I apologize. In playing around with the {$entry.categories.0.category_name} stuff...... I noticed that the output from your original custom function was now populated.
I changed it back to echo $entry[0]['properties']['ep_CustomField1']; and it now works.
I can't explain how/why it works now and was blank before.
I removed any new code from the template I'm experimenting with and it still works.
I can only attribute it (possibly?) to something not quite refreshing properly.... I'm really unsure...
But in any case, it seems that there might be several ways to skin this pig.
I think I will go back to your custom function for now and see if I can make it do what I want...
Thanks...
I apologize. In playing around with the {$entry.categories.0.category_name} stuff...... I noticed that the output from your original custom function was now populated.
I changed it back to echo $entry[0]['properties']['ep_CustomField1']; and it now works.
I can't explain how/why it works now and was blank before.
I removed any new code from the template I'm experimenting with and it still works.
I can only attribute it (possibly?) to something not quite refreshing properly.... I'm really unsure...
But in any case, it seems that there might be several ways to skin this pig.
I think I will go back to your custom function for now and see if I can make it do what I want...
Thanks...
Okay, after much messing around I've gone with using separate serendipity_fetchPrintEntries calls from my index.tpl, supported by individual templates for each call.
It would be nice if I could send another parameter to the individual templates (that way I would only need 1 template instead of 3) but I'm not going to complain at this point. It's working!
Each individual "category" template then looks something like this:
I'm quite pleased I've gotten to this point. Though I have a feeling I'm going to be scratching my head again when it comes to accessing the output and elements of a plugin.... 
BTW... CORRECTION on my last reply about your function working - actually it doesn't. It repeated the same data for each of those 4 Category ids when you would expect 4 different pieces of data. Oh well...
Code: Select all
{serendipity_fetchPrintEntries category="1" full=true fetchDrafts=false noSticky=true limit="0,1" template="front_one.tpl"}
{serendipity_fetchPrintEntries category="2" full=true fetchDrafts=false noSticky=true limit="0,1" template="front_two.tpl"}
{serendipity_fetchPrintEntries category="3" full=true fetchDrafts=false noSticky=true limit="0,1" template="front_three.tpl"}Each individual "category" template then looks something like this:
Code: Select all
{foreach from=$entries item="dategroup" name="cat1"}
//pull any data I want such as link, title, etc.
eg.: {$dategroup.entries.0.link}
{/foreach}BTW... CORRECTION on my last reply about your function working - actually it doesn't. It repeated the same data for each of those 4 Category ids when you would expect 4 different pieces of data. Oh well...
-
garvinhicking
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Hi!

Regards,
Garvin
I believe you *can* do that! Try to use the {assign ...} smarty command just before you use the fetchprintentries call. The value you assign there should be carried over to the front_one.tpl file where you should be able to check it, and when you change it after the call to another value, the next call should be able to give different output!It would be nice if I could send another parameter to the individual templates (that way I would only need 1 template instead of 3) but I'm not going to complain at this point. It's working!![]()
This might've been caused by the stickies getting fetched, I didn't know about you using thoseBTW... CORRECTION on my last reply about your function working - actually it doesn't. It repeated the same data for each of those 4 Category ids when you would expect 4 different pieces of data. Oh well...
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/
# 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/
That wasn't it. Not sure what was going wrong but I decided to go with the separate templates approach which is working out quite well so far.
But I knew it wouldn't be long before I ran into another stumbling block. I've gotten up to speed on Smarty in a remarkable period of time (only scratching the surface, of course), but I now realize that my SQL skills are also quite laxing (also not sure exactly how the database structure was designed).
So my latest question I have posed in a new thread here:
http://board.s9y.org/viewtopic.php?p=79127
But I knew it wouldn't be long before I ran into another stumbling block. I've gotten up to speed on Smarty in a remarkable period of time (only scratching the surface, of course), but I now realize that my SQL skills are also quite laxing (also not sure exactly how the database structure was designed).
So my latest question I have posed in a new thread here:
http://board.s9y.org/viewtopic.php?p=79127