2

I created a custom module and there is a simple hello_world() function. Is it possible to call this function in the page template file?

Module file looks like this.

function choco_custom_newui_total_students(){
return 500;
}

function choco_custom_newui_preprocess_page(&$variables) {
    $variables['total_students'] = total_students();
}

page--front.tpl.php

<h2><? echo $total_students?></h2>
8
  • You should call choco_custom_newui_total_students() but not total_students() – Jimmy Ko Nov 24 '16 at 12:35
  • That's what i was trying to do since beginning. Why would we write the "preprocess_page" function if it was possible .. any ways , its not working. – codestings Nov 24 '16 at 13:03
  • 1
    I can think of a list that you can check. [1] ensure choco_custom_newui is the module name. [2] see if choco_custom_newui module has been enabled. [3] run print 'stop here'; exit; and refresh the page to see if the preprocess function is really being implemented. – Jimmy Ko Nov 24 '16 at 14:08
  • 1
    Thank you for the checks. It worked. Sorry the module was not enabled :) – codestings Nov 24 '16 at 15:00
  • 1
    Haha.. good to know. – Jimmy Ko Nov 24 '16 at 15:02
3

You can do it, but it's not recommended, because in the MVC pattern you logic is in the Controller (in this case your module) and in the View (your .tpl file) you should only have code to show the values that comes from the Controller.

The idea is that all variables are prepared in template preprocess functions (in modules or in your template.php).

You can implement the hook_preprocess_page:

function YOUR_MODULE_preprocess_page(&$variables) {
    $variables['my_var'] = hello_world();
}

and in your page.tpl.php file you can now use to output the value set in your preprocess function:

echo $my_var;
4
  • thanks, i have updated my question with your code. Still i am unable to output any thing in the TPL file. – codestings Nov 24 '16 at 8:43
  • @codestings : bizarre .... despite your prior comment, you still accepted this answer ... Could it be that your module was not enabled? If so please add an extra comment here also, or just delete the prior comment. To Adrian: maybe refine your answer to explan WHY it is not recommended? Eg because you should not do so on Merci-giving-day? – Pierre.Vriens Nov 25 '16 at 8:31
  • @Pierre.Vriens I added a few lines if you want you can edit the answer too. – Adrian Cid Almaguer Nov 25 '16 at 16:29
  • Why only a single line (there's room enough for an entire chapter ...). Moreover, I don't think I'll edit it, since I just used the opportunity to hijack your answer to post a new question I asked myself ... And BTW: MVC in mainframe-speak is an assembler instruction to perform a "MoVe Character" ... (though I realize that's another MVC-thing). – Pierre.Vriens Nov 25 '16 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.