Answers.Body: <p>I found a way to achieve the goal, although I'm sure there are other ways and possibly a more correct way. </p>
  
  <p>In the module, assuming the value of $var is already set, in the module I have:</p>
  
  <pre><code> $form-&gt;addElement('text','my_var', $var );
  
    // dynamically insert a template block in the page
     $templatePath = realpath(dirname(__FILE__)."/templates");
      CRM_Core_Region::instance('page-body')-&gt;add(array(
        'template' =&gt; "{$templatePath}/testfield.tpl"
  </code></pre>
  
  <p>Then the template file is this.  My data appears under 'label' instead of 'value'.  It does work though.  </p>
  
  <pre><code>{* template block that contains the new field *}
  &lt;div id="testfield-tr"&gt;
    &lt;div&gt;The value of var is: {$form.my_var.label}&lt;/div&gt;
  &lt;/div&gt;
  {* reposition the above block after #someOtherBlock *}
  &lt;script type="text/javascript"&gt;
    cj('#testfield-tr').insertAfter('#intro_text')
  &lt;/script&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-24T02:23:48.627Z
Answers.LastActivityDate: 2015-10-24T02:23:48.627Z
Answers.UserDisplayName: Peter_
Answers.Body: <p>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.</p>
  
  <p>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...?)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-23T15:05:18.510Z
Answers.Text: It's like you turned my question into a statement.
Answers.UserDisplayName: Peter_
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-23T11:39:09.170Z
Answers.LastActivityDate: 2015-10-23T11:39:09.170Z
Answers.UserDisplayName: Xavier
Body: <p>Big picture:  I'm working on a module that will display view-only custom data on a specific contribution page. </p>
  
  <p>Please understand, if you're not aware of this already, view-only custom data is not available to smarty templates automatically.  </p>
  
  <p>I have successfully put my custom-data in a variable and have displaying in a debug message on my specific contribution page.</p>
  
  <p>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.  </p>
  
  <p>In my template, I've tried using {$variable_name}, but it seems that's not enough to display my variable. </p>
  
  <p>I've also tried:
  $form->setVar( 'my_var', $variable_name); //in the module</p>
  
  <p>and</p>
  
  <p>{$my_var} // in the template  </p>
  
  <p>or</p>
  
  <p>{$form.my_var} //  in the template.  </p>
  
  <p>Still nothing.  How do I pass a variable to a smarty template?</p>
  
  <p>Can someone reply with some source code for the module and accompanying template help illustrate this for me?  </p>
  
  <p>Thanks much</p>
  
Tags: <custom-fields><smarty>
Title: How do I pass a variable to a smarty template?
ThreadText: # How do I pass a variable to a smarty template?
  
  (Asked by: username_0 on 2015-10-22)
  
  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
  
  **Tags:** custom-fields, smarty
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-10-24. Score: 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 *}
  
    The value of var is: {$form.my_var.label}
  
  {* reposition the above block after #someOtherBlock *}
  
  
  ---
  
  ### Answer by username_1 on 2015-10-23. Score: 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...?)
  
  > **Comments:**
  > **username_0**: It's like you turned my question into a statement.  
  
  ---
  
  