1

I am being passed an array that unfortunately I cannot restructure:

"options": [
            {"name":"namea","text":"valuea"},
            {"name":"nameb","text":"valueb"},
            {"name":"namec","text":"valuec"},
            {"name":"named","text":"valued"}
           ]

I need to be able to find the object with the name equal to namea, nameb, namec, etc. and then produce the appropriate text. I tried the following and few other variations of that but could not get it to work:

{% for item in event.options %} 
    {% if item.name == "nameb" %}
        {{ item.text|capitalize }}
    {% endif %}
{% endfor %}

Thanks in advance for any help.

Edit: I basically only have access to a text box to input HTML and Twig. I do not have access to framework, custom extensions, etc.

Edit: JSON with exact formatting:

{
"source": "TEST.COM",
"trigger": "test",
"options": [{
            "name":"test_date",
            "value":"31-05-2017"
            },
            {
            "name":"test_number",
            "value":"9081003"
            },
            {
            "name":"test_test",
            "value":"9asd003"
            },
            {
            "name":"Name",
            "value":"Todd"
            },
            {
            "name":"test_other",
            "value":"kslkjsfd"
            },
            {
            "name":"test_help",
            "value":"908sdf3"
            }]
}
jwid
  • 13
  • 5
  • Maybe you should decode this Json. Check https://stackoverflow.com/questions/14500698/decoding-json-in-twig – Albeis Jun 04 '17 at 08:40
  • 1
    What does "could not get it to work" exactly mean? – zerkms Jun 04 '17 at 09:11
  • @albeis Unfortunately I am strictly limited to twig without a framework or way to extend functionality. – jwid Jun 04 '17 at 09:12
  • @zerkms Honestly hard to say. I'm using twig as a scripting language for preprocessing email content before sending. When there is an error, I simply do not receive a test. No errors or anything unfortunately. – jwid Jun 04 '17 at 09:17
  • `{{ dump(item) }}`? If you don't see anything - try output something yourself. – zerkms Jun 04 '17 at 09:17
  • Try to decode Json on your php code and send to twig template. – Albeis Jun 04 '17 at 09:22
  • @Albeis why do you think it's a JSON? Even if it is, why do you think it's not decoded already? – zerkms Jun 04 '17 at 09:24
  • I do not believe I have access to the debug extension that dump comes from. I basically only have access to a text box to input HTML and Twig. Really makes life difficult. – jwid Jun 04 '17 at 09:26
  • @jwid is it all about belief or facts? What happens if you do what I suggested? – zerkms Jun 04 '17 at 09:26
  • @Albeis I cannot make any changes to the JSON or the way it is being passed. I am not passing it. Actually it isn't even my own company that is passing it. – jwid Jun 04 '17 at 09:27
  • I only want to ensure that the JSON is converted to php std class by Json_decode, and then pass to twig.. Why can't you take this JSON and transform in your php code? – Albeis Jun 04 '17 at 09:33
  • @zerkms This is fact. Or at least fact from the view of our product I have access to. I do not have full access to a dev environment. – jwid Jun 04 '17 at 09:35
  • @Albeis I do not have access. – jwid Jun 04 '17 at 09:35
  • @jwid What happens if you do what I suggested? – zerkms Jun 04 '17 at 09:36
  • @zerkms Since I only have limited access, I drop in {{ dump(item) }} into my HTML and hit save. There is no response because the scripting and variables are processed on send. Then I trigger a test via api from another system and pass the JSON. Just like before, I do not receive the test or any error dialog. Sorry I'm not trying to be difficult. I just have very limited access. – jwid Jun 04 '17 at 09:45
  • Just to get rid of this guess work, be more specific. What is the contents of `event.options`, is it a json _string_, how is is formatted (does it have linebreaks or just a single line). Or is it an actual array of things (which I doubt as the where would work). But I do like to know the exact format for the json string if it is. Also dump() is only available if debug is set true. Which is not the default. – Jenne Jun 04 '17 at 09:46
  • @JennevanderMeer Sorry if I wasn't clear. I have included the exact formatting below of the JSON I am testing. Yes that was my understanding of dump as well. `"options": [{ "name":"test_date", "value":"31-05-2017" }, { "name":"test_number", "value":"9081003" }]` Edit: looks like it did not include the new lines between objects. – jwid Jun 04 '17 at 09:59
  • Just update the question*, however its important as you might be able to do some really weird split things. Otherwise have you considered using twig to write javascript to write html? – Jenne Jun 04 '17 at 10:01
  • Updated. It would most likely have a total of 14 total objects in the array in the end, but I am doing initial testing with only a subset. No, I have not, but if that is the only way, I may have to find an alternative route or scrap the project. – jwid Jun 04 '17 at 10:09

1 Answers1

1

This is going to be very hacky either way (I feel the javascript way as mentioned in the comments would still be less hacky).

However as its multilined we could write a basic parser like thing (this is by no means bullet proof and fails if the content has a ", or the structure changes or whatever changes to be honest):

{# create an array of lines #}
{% set event = event|split('\n') %}

{# an array to hold our options #}
{% set options = [] %}

{# using a bool to track if we are inside the options part #}
{% set inOptions = false %}

{# this holds the name of the last name value we have passed #}
{% set currentKey = '' %}

{# go through each line #}
{% for line in event %}

    {# if we are inside the options tag do our magic #}
    {% if inOptions %}
        {# check if the line starts with "name": and track the current key name #}
        {# or check if it starts with "value": and set the current key to that value #}
        {% if line matches '/"name":/' %}
            {# split line on the double quotes and get the last bit #}
            {% set currentKey = line|split('"') %}
            {# cant get the array index piped in one go idk.. #}
            {% set currentKey = currentKey[3] %}
        {% elseif line matches '/"value":/i' %}
            {% set currentValue = line|split('"') %}
            {% set options = options|merge({(currentKey): currentValue[3]}) %}
        {% elseif line matches '/\s*}]/' %}
            {% set inOptions = false %}
        {% endif %}
    {% endif %}

    {# check for the options sectioning start #}
    {% if line matches '/^"options/' %}
        {% set inOptions = true %}
    {% endif %}
{% endfor %}

{{ dump(options) }}

This returns in my dump (which you didn't have so you cant see):

array:6 [▼
  "test_date" => "31-05-2017"
  "test_number" => "9081003"
  "test_test" => "9asd003"
  "Name" => "Todd"
  "test_other" => "kslkjsfd"
  "test_help" => "908sdf3"
]

As an actual array where you can then call options.test_date but all this is super hacky, it still can be improved but twig is not made for it and the syntax gets so clunky its hard to maintain.

Jenne
  • 864
  • 5
  • 11
  • 1
    Thanks for the answer, Jenne. As you have said, it is clunky, but I guess that is just the nature of the issue. Just for my understanding, could you explain why my example didn't work? – jwid Jun 04 '17 at 17:47
  • Well that's te difference between [strings](http://php.net/manual/language.types.string.php) and [array](http://php.net/manual/language.types.array.php)/ [object/class](http://php.net/manual/language.types.object.php) types. The main problem being JSON (JavaScript Object Notation) is a string representation of a javascript object. So not an array/ object we can access. Your example would be the ideal solution (and quick scanned seems correct too) were it not you are not able to [convert (json_decode)](http://php.net/manual/function.json-decode.php) the JSON string to a PHP array/object. – Jenne Jun 04 '17 at 19:16
  • Ah that makes sense. I am still very much learning much of this, so this helped me understand a lot better. Really appreciate your help. – jwid Jun 04 '17 at 21:45