How do I use fetch?

Author Message

Steven Stieng

Friday 28 May 2010 1:26:48 am

I have the following code to fetch random news article on my frontpage:

{def $random_articles=fetch('content', 'list', hash('parent_node_id', 72, 'offset', rand(0,4), 'limit', 1))}

{foreach $random_articles as $article}
    <h3>{$article.name|wash}</h3>
    {$article.intro|wash}
{/foreach}

I am able to output the article name. But why can't I output the intro text? The attribute name is correct, and it has content.

 

André R.

Friday 28 May 2010 1:35:33 am

name is a node attribute, intro is an content object attribute available on the 'data_map' property.
short: intro has a much more complex structure, it's an object, not an string, and how you output an attribute's content depends on the attribute type (string/ int/ xml/ options...), so instead just use:

{attribute_view_gui attribute=$node.data_map.intro}

If you want to customize how it is outputted, turn on debug outout and site.ini[TemplateSettings]ShowUsedTemplates=enabled to see which template is used. In this case it will be <something>/content/datatype/view/<something>, and you'll be able to either copy and modify the template code instead of attribute_view_gui line, or override the template if you want to do it globally (placement override) or given a set of conditions (override.ini override).

Edit: in your specific code, use "$article" instead of "$node".

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Steven Stieng

Friday 28 May 2010 1:47:18 am

Humm...

But how can I use  {attribute_view_gui attribute=$node.data_map.intro} when the fetched data is stored in the variable $random_articles?

Putting that line inside my foreach loop does nothing. 

{foreach $random_articles as $article}
  <h3>{$article.name|wash}</h3>
  {attribute_view_gui attribute=$node.data_map.intro}
{/foreach}

Håkan Bergman

Friday 28 May 2010 2:01:03 am

You should do as André said and enable debug information and use |attribute(show, 1), its a great help to see your node identifiers and the data_map identifiers and data.

{def $random_articles=fetch('content', 'list', hash('parent_node_id', 72, 'offset', rand(0,4), 'limit', 1))}

Using 'list' will get the child nodes with object data and as André said you need to access the data_map.

Inside your foreach loop you can do this:

{foreach $random_articles as $article}
<h3>{$article.name|wash}</h3>
{$article|attribute(show, 1)}
{$article.data_map|attribute(show, 1)}
{/foreach}

Then you will first view the node attributes and data and then the object data_map attributes and data.

Make sure you have the correct identifier name (intro) as well from the class file. With this "debug" code you will be able to see the identifier name and the content data that is stored into it.

Steven Stieng

Friday 28 May 2010 2:19:44 am

Thanks for your reply.

Debuging is on. But that doesn't really help a noob like me to understand how to output whatever is inside my array.
Thanks for showing the code to output the arrays attributes. I was looking for that.

And now I understand :) 
In order to output the node data from the array, I need to use this line: 
{attribute_view_gui attribute=$article.data_map.intro}

 Now I get the intro content. (I was using $node instead of $article)

Thanks for your help.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.