Author Topic: How can i use hepers functions in a view ?  (Read 3258 times)

__fabrice

  • Newbie
  • *
  • Posts: 7
    • View Profile
How can i use hepers functions in a view ?
« on: February 24, 2011, 07:29:45 am »
Hi,
I'm a newbie ;) with TinyMVC and I want to use some functions in a view.

The helper :
Code: [Select]
<?php

class TinyMVC_Script_Myhelpers {

function esc($string) {
return htmlentities($string);
}

function anchor($url$text) {
return "<a href=\"$url\">$text</a>";

}
}


The controller :
Code: [Select]
$this->load->script('myhelpers');
The view :
Code: [Select]
<p><?php 
//echo TinyMVC_Script_Myhelpers::anchor($output,'this is a link');
echo anchor($output,'this is a link');
?>
</p>
I have an error using the anchor function :
Quote
Fatal error: Call to undefined function anchor() in /var/www/framework/tinyMVC/myapp/views/hello_view.php on line 55

Anybody help me please ? :)

Fabrice

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: How can i use hepers functions in a view ?
« Reply #1 on: February 24, 2011, 08:39:07 am »
Is your helper filename all lower-case? required for the autoloader.

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: How can i use hepers functions in a view ?
« Reply #2 on: February 24, 2011, 08:45:50 am »
Hmm I guess that is a manual load. It should work, what is the filename of your myhelpers file?

__fabrice

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How can i use hepers functions in a view ?
« Reply #3 on: February 24, 2011, 05:40:56 pm »
Hi,

Quote
Is your helper filename all lower-case? required for the autoloader.
Yes

Quote
Hmm I guess that is a manual load. It should work, what is the filename of your myhelpers file?
The name of my helper file is : "/myapp/plugins/tinymvc_script_myhelpers.php"

Thanks for help.

For information, i do a basic application with tinyMVC and :

a simple layout system
960GS
lessCSS
jquery
jqueryUI
headjs
modernizr
selectivizr
some hack IE from :   http://code.google.com/p/ie7-js/

Regards,
Fabrice

eeverts

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: How can i use hepers functions in a view ?
« Reply #4 on: August 19, 2011, 02:14:03 am »
Because this topic is not solved, and I had the same problem, and did find the (simple!) solution.

There are two ways to make it work. The first, not preferred:

The helper :
Code: [Select]
<?php
class TinyMVC_Script_Myhelpers {
function anchor($url$text) {
return "<a href=\"$url\">$text</a>";
}
}


The controller :
Code: [Select]
$this->load->script('myhelpers');
The view :
Code: [Select]
<p><?php 
echo TinyMVC_Script_Myhelpers::anchor($output,'this is a link');
?>
</p>

The better way, as it should be:

The helper :
Code: [Select]
<?php
function anchor($url$text) {
return "<a href=\"$url\">$text</a>";
}


The controller :
Code: [Select]
$this->load->script('myhelpers');
The view :
Code: [Select]
<p><?php 
echo anchor($output,'this is a link');
?>
</p>

Conclusion: use no class-definition in the helper script!

grx, Evert

mohrt

  • Administrator
  • Sr. Member
  • *****
  • Posts: 275
    • View Profile
Re: How can i use hepers functions in a view ?
« Reply #5 on: August 19, 2011, 10:19:37 am »
Actually that is the way to do it, just use PHP functions in the MyHelpers plugin, as the example is done in the docs. ie, do not make a "myhelpers" class. Only libraries require classes, not scripts.
« Last Edit: August 19, 2011, 10:23:40 am by mohrt »