Imad uddin
Imad uddin

Reputation: 147

How to get VOLT TEMPLATE in a variable inside CONTROLLER?

I'm trying to generate PDF using DOMPdf where i need HTML of a volt template. The volt template is generated dynamically. So how can i get the volt template html in my controller as variable.

MyController Action:

public function printEvaluationQuizResultAction() {

        // I WANT TO GET HTML FROM a TEMPLATE in $html variable 
        //$html = $this->request->getPost('html');

        // instantiate and use the dompdf class
        $dompdf = new Dompdf();

        $dompdf->loadHtml($html);

        // (Optional) Setup the paper size and orientation
        $dompdf->setPaper('A4', 'landscape');

        // Render the HTML as PDF
        $dompdf->render();

        // Output the generated PDF to Browser
        $dompdf->stream();
    }

Template:

<div id="print_result" style="display:none">

    <div class="row">
       <!-- Start of Symptoms Card/Table/Section -->
       <div class="col-md-6">
             <div class="card">
                 <div class="card-header">
                 <div class="text-center"><span class="section-heading-text">SYMPTOMS</span></div>
             </div>
             <?php if($quizDataArray['symptoms']): ?>
                 {% for key, symptom in quizDataArray['symptoms'] %}
                     <div class="col-md-12 card-box">
                         <div class="card-body">
                               <h5 class="card-title">{{ symptom['title'] }}</h5>
                               <?php if (isset($symptom['results'])): ?>
                                   <h6 class="card-subtitle mb-2 text-muted">
                                       {% for key, result in symptom['results'] %}
                                          <?php  $causeId = key($result); ?>
                                            {{ resultKeyArray[key] }} <?php if($causeId) { ?> {{ result[causeId] }} <?php } ?>
                                       {% endfor %}
                                   </h6>
                               <?php endif;?>
                         </div>
                     </div>
                 {% endfor %}
             <?php endif; ?>

             <div class="text-center"><span class="section-heading-text">DR.  RECOMMENDED PRODUCTS</span></div>
             <?php if($thingsToTry): ?>
                 <?php foreach ($thingsToTry as $remedy): ?>
                  <div class="col-md-12 card-box">
                      <div class="card-body">
                          <!-- Todo: To print image here -->
                        <div class="col-md-8">
                          <div class="info-box">
                            <h5><?php echo $remedy['title'] ?></h5>
                            <p>
                                <?php echo $remedy['description'] ?>
                            </p>
                          </div>
                        </div>
                      </div>
                      <div class="clear"></div>
                  </div>
                <?php endforeach; ?>
            <?php endif; ?>

            <div class="text-center"><span class="section-heading-text">Health Food Store Recommendations</span></div>
            <?php if($thingsToTry): ?>
                {% for key, remedy in thingsToTry %}
                    <div class="remedy-box">
                        <h5><strong>*{{ remedy['title'] }}</strong></h5>
                    </div>
                {% endfor %}
            <?php endif; ?>

       </div>
    </div><!-- End of Symptoms Card/Table/Section -->

</div>

I would love to add more details if any explanation is required just let me know.

Thanks

Upvotes: 0

Views: 693

Answers (1)

Juri
Juri

Reputation: 1367

First of all you should use volt in your templates. Right now you use one time php one time volt, even thogh you can use everywhere volt in this example.

To get html from volt i use in for example controller:

$view = $this->view;
$view->start();
$view->render('mail', 'exam');
$view->finish();
html = $view->getContent();

Upvotes: 0

Related Questions