尝试在octobercms中创建可打印的网页

时间:2017-02-12 19:46:07

标签: javascript php ajax octobercms

您好我正在尝试通过octobercms中的表单发送数据来创建可打印页面 我创建了一个插件组件,我称之为PrintPageForm

<?php namespace Acme\PrintPage\Components;

use Cms\Classes\ComponentBase;
use Input;
class PrintPageForm extends ComponentBase
{
    public function componentDetails()
    {
        // TODO: Implement componentDetails() method.
        return
        [
            'name' => 'Print Page Form',
            'description' => 'Detail page print form'
        ];
    }
    public function onHandleForm()
    {
        $var =
            [
                'overview' => Input::get('print_overview'),
                'photos' => Input::get('print_photos')
            ];

我在默认的htm文件中有这个

<form action="/print" data-request-data="printpageform::onHandleForm" data-request-validate data-request-flash accept-charset="utf-8" class="form ajax-form">
 <h3 class="sub-heading">Print Details</h3>
  <p>To build a printer friendly formatted page, please select from the options shown below:</p>
     <ul class="print-section">
         <li>
             <input type="checkbox" class="checkbox-input" value="1" name="print_overview" id="print_overview">
             <label class="checkbox-label period" for="print_overview">Overview: Summary and key features alongside a photo of the property.</label>
         </li>
         <li>
             <input type="checkbox" class="checkbox-input" value="1" name="print_photos" id="print_photos">
             <label class="checkbox-label period" for="print_photos">Photos: Photo gallery of the property.</label>
         </li>
                  </ul>
     <input type="hidden" name="print" value="1">
     <button class="btn button-large one-third palm-one-whole" type="submit" rel="print" >Print</button>
</form>

我正在尝试在打印视图页面中访问print_overview和print_photo值的值,但无法弄清楚如何访问这些值我可以看到这些值在Debugbar中传递如下&#34; request_query 数组:2 [&#34; print_overview&#34; =&GT; &#34; 1&#34; &#34;打印&#34; =&GT; &#34; 1&#34; ]&#34;在我的视图文件中我有

{%if "print_overview" == "1" %}
 {{ 'checked' }}
  {% else %}
  {{ 'Not Checked' }}
  {% endif %}

但是print_overview的价值看起来似乎很重要只有echos out没有检查我在一个车辙中,我无法弄清楚任何想法会被感激地接受。

2 个答案:

答案 0 :(得分:2)

几点指针。在Twig中渲染表单时,您应该使用the {{ form_open() }} or {{ form_ajax() }} tags

其次,您可以通过组件类中的post()函数访问请求数据;并通过page属性将其传递给您的视图(组件部分)。所以,你的处理程序需要像:

public function onHandleForm()
{
    // Pass the variables to the view renderer
    $this->page['print_overview'] = (bool) post('print_overview');
    $this->page['print'] = (bool) post('print');

    // Return a partial response http://octobercms.com/docs/ajax/update-partials#pushing-updates
    return ['#view-response-element' => $this->makePartial('@response')]; 
}

虽然您的response.htm部分文件看起来像这样:

{% if print_overview %}
    "checked"
{% else %}
    "not checked"
{% endif %}

注意,如果您使用{% macro %}标记,则这些标记无法访问部分文件的本地范围,即它们无权访问提供给视图的变量。在{% macro %}标记内完成的任何评估都需要基于传递给它的变量。

答案 1 :(得分:2)

我发现打印的最佳策略是使用JavaScript:

<!-- Link to print -->
<p><a href="javascript:printInvoice()">Print this invoice</a></p>

<!-- Invoice printer -->
<script type="text/template" id="invoiceTemplateContents">
    Printable contents go in here
</script>

<!-- Script -->
<script>
    function printInvoice() {
       var printWindow = window.open('','','left=0,top=0,width=950,height=500,toolbar=0,scrollbars=0,status=0')
       printWindow.document.write($('#invoiceTemplateContents').html())
       printWindow.document.close()
       printWindow.focus()
       printWindow.print()
       printWindow.close()
    }
</script>