I'm using a framework called bolt.cm. A simple framework based on silex. There is a twig function to get records of specific contenttype :
{% setcontent products = 'products' %}
To get record and order them by specific field :
{% setcontent products = 'products' orderby 'datepublish' %}
Above will fetch records ordered by datepublish field. Now, I wanted to pass the orderby field from GET parameter. I store the GET parameter in a global twig variable which passed from the controller, defined as sort_by
variable.
{{ sort_by }}
Above will print sort_by value into html. In page : /products?sort_by=datepublish
will set sort_by
value as datepublish
Now I'm going to combine this variable into setcontent function as described below :
{% setcontent products = 'products' orderby sort_by %}
Now I'm getting error :
An exception has been thrown during the compilation of a template ("Attribute "value" does not exist for Node "Twig_Node_Expression_Name".") in "products.twig".
My question is simply, how to make sort_by recognized as a value inside that function ? Please don't tell me to do :
{% if sort_by == 'datepublish' %}
{% setcontent products = 'products' orderby 'datepublish' %}
{% elseif sort_by == 'another_field' %}
{% setcontent products = 'products' orderby 'another_field' %}
{# and so on for every single field #}
{% endif %}
Which version of Bolt are you using? This should be possible since version 1.6. The release notes say:
You can now use variables in your {% setcontent %} tags, like this: {% setcontent mypages = 'pages' order my_var limit other_var %}.