simple variable problem

Author Message

Rinze Cats

Wednesday 21 April 2004 3:31:47 am

I have a variable problem that is bugging me

I have a general .tpl that I use only for my homepage. It loops through alle related objects of the homepage (articles) and displays an introduction and link to each article. This is working fine.

However. The articles can have an object relation (defined with in the article class), which point to an image. If so, it should be displayed. I cannot properly instantiate this image object! please help, this should be easy. I keep getting a namespace error.

I want to fetch the object, assign is to a variable and use the variable to access the object and it's attributes

{section name=Related loop=$current_node.object.related_contentobject_array}
{* loop through all related objects *}
{* first get the parent to define placement on the page*}
{set-block name=homepageContent scope=global variable=parentID}
{$Related:item.parent_nodes.0}
{/set-block}
{switch match=$homepageContent:parentID}
{case match=49} {* categorie 1 *}
{set-block name=homepageContent scope=global variable=content}
{$Related:item.data_map.introductie.content}
{/set-block}
{set-block name=homepageContent scope=global variable=contentlink}
<a href={$Related:item.main_node.url_alias|ezurl} title=" ">more...</a>
{/set-block}
{* ************ *}
{* and now I need to fetch the image and put it in an object variable*}


{let name=homePageContent object=fetch( 'content', 'object', hash( 'object_id', $Related:item.data_map.thumbnail.content.id ))}
{/let}
{/case}
{/switch}

..can't figure it out

Alex Jones

Wednesday 21 April 2004 6:27:06 am

Hrrrm, I'm not 100% positive on this, but I think you are trying to call the variable incorrectly. You are setting the variable with:

{set-block name=homepageContent scope=global variable=parentID}
{$Related:item.parent_nodes.0}
{/set-block}

And trying to access it with: <i>$homepageContent:parentID</i>

Try calling it with <i>$parentID</i> only and see if that works. Also, try setting the scope to <i>root</i> instead of global. Here is an example of code that works on my installation, it is a bit different than what you are trying to do, but it may help:

{set-block scope=root variable=KeyCode}
  {let parentnode=fetch('content','node',hash(node_id,$DesignKeys:used.parent_node))}
    {$parentnode.data_map.keycode.data_text}
  {/let}
{/set-block}

{section show=ne($KeyCode,'0')}{$KeyCode} - {/section}

Hope this helps,

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Rinze Cats

Wednesday 21 April 2004 6:52:23 am

Hi alex, thanx for the reply

I will try your code suggestion and change the variable scope to root.
however, the part about the parentid per se is not causing the problem ( i think)
homePageContent.parentid used in the case if functional (although the code might not be ideal. Using the homePageContent namespace makes it simple to access the variables in the rest of the pages code.
the real problem lies in the part

{* ************ *}
{* and now I need to fetch the image and put it in an object variable*}

{let name=homePageContent object=fetch( 'content', 'object', hash( 'object_id', $Related:item.data_map.thumbnail.content.id ))}

the let defined here is not working. If I use the fetch in a set-block, like I do with the other two attributes, it actually returns something like "(ezContentobject) ... "
Can't remember exact, but it looks like it does fetch the object i want. The problem is that a set-block only returns strings, preventing me from actually using the attributes within the object. So I need a working alternative for

set-block name=homepageContent scope=global variable=myobject>
{fetch( 'content', 'object', hash( 'object_id', $Related:item.data_map.thumbnail.content.id ))}
{/set-block}

because this returns the string and I want to place the object in a variable, in my homepageContent namespace.

greetz
rinze

Alex Jones

Wednesday 21 April 2004 9:02:30 am

Hrrrm, I'm not sure about this one. Sorry. Hopefully someone else can help out.

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Dominik Pich

Thursday 22 April 2004 12:41:55 am

a let to init (all) the variable(s) and a set to fetch the real content.

Rinze Cats

Thursday 22 April 2004 1:03:50 am

could you possibly supply a small example to illustrate this? Preferably using a namespace as well. I have already spend hours on somethign as trivial as this ;-)

how do I for instance initialize an object using {let}. Do I have to assign a value of can I just say
<i>{let name=homepageContent myobject}</i>
....
<i>{let myobject = fetch (etc...}</i>

and then use the code by saying:

{$homepageContent:myobject.name}

?

thanx for the help!

Dominik Pich

Thursday 22 April 2004 1:18:48 am

{let name=data var=false}
{set var=fetch(...)}
{$data:var}
{/let}

Alex Jones

Thursday 22 April 2004 6:07:53 am

Oh yeah! I can't believe I missed that. Nice catch Dominik.

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

Rinze Cats

Thursday 22 April 2004 7:17:34 am

hi guys,

this solved my problem indeed. i knew it was supposed to be easy! I also implemented some code to check whether an image has been added. If so, fetch it! First I added the 'let' to my section

{section name=Related loop=$current_node.object.related_contentobject_array}
{let myobject=false}

then I added the following logic

{* get the object relation. *}
{* from the related object, extract the image and the alternative text and create an image tag (if exists*}
  {* check if homepage picture is available, if so proces it *}
  {section show=ne($Related:item.data_map.thumbnail.content.id,'')}
  {set myobject=fetch( 'content', 'object', hash( 'object_id', $Related:item.data_map.thumbnail.content.id ))}
  {set-block name=homepageContent scope=global variable=mythumbnail}
  {* check if the object has been set, if so: create the image tag*}
  {section show=ne($Related:myobject,'')}
	<img src="{$Related:myobject.main_node.data_map.image.content.small.url}" alt="{$Related:myobject.main_node.data_map.caption.content.output.output_text}">
  {section-else}
  {/section}
  {/set-block}
  {section-else}
{/section}

then I added
<i>{$homepageContent:mythumbnail}</i> where I want to show the thumbnail.

I guess the conditional part needs some improvement. suggestions are welcome!

thanx again for the help!
rinze

Alex Jones

Thursday 22 April 2004 7:38:49 am

Well, one quick note: you don't need to use those two instances of <i>{section-else}</i> as you are not executing any other code. So you can tighten it up to look like:

{section show=ne($Related:item.data_map.thumbnail.content.id,'')}
  {set myobject=fetch( 'content', 'object', hash( 'object_id', $Related:item.data_map.thumbnail.content.id ))}
  {set-block name=homepageContent scope=global variable=mythumbnail}
    {section show=ne($Related:myobject,'')}
      <img src="{$Related:myobject.main_node.data_map.image.content.small.url}" alt="{$Related:myobject.main_node.data_map.caption.content.output.output_text}">
    {/section}
  {/set-block}
{/section}

Alex

Alex
[ bald_technologist on the IRC channel (irc.freenode.net): #eZpublish ]

<i>When in doubt, clear the cache.</i>

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