【问题标题】:How to get VOLT TEMPLATE in a variable inside CONTROLLER?如何在 CONTROLLER 内的变量中获取 VOLT TEMPLATE?
【发布时间】:2019-03-03 11:20:07
【问题描述】:

我正在尝试使用 DOMPdf 生成 PDF,其中我需要一个 volt 模板的 HTML。伏特模板是动态生成的。那么如何在我的控制器中获取 volt 模板 html 作为变量。

MyController 动作:

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();
    }

模板:

<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>

如果需要任何解释,我很乐意添加更多细节,请告诉我。

谢谢

【问题讨论】:

  • 到目前为止你有什么尝试?
  • @Alberto 我尝试添加一个函数 getPrintQuizTemplate 我返回 $this->view->content 但它没有返回 html!

标签: php pdf-generation phalcon dompdf volt


【解决方案1】:

首先,您应该在模板中使用 volt。现在你使用一次 php 一次 volt,即使在这个例子中你可以在任何地方使用 volt。

要从我使用的 volt 获取 html,例如控制器:

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

【讨论】:

  • 您能详细说明一下答案吗? $view->render('邮件', '考试'); ?谢谢
  • 第一个是控制器名(viewDir中的目录),第二个是要渲染的动作(文件名)
猜你喜欢
  • 2013-07-29
  • 2010-11-19
  • 2014-05-03
  • 2016-07-21
  • 2023-01-11
  • 2014-11-03
  • 2017-05-10
  • 2020-08-14
  • 2013-12-11
相关资源
最近更新 更多