Smarty use a variable instead of total form

50 views Asked by At

I have a form in smarty like this

<form class="psmd-form psmd_"> 
   <div class="psmd-fields"> 
  {if {$display_fields} == 1 || {$display_fields} == 2 } 
      <input type="text" placeholder="Enter first name" name="psmd_fname" class="psmd_fname"> 
  {/if} 
  {if {$display_fields} == 2} 
      <input type="text" placeholder="Enter last name" name="psmd_lname" class="psmd_lname"> 
  {/if} 
      <input type="text" placeholder="Enter your email" name="psmd_email" class="psmd_email"> 
   </div> 
   <div class="psmd-btn-cont"> 
      <button class="psmd-btn">{$submit_button_text}</button> 
   </div> 
   <div class="psmd-clear"></div> 
   <div class="psmd-validation"></div> 
{$popup_content} 
</form>

Now I want something like store the total form in a variable and when I will use that variable like {$form} then this total form should be visible. So can someone tell me how to do this only in smarty. Any help and suggestions will be really appreciable. Thanks

1

There are 1 answers

0
axiac On

You can use the {capture} built-in Smarty function to prepare a block of text:

{capture name=form1}
<form class="smarty_form">
    <div class="form-fields">
        <input type="text" placeholder="Enter first name" name="fname" class="fname">
        <input type="text" placeholder="Enter last name" name="lname" class="lname">
        <input type="text" placeholder="Enter your email" name="email" class="email">
    </div>
    <div class="btn-cont">
        <button class="btn">Submit</button>
    </div>
</form>
{/capture}

It is not rendered into the page but saved in an internal Smarty variable.

You can use it later in the template like this:

{$smarty.capture.form1}    {* Use the name specified in the {capture} opening tag *}