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

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

我试图在我的打印 View 页面中访问 print_overview 和 print_photo 值的值,但无法弄清楚如何访问这些值我可以看到这些值在 Debugbar 中传递,如下所示“request_query array:2 [ "print_overview"=> "1""print"=> "1"]"在我的 View 文件中有

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

但 print_overview 的值似乎很重要,因为该页面仅回显 Not Checked 我一成不变,我无法弄清楚任何想法都会被感激地接受。

最佳答案

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

其次,您可以通过组件类中的post() 函数访问请求数据;然后通过 page 属性将它传递给您的 View (组件部分)。所以,你的处理程序会喜欢这样的东西:

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 %} 标签,则这些标签无权访问部分文件的本地范围,即它们无权访问所提供的变量到 View 。在 {% macro %} 标签内完成的任何评估都需要基于传递给它的变量。

关于javascript - 尝试在 octobercms 中创建可打印的网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42192388/

相关文章:

javascript - Ajax请求cors和cookies步骤

javascript - 根据输入更改输入数字类型的 CSS

javascript - 有没有办法进一步简化这些 javascript 函数?

php - postgres 中一些带双引号的列和一些不带双引号的列

PHP 函数在 Ajax 调用时返回 null

ajax - "AJAX"-only "application/json"-only POST 是否需要 CSRF token ?

javascript - 在删除节点之前如何检查节点是否有childNode?

javascript - 将存储在变量中的对象添加到另一个对象而不指定键就是使用变量名作为键。这是预期的行为吗?

php - 浏览器:刷新时 Cookie 丢失

javascript - AJAX 表单 Post PreventDefault 不起作用