Jump to content

Call Function From Template File


Blesta Addons

Recommended Posts

You shouldn't be calling php functions from template files. That is contrary to the MVC design and breaks the separation of business logic from presentation. Any data that should be available to a template should be set by the controller.

Link to comment
Share on other sites

so i shold create a plugin and work with it ?

i want to use some custom 'let say widgets' for theme . so is not possible at all to do that .

for example i want to make a ajax request for login . normally if i call the login page http://xxxxx.com/blesta/client/login/ with ajax i get a html response and i want ji*ust a true/false reponse , i should create a custom page in php to handel this , no ?

Link to comment
Share on other sites

i have in my new theme a portal system and it having a div box for login , i want to add a ajax request with login , if the login is corrected the box is updated with some client info .

 

also  in some area i want to push some info from database that are not available (or i have not looked well at the data available for templete .

 

the important thing is i have some php  function that show some data based in the client IP and client Email . so i want to pass the client_id and client_email to this file n and this file return me a response , this is possible with blestaRequest ajax , but what is not possible is how that file (located in templete folder load or communicated with other models in blesta ?
 

Link to comment
Share on other sites

I don't know where you got the date_added property from, but you should be calling Logs::getUserLog($user_id) if you want to know the last time a user was active.

 

 

yes Tyson ; i have this $clients->date_added and it show the date registration in template file :)

 

i can call this function from template Logs::getUserLog($user_id) ?

Link to comment
Share on other sites

i'm not talking about API or plugin .

 

i want to do this via template files , i want to make  request to database from template file .

 

for exemple , inside the template file client_main_myinfo.pdt , i want to do something like

Welcome Back  <strong><?php echo $this->Html->_($client->first_name, true) . " " . $this->Html->_($client->last_name, true);?></strong>

Member Sinse <small><?php $this->Html->_($client->date_added);?></small> , Last Visit <small> <?php HERE_WHAT_SHOULD_I_PUT_I_CODE_TO_MANUPULATE_DATABASE ?></small>

calling class from template file give a error "class not found" .

Link to comment
Share on other sites

You can't access the database from template files. There are several (hopefully obvious) reasons why you cannot.

 

To fetch information that is not available to the template, you will need to update the controller to make it available.

Link to comment
Share on other sites

You can't access the database from template files. There are several (hopefully obvious) reasons why you cannot.

 

To fetch information that is not available to the template, you will need to update the controller to make it available.

 

 

Thanks . is clear now , we should stop thinking about adding data to the templete files .

 

with this situation we can't do more with template system .

 

i think a feature request for something semilar should be done

Link to comment
Share on other sites

You can't access the database from template files. There are several (hopefully obvious) reasons why you cannot.

 

To fetch information that is not available to the template, you will need to update the controller to make it available.

what about helpers ? is possible to include them in template , then i use helpers to retch database ?

Link to comment
Share on other sites

Helpers can be available to templates, but none perform database actions, nor should they. Helpers are geared toward formatting, which is useful for display purposes, and is why they can be used in views.

 

With any MVC framework, there is a logical design separation between Models, Views, and Controllers. Business logic, like database queries, are in the models, and views do not access models--they only get data by way of their controller. So like I mentioned above, the correct way to go about getting data for a view is to update the controller to make it available to the view.

Link to comment
Share on other sites

Thanks tyson for clarification .

our probleme is that we don't want to hack or change the core files .

 

I can understand that. But Blesta does not make data available to views that it doesn't use, since that would be a waste of resources.

 

It sounds like your template customizations need to be supported through a plugin instead, but since you don't want to use a plugin, there is no proper alternative than to change core files to do what you want.

 

That said, I would not recommend anyone do this for reasons already mentioned, but you could technically load a model in the view and call it:

$obj = new stdClass();
Loader::loadModels($obj, array("Clients"));
$client = $obj->Clients->get(1);
Link to comment
Share on other sites

It sounds like your template customizations need to be supported through a plugin instead, but since you don't want to use a plugin,... 

That said, I would not recommend anyone do this for reasons already mentioned, but you could technically load a model in the view and call it:

using a plugin was our final raod to take .

if i use a plugin , i can make the controller send the data to any view file without going to the url plugin ?

for example when we access the page http://xxxx.xxx/blesta/client/main/ , we can load data from the plugin controller XXXX . ( as i know the controller send data to template file when is reached as url ( http://xxxx.xxx/blesta/admin/plugin/xxxxx/admin_main/ ) ?

our last solution was the fallowing

1 - create plugin

2 - set fonction in controller to send a specific data like

- http://xxxx.xxx/blesta/admin/plugin/request_ajax/totalservice/

- http://xxxx.xxx/blesta/admin/plugin/request_ajax/lastvisit/

- http://xxxx.xxx/blesta/admin/plugin/request_ajax/totalopenticket/

3 - call this function with blestarequest ajax .

Thanks

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...