Stash icon indicating copy to clipboard operation
Stash copied to clipboard

FR: "output formatting" concept for get/get_list

Open timkelty opened this issue 10 years ago • 2 comments

Example:

{exp:stash:set_list name="companies"}
  {stash:company}Fusionary{/stash:company}
  {stash:city}Grand Rapids{/stash:city}
  {stash:company}Hallmark Design{/stash:company}
  {stash:city}Brighton{/stash:city}
{/exp:stash:set_list}

{exp:stash:get_list
  name="companies"
  output_format="json"
}

Would render:

[
  {
    "company": "Fusionary",
    "city": "Grand Rapids"
  },
  {
    "company": "Hallmark Design",
    "city": "Brighton"
  }
]

Example 2 (using same set as #1):

<div class="{exp:stash:get_list name="companies" output_format="class_attribute"}"></div>

Would render:

<div class="fusionary grand_rapids hallmark_design brighton"></div>

Example 3:

{exp:stash:set_list name="attrs"}
  {stash:class}class1 otherClass{/stash:class}
  {stash:id}my-id{/stash:id}
  {stash:data-foo}myDataValue{/stash:data-foo}
{/exp:stash:set_list}

<div {exp:stash:get_list name="attrs" output_format="html_attributes"}></div>

Would render:

<div class="class1 otherClass" id="my-id" data-foo="myDataValue"></div>

I for one, would get endless use out of this. Much of the stash wrangling I do is for output like this.

What would be really cool, is if you modularized each "output format" or provided a hook, so that others could make output format extensions themselves!

timkelty avatar Feb 28 '14 16:02 timkelty

I think this is a great idea, and I need this too.

croxton avatar Mar 21 '14 18:03 croxton

i first just thought about quickly adding a snippet to rebuild_list(). e.g. if (tagparam('output_format')=='json') return json_encode($list);

then, actually, all you need is this in a quick plugin

    public function json() 
    {
        $this->EE = get_instance();

        // Pass on the parameters to stash :-)
        // name="listname" type="snippet" scope="site" 
        // match="#{segment_3}#" against="entry_id"
        $params = $this->EE->TMPL->tagparams;

        $list = Stash::rebuild_list($params);
        return json_encode($list, JSON_PRETTY_PRINT);
    }

GDmac avatar Oct 07 '14 16:10 GDmac