Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: "output formatting" concept for get/get_list #97

Open
timkelty opened this issue Feb 28, 2014 · 2 comments
Open

FR: "output formatting" concept for get/get_list #97

timkelty opened this issue Feb 28, 2014 · 2 comments

Comments

@timkelty
Copy link
Contributor

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!

@croxton
Copy link
Owner

croxton commented Mar 21, 2014

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

@GDmac
Copy link
Contributor

GDmac commented Oct 7, 2014

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);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants