Forums / General / How do I use fetch?

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.

eZ debug

Timing: Jan 17 2025 23:58:48
Script start
Timing: Jan 17 2025 23:58:48
Module start 'content'
Timing: Jan 17 2025 23:58:48
Module end 'content'
Timing: Jan 17 2025 23:58:48
Script end

Main resources:

Total runtime0.1592 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0072 588.7031180.8516
Module start 'content' 0.00720.0060 769.554797.8984
Module end 'content' 0.01320.1459 867.4531526.7344
Script end 0.1591  1,394.1875 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00352.2291200.0002
Check MTime0.00130.8201200.0001
Mysql Total
Database connection0.00070.436610.0007
Mysqli_queries0.112970.93381410.0008
Looping result0.00130.83421390.0000
Template Total0.145691.410.1456
Template load0.00060.399610.0006
Template processing0.144991.027510.1449
Override
Cache load0.00040.271410.0004
Sytem overhead
Fetch class attribute can translate value0.00070.457410.0007
XML
Image XML parsing0.00020.149610.0002
General
dbfile0.00442.7576200.0002
String conversion0.00000.004530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs