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.

eZ debug

Timing: Jan 15 2025 09:09:24
Script start
Timing: Jan 15 2025 09:09:24
Module start 'layout'
Timing: Jan 15 2025 09:09:24
Module start 'content'
Timing: Jan 15 2025 09:09:25
Module end 'content'
Timing: Jan 15 2025 09:09:25
Script end

Main resources:

Total runtime1.0886 sec
Peak memory usage6,144.0000 KB
Database Queries65

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 685.4063183.6719
Module start 'layout' 0.00610.0030 869.078152.5313
Module start 'content' 0.00911.0779 921.60941,883.8906
Module end 'content' 1.08710.0015 2,805.500072.5703
Script end 1.0885  2,878.0703 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00540.4986160.0003
Check MTime0.00130.1186160.0001
Mysql Total
Database connection0.00070.060910.0007
Mysqli_queries1.014593.1978650.0156
Looping result0.00050.0500630.0000
Template Total1.054596.920.5272
Template load0.00330.303720.0017
Template processing1.051296.563720.5256
Template load and register function0.00130.117710.0013
states
state_id_array0.00090.082910.0009
state_identifier_array0.00140.125220.0007
Override
Cache load0.00320.2917840.0000
Sytem overhead
Fetch class attribute can translate value0.00050.043530.0002
Fetch class attribute name0.00190.179160.0003
XML
Image XML parsing0.00080.074530.0003
class_abstraction
Instantiating content class attribute0.00000.001360.0000
General
dbfile0.00150.1352170.0001
String conversion0.00000.000840.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
5content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
17content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
23content/datatype/view/ezxmltags/emphasize.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/emphasize.tplEdit templateOverride template
15content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 63
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs