Call Custom Function from Block's view.php
Permalink
Hello All-
I've been trying to figure this out and have been driving myself crazy.
I'm trying to modify the autonav block so that my custom template will truncate the after 25 characters. I've written a function to do so, and placed it within the autonav/controller.php underneath .
My function is:
In blocks/autonav/templates/sidebar/view.php, instead of the default $ni->getName(), I have $this->controller->truncateText($ni->getName);, but I receive this error:
.
Not sure why, because when I try $this->controller->getBlockTypeName(); it returns Auto-Nav (which is what the function should do).
Can anyone help me out?
Thanks!
I've been trying to figure this out and have been driving myself crazy.
I'm trying to modify the autonav block so that my custom template will truncate the
$ni->getName()
class AutonavBlockController extends BlockController {
My function is:
In blocks/autonav/templates/sidebar/view.php, instead of the default $ni->getName(), I have $this->controller->truncateText($ni->getName);, but I receive this error:
Fatal error: Call to undefined method AutonavBlockController::truncateText() in /home/niagara/public_html/blocks/autonav/templates/sidebar/view.php on line 73
Not sure why, because when I try $this->controller->getBlockTypeName(); it returns Auto-Nav (which is what the function should do).
Can anyone help me out?
Thanks!
Hi,
sorry to revive an oldish problem but here's my problem:
my function in the controller takes a variable
I call it from the view using
but all my function echoes is "my variable=" without the actual variable.
I even tried to give the variable a default vaue in the function like so:
and still, only the text part is echoed, never the variable.
Any idea?
Thank you
sorry to revive an oldish problem but here's my problem:
my function in the controller takes a variable
public function myFunc($variable) { echo "my variable=" . $variable; }
I call it from the view using
$controller->myFunc("Hello World");
but all my function echoes is "my variable=" without the actual variable.
I even tried to give the variable a default vaue in the function like so:
public function myFunc($variable = 'no value') { echo "my variable=" . $variable; }
and still, only the text part is echoed, never the variable.
Any idea?
Thank you
This is not a solution, but architecturally, I think its neater if the controller fn returns a string that is then echoed from the view. ie only the view actually outputs.
If its a complicated output (such as creating some script or style to insert), I usually use php's output buffering to build a string.
Back to the original problem, if $variable is not appearing, my guess is that somewhere it has been misnamed or set to ''.
public function myFunc($variable){ return "my variable=".$variable; }
echo $controller->myFunc($variable);
If its a complicated output (such as creating some script or style to insert), I usually use php's output buffering to build a string.
public function myFunc($variable){ ob_start(); // complicated whatever ... 300 x echo $variable; // then to finish... $html = ob_get_contents(); ob_end_clean(); return $html; }
Back to the original problem, if $variable is not appearing, my guess is that somewhere it has been misnamed or set to ''.
ok don't make fun but here's what happened:
The returned string was of the form "<style>....</style>"
So of course, it was returned but interpreted as part of the page code as a style block :\
Yeah I feel stupid...
Your thing about buffering will be kept in my little book though.
The returned string was of the form "<style>....</style>"
So of course, it was returned but interpreted as part of the page code as a style block :\
Yeah I feel stupid...
Your thing about buffering will be kept in my little book though.
Also, note that C5 has a built-in text truncation function, in the text helper (called "shortenText" I believe).