ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[SOLVED] EE template parsing with parse_variables

January 13, 2014 12:12pm

Subscribe [1]
  • #1 / Jan 13, 2014 12:12pm

    MrBaseball34

    99 posts

    I have written an extension that builds a vars array, loads an email template and sends the html email.

    However, the template is not being filled with the values of the variables and my output is EMPTY.

    Here is the call to parse the variables:

    //******************************************************
        // Get the Template class;
        //******************************************************
        $this->EE->TMPL2 = new EE_Template();
        
        //******************************************************
        // Get the default email variables from our DB
        //******************************************************
        $email= $this->EE->email_model->get_email('customer-order-new');
        
        //******************************************************
        // Now load the email template from a file into the content
        //******************************************************
        $fl = PATH_THIRD.'_local/notifications/order-new.html';
        if(file_exists($fl)){
            // File helper
            $this->EE->load->helper('file');
            $email["content"] = read_file($fl);
        } else {
            return false;            
        }
        
        //******************************************************
        // Builds the $vars to be used to parse into the template
        // class variable $this->vars is populated in this function
        //******************************************************
        $this->build_vars($data); 
        
        //******************************************************
        // Parse the subject
        //******************************************************
        $subject = $this->EE->TMPL2->parse_variables($email["subject"], $this->vars);
        
        //******************************************************
        // Takes the $vars and parses the template with the values
        //******************************************************
        $output  = $this->EE->TMPL2->parse_variables($email["content"], $this->vars);
        
        //******************************************************
        // Pass output to parse method by reference 
        //******************************************************
        $this->EE->TMPL2->parse($output);

    The contents of the $email variable is in the attachment.
    This is the contents of the vars array:

    Array
        (
            [0] => Array
                (
                    [site_name] => TNA
                    [site_url] => <a href="http://localhost:8040/">http://localhost:8040/</a>
                    [imagelink] => <a href="http://localhost:8040//media/images/logo_email.png">http://localhost:8040//media/images/logo_email.png</a>
                    [currency_marker] => $
                    [fullname] => Mr. Obama
                    [customer_info] => <span>
    1600 Pennsylvania Avenue NW
    Washington, DC 20500
    US
    T:202-456-1414</span>
                    [payment_info] => Credit Card<table>    <tr>        <td><br>Credit Card Type:</td>        <td><br>VI</td>    </tr>    <tr>        <td><br>Credit Card Number:</td>        <td><br>XXXX-1111</td>    </tr>    <tr>        <td><br>Payer Email:</td>        <td>[email protected]<br></td>    </tr></table>
                    [disclaimers] => Thank you for your order from the TNA.
        <b>IMPORTANT INFORMATION REGARDING YOUR ORDER:</b> Verify the order by calling us at 202-456-1414.  
        <hr >
                    [grandtotal] => 419
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [price] => 419
                                    [sku] => 20140801OCA
                                    [qty] => 1
                                    [description] => <dl  padding:0;"><strong>Obama Care Pkg</strong>
    </dd>
        </dt>
        PRE-PAID Policy
    
                                )
        
                        )
        
                )
        )

    The $subject is parsed just fine, when it is echo’ed out it looks like this:

    Thank you for your order - Order # 40000002

    I’m at a loss as to why the $output is empty. Am I doing something wrong here?

  • #2 / Jan 13, 2014 12:12pm

    MrBaseball34

    99 posts

    The attachment didn’t work. Here is the contents of the $email variable:

    Array
        (
            [email_id] => 1
            [title] => order-new
            [version] => 1.00
            [content] => <html >
            <head>
            <meta http-equiv="Content-Type" c charset=utf-8">
            <style type="text/css">
                body,td {
                    color:#2f2f2f; 
                    font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; 
                    }
            </style>
            <title></title>
            </head>
            <body>
             <div  Verdana, Arial, Helvetica, sans-serif;">
              <table cellspacing="0" cellpadding="0" border="0" width="98%"  font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; margin-bottom:10px;">
               <tr>
                <td valign="top">
                                <!-- [ header starts here] -->
                 <div align="center">
                  <table>
                   <tr>
                    <td valign="top">
                                                <a href="http://{site_url}">_                                                {imagelink}_                                            </a>
                    </td>
                   </tr>
                  </table>
                 </div><br> 
                 <div align="center">
                  <table>
                   <tr>
                    <td valign="top">
                                                <strong>Dear {fullname}</strong>,
    
                                                {disclaimers}
                                                
    
                                                <table>
                      <thead>
                       <tr>
                        <td width="48%"><br><b>Billing Information:</b></td>       <!-- static -->
                        <td width="3%"> </td>                                  <!-- static -->
                        <td width="48%"><br><b>Payment Method:</b></td>            <!-- static -->
                       </tr>
                      </thead>
                      <tr>
                       <td valign="top"><br>                                          <!-- Customer Info -->
                                                            {customer_info}
                       </td>
                       <td> </td>                                                 <!-- static -->
                       <td valign="top">
                                                            {payment_info}
                       </td>
                      </tr>
                     </table><br> 
                     <table border="1" cellspacing="0" cellpadding="0" width="100%">
                      <thead>
                       <tr>
                        <td><br><b>Item</b></td>
                        <td><br><b>Sku</b></td>
                        <td><br><b>Qty</b></td>
                        <td><br><b>Subtotal</b></td>
                       </tr>
                      </thead>
                                                    {items}
                      <tr>
                       <td valign="top">                                               <!-- description -->
                                                            <br>
                                                            {description}
                                                        </td>                                   
                       <td valign="top"><br>{sku}</td>                                <!-- sku -->
                       <td valign="top"><br>{qty}</td>                                <!-- qty -->
                       <td valign="top"><br>${price}</td>                             <!-- price -->
                      </tr>
                      <tr>
                       <td colspan="3"><br>Subtotal</td>                             <!-- static -->
                       <td><br>${price}</td>                                         <!-- price -->
                      </tr>
                                                    {/items}
                      <tr>
                       <td colspan="3"><br><strong>Grand Total</strong></td>         <!-- static -->
                       <td><br><b>${grandtotal}</b></td>                             <!-- grandtotal -->
                      </tr>
                     </table>
                                                <br> 
                                                Thank you again,
    
                                                <strong>TNA</strong>
                                                
    
                    </td>
                   </tr>
                  </table>
                 </div>
                </td>
               </tr>
              </table>
                    <br> 
             </div>
            </body>
        </html>
            [site_id] => 1
            [subject] => Thank you for your order - Order #{order_id}
            [bcc_list] => 
            [from_name] => TNA
            [from_email] => .(JavaScript must be enabled to view this email address)
        )
  • #3 / Jan 15, 2014 4:50pm

    MrBaseball34

    99 posts

    I actually didn’t solve this exact issue but I went another route and was able to get things worked out. If you have an answer, I’ll stay subscribed so please post it.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases