Call Custom Function from Block's view.php

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
class AutonavBlockController extends BlockController {

My function is:
public function truncateText($text) {      
   $char = strlen($text);
   if ($char > 25) {
      $short = substr($text,0,24);
      $return = $short. '…';
      return $return;
   } else { return $text; }

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?


View Replies: View Best Answer
jordanlev replied on at Permalink Best Answer Reply
Try just $controller->truncateText(...)

Also, note that C5 has a built-in text truncation function, in the text helper (called "shortenText" I believe).
mnakalay replied on at Permalink Reply
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
JohntheFish replied on at Permalink Reply
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.
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){
  // complicated whatever ...
    300 x echo $variable;
  // then to finish...
  $html =  ob_get_contents();
  return $html;

Back to the original problem, if $variable is not appearing, my guess is that somewhere it has been misnamed or set to ''.
mnakalay replied on at Permalink Reply
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.