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:
The code in the template(s)
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)
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:
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)
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
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!
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)
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> ... ....
Viewing 15 lines of 17 lines. View entire code block.
Your naming is not quite right
If the helper class is
The file must be
Loaded with (assuming the file is in a package /helpers directory)
Loaded with (assuming the file is in root/helpers)
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');
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
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:
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....
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....
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.
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....
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.
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.
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
is there some-way of doing this?
if (page template) { // do something } elseif (page-list template) { // do something else }
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
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
You also have some typos but I don't know if it's in your code or just when you typed this message