aleksip / plugin-data-transform

Data Transform Plugin for Pattern Lab PHP
GNU General Public License v2.0
34 stars 10 forks source link

Markup Outputting Double Quotes in Attribute Values #18

Closed hkorik closed 6 years ago

hkorik commented 6 years ago

I've been attempting to use this plugin and I'm seeing the following result when setting any attribute in the global PL data file. I've tested the same with other attributes (id as an example) which gave me the same result.

I'm using versions: pattern-lab/core => 2.9.0 aleksip/plugin-data-transform => 1.2.0

<div class=""layout"">
 </div>

I've tried setting this attribute value with both the JSON and YAML formats. See below.

JSON Format:

{
  "layout_title": "Singe Column Layout",
  "items": [
    {
      "attributes": {
        "Attribute()": {
          "class": ["layout"]
        }
      }
    },
    {
      "attributes": {
        "Attribute()": {
          "class": ["layout"]
        }
      }
    },
    {
      "attributes": {
        "Attribute()": {
          "class": ["layout"]
        }
      }
    }
  ],
  "modifiers": {
    "1": "pl"
  }
}

YAML Format:

layout_title:
  "Singe Column Layout"
items:
  one:
    attributes:
      Attribute():
        class: ["layout"]
  two:
    attributes:
      Attribute():
        class: ["layout"]
  three:
    attributes:
      Attribute():
        class: ["layout"]
modifiers:
  1:
    pl

The Twig template markup is as follows:

{% if layout_title %}
  <div class="layout__title-wrap">
    <h2>{{ layout_title }}</h2>
  </div>
{% endif %}

<div class="component-layout-container {{ layout_class }} {% for modifier in modifiers %}layout--{{ modifier }}{% endfor %}">
  {% if content %}
    {{ content }}
  {% else %}
    {% for item in items %}
      <div{{ item.attributes }}>
        {{ item.content }}
      </div>
    {% endfor %}
  {% endif %}
</div>
aleksip commented 6 years ago

Hi, thanks for reporting this. I tried to replicate with the test files you provided (both JSON and YAML) but did not get the double quotes.

Which OS and Twig version are you using? I tested with macOS 10.14 and Twig 1.35.4.

hkorik commented 6 years ago

macOS - 10.13.6 Twig - 1.35.4

I can try upgrading my OS later today and letting you know if I still get the same results.

aleksip commented 6 years ago

Thanks, probably not due to the macOS version, was just wondering if it might be a Windows issue as I have only tested DTP on macOS and Linux.

Must admit that I am quite puzzled about the cause of this issue. Could it have something to do with the PHP version you are using? But I think that during the time of its existence I have used DTP on 5.6, 7.0, 7.1 and 7.2 without noticing this...

Is it possible for you to test in an other environment, like a virtual machine?

hkorik commented 6 years ago

This project is using a VM.

Ubuntu 16.04 PHP 7.2

I will test this later on in other projects as well to see if I get the same results.

hkorik commented 6 years ago

@aleksip I've tested this on other DrupalVM projects using PL with the same results.

However, after some additional debugging, I've noticed the only way that i've been able to get the markup printed correctly is if the attributes|raw filter was used with the twig file markup.

Is that necessary? It seems like it worked for you without that?

<div class="component-layout-container {{ layout_class }} {% for modifier in modifiers %}layout--{{ modifier }}{% endfor %}">
  {% if content %}
    {{ content }}
  {% else %}
    {% for item in items %}
      <div{{ item.attributes|raw }}>
        {{ item.content }}
      </div>
    {% endfor %}
  {% endif %}
</div>

Also to clarify the PL data is inside the components directory .yml file and not in the global _data.yml file.

screen shot 2018-10-31 at 12 09 54 pm

aleksip commented 6 years ago

Please check that you have twigAutoescape: false in your Pattern Lab config file (./config/config.yml). If not, try generating PL with that setting.

hkorik commented 6 years ago

That did it! Thanks!! Can you update the Readme documentation to reflect this?

aleksip commented 6 years ago

Excellent! Will definitely update the Readme, did not realize the setting would affect Attribute objects in this way.