2

Big picture: I'm working on a module that will display view-only custom data on a specific contribution page.

Please understand, if you're not aware of this already, view-only custom data is not available to smarty templates automatically.

I have successfully put my custom-data in a variable and have displaying in a debug message on my specific contribution page.

Now I want to display it for the user. Some folks seem to be using the hook buildform for this. I've looked at he buildform reference, and can make a form field appear on my contribution page.

In my template, I've tried using {$variable_name}, but it seems that's not enough to display my variable.

I've also tried: $form->setVar( 'my_var', $variable_name); //in the module

and

{$my_var} // in the template

or

{$form.my_var} // in the template.

Still nothing. How do I pass a variable to a smarty template?

Can someone reply with some source code for the module and accompanying template help illustrate this for me?

Thanks much

2 Answers 2

2

I found a way to achieve the goal, although I'm sure there are other ways and possibly a more correct way.

In the module, assuming the value of $var is already set, in the module I have:

 $form->addElement('text','my_var', $var );

  // dynamically insert a template block in the page
   $templatePath = realpath(dirname(__FILE__)."/templates");
    CRM_Core_Region::instance('page-body')->add(array(
      'template' => "{$templatePath}/testfield.tpl"

Then the template file is this. My data appears under 'label' instead of 'value'. It does work though.

{* template block that contains the new field *}
<div id="testfield-tr">
  <div>The value of var is: {$form.my_var.label}</div>
</div>
{* reposition the above block after #someOtherBlock *}
<script type="text/javascript">
  cj('#testfield-tr').insertAfter('#intro_text')
</script>
0

add {debug} into your template, it should show you all the variables that are assigned, if you are lucky, the variable you want is already there.

If it isn't you need to write a bit of php code to assign it to the template. Depends on where/how you want it, but the more "future proof" is to create an extension (with civix) and use a hook (buildform or page or...?)

1
  • It's like you turned my question into a statement.
    – Peter_
    Commented Oct 23, 2015 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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