By green monkey on
Drupal 4.7.4
I'm trying to learn more about these cool snippets and just beginning to understand how to work with the theme templates.
I have the below snippet working just great - except for that it is printing » Array at the bottom of each node.
Not sure what to look for:
1) is it the snippet?
2) did I plug it in wrong?
3) do I need to add something to close this snippet?
<?php
$vocabularies = taxonomy_get_vocabularies();
foreach($vocabularies as $vocabulary) {
if ($vocabularies) {
$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vocabulary->vid);
if ($terms) {
$links = array();
print '<li>' . $vocabulary->name . ': ';
foreach ($terms as $term) {
$links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
}
print implode(', ', $links);
print '</li>';
}
}
}
?>any suggestions of what to look for would be helpful - thanks!
Comments
I think I've got it
The code you've shown is basically correct and doesn't have any bugs in it.
I assume this is pasted directly into your node.tpl.php ?
If so, there is already a $terms variable there contains the rendered HTML for that nodes taxonomy terms. You are defining a $terms variable in your snippet as an array. So later on in your template there is existing code that prints out $terms directly and $terms is now an array rather than HTML output.
You can either:
1) remove the existing bit from your template that prints out $terms (thats the easy way)
2) or redefine the way the $terms variable is rendered to reflect your snippet (this is the more advanced but more elegant way IMO). See http://drupal.org/node/16383 for more details. I could post examples later if you want to do it that way and need more help with it.
The reason I regard 2) as more elegant is that it keeps your templates clean and freer of code making them easier to edit by designers as they are still just for layout. Also the new rendering can be reused transparently in multiple node-nodetype.tpl.php templates without needing to make copies of the snippet.
PS: I also assume the
<ul>...</ul>enclosing tags are printed somewhere else?--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
good logic
Anton, thanks for the post. Your logic is good - it was just the wrong array - but heck how would you know what I'm doing. I still don''t know how to fix this yet - but I might as well post everything here - for help and as a reference in case someone else can use this.
I decied to make this task as easy as possible and use Bluemarine, as it has a very simple node.template.php file
I am replacing line 7
<span class="taxonomy"><?php print $terms?></span>with the above snippet all works well except as noted above
After reading your Post I have noticed that it is the $links array that is bugging out - and also seems to be the precise area where the links (Read more and Add comment) should be (I didn't even notice they were missing until just now) - but the word array displays instead.
deleting
<?php if ($links) { ?><div class="links">» <?php print $links?></div><?php }; ?>still leaves me without a "Read more" or "Add comment" link .... grrr
Yes! I would very much like your help and be very grateful ... but "as more elegant" that would be a big jump for me. I'm still a "cut and paste" programmer. By this I mean I scan all the fourms and Drupal handbook... find bits and pieces that might work for me and then I bind them together with duct tape and jump with joy when I actaully get something to display - besides an error message :-) ... but I am learning a little more each time :-)
Same solution
Your snippet is also redefining $links as well as $terms. Both those variable names have a purpose in node.tpl.php, so either they weren't the best name choices or they were intended to be used inside a function where they wouldn't conflict.
You can still either:
1) change $links to something else (eg $term_links) inside your snippet while leaving the original $links bit how it was outside your snippet.
2) change your snippet into a function defined in your template.php that can be reused in other node templates (I added the missing list tags):
Then in your node.tpl.php (or any other template) have something like:
or 3) the original 2nd option I mentioned in my first reply would still have solved this.
And whatever solution you choose, don't forget to add the original $links part of the template back :)
--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
cool - thanks
Hi Anton,
I think I almost have this - thank you the code changes and the time to try teach me some of this - it is greatly appericated.
everything seems to be ok, except 2 lines (which I have in template.php) there are parse errors
output .= '<li>' . $vocabulary->name . ': ';$links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));I've been messing wiht this for a couple of hours - I'm sure its something silly I don't know yet.
going to walk away and look it again in the morning - not giving up - just recharging:-)
path?
am I suppose to path this where you have "yourthemename" ? ... so it can find the function - how does it know its inside template.php
Yep
Yes, change 'yourthemename' to your theme name :)
It will find the function anyway, the theme engine takes care of all that stuff.
--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
it got me
lol, I coudn't walk away - had to keep trying
well I have simple way working - I just added a "_1" behind $terms and $vocabularies
I understadn that much now - which is more than yesterday :-)
but your other method seems like a better overall - long term soulation.
I'm just not getting it yet
Whoops
I left the $ off the beginning of the $output variable in one of those lines. I didn't change anything on the other line, so the parse error might be a carry over from the previous one.
--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
I spotted that
yep I saw the missing $ - I added it, but was still having issues - but then again - I was at the point of chasing my own tail
I'll try it again - in the morning with fresh mind :-)
thanks again - I'll be back :-)
Great snippet, thanks!
Great snippet, thanks!
Different snippet
I found a different snippet doing almost the same, except also the vocab name is printed:
greetings,
Martijn