function returns value in page template but not in block template (page list)

Permalink
Hi All,

I am experimenting with C5 and i have build my own little, very simple helper with a function to display a page attribute.

Here's the code:

<?php  defined('C5_EXECUTE') or die("Access Denied.");
class MySimpelHelper {   
    public function myFunction() {        
      $myAtribute = Page::getCurrentPage()->getCollectionAttributeValue('my_attribute');
        return $myAttribute;
    }
}

The code in the template(s)
$sh = Loader::helper('mysimplehelper');
echo myFunction();


If i echo myFunction() in a page template it returns the attribute value, if i echo the function in a pagelist template it does'nt return anything.

What am i doing wrong?
(thanks)

 
mnakalay replied on at Permalink Reply
mnakalay
hi,
it really also depends on where your helper is? Is it in a package? ae the 2 templates in the same location?

If it's in a package maybe you should specify the package when loading the helper
Loader::helper("some_helper_name", "some_package_name")


Also, the way you named things is not correct
if your class name is MySimpleHelper than the file name should be my_simple.php and when you call it you should call
Loader::helper("my_simple")


You also have some typos but I don't know if it's in your code or just when you typed this message
smenster replied on at Permalink Reply
Hi mnakalay,

Thanks for responding!

It are indeed typos, i changed the names to make things more readable.
Below is the "real" code.

Helper (path) root/helpers/topaanbod.php

class TopAanbodHelper {   
    public function offerType() {        
      $offerType = Page::getCurrentPage()->getCollectionAttributeValue('offer_type');
        return $offerType;
    }


This is the code i am using in a page template (path) root/templates/mytemplate/single.php, please note that this does return a value!
.....
<?php
      $tah = Loader::helper('topaanbod');
      echo $tah->offerType();      
?>
......


and this is the code i am using for the page list (path) root/blocks/page_list/view.php (which does not return a value)

defined('C5_EXECUTE') or die("Access Denied.");
$pages = $cArray;
$th = Loader::helper('text');
$tah = Loader::helper('topaanbod');
....
....
<?php foreach ($pages as $page):
....
....
<div class="ccm-page-list-description">
        <?php echo $tah->offerType(); ?>
   <?php echo $description ?>
</div>
...
....
JohntheFish replied on at Permalink Reply
JohntheFish
Your naming is not quite right

If the helper class is
class TopAanbodHelper {


The file must be
top_aanbod.php



Loaded with (assuming the file is in a package /helpers directory)
$tah = Loader::helper('top_aanbod', 'your_package_name');


Loaded with (assuming the file is in root/helpers)
$tah = Loader::helper('top_aanbod');
mnakalay replied on at Permalink Reply
mnakalay
Well it seems John and I both agree the naming convention is a problem. It is weird however that you say it's working in the page template. Anyway, correct the naming and then see if it works and let us know. It should solve the problem
smenster replied on at Permalink Reply
The naming convention has nothing to do with it i think, as you said the function is returning a value in the page template. ( i did try renaming the helper file but nothing changed)

The problem is that the function is returning the attribute value of the page you are currently viewing and not the pages the pagelist is reffering to in the loop. I guess the problem is this piece of code:

$offerType = Page::getCurrentPage()->getCollectionAttributeValue('offer_type');


I could off-course call the value from the pagelist template itself but that would mean i would have to maintain 2 pieces of code that do the same thing....
JohntheFish replied on at Permalink Reply
JohntheFish
That it works in some contexts, but not others, is (I suspect) pure luck (especially if you are on windows with case-insensitive file names). You need to have a common way of loading & calling it that works in all contexts (ie, have the naming convention right). That the names you are using work in some contexts but not others is indication that you do not have the naming convention right.
smenster replied on at Permalink Reply
i have solved the problem with the naming convention. The problem now is that the function does return a value in the pagelist only it is the wrong value. The value of the function is the value of the page that is being viewed and it should be the value of the page that is listed in the pagelist block....
JohntheFish replied on at Permalink Reply
JohntheFish
Your original code calls getCurrentPage. Hence the current page.

You probably need to look at getByID or getByPath, as per http://www.concrete5.org/documentation/developers/pages/overview/... , passing the page ID or path from the list as a parameter.
smenster replied on at Permalink Reply
I agree that that is the problem, question is if this will work on both the page template and the pagelist template so i don't have to make/maintain 2 separate functions. I'll get back on this... Thanks for your help
smenster replied on at Permalink Reply
is there some-way of doing this?

if (page template) {
// do something
}
elseif (page-list template) {
// do something else
}