jeromegn / slang

Slim-inspired templating language for Crystal
MIT License
236 stars 30 forks source link

Can we see Slang adding JSON building (feature suggestion). #38

Closed thegreyfellow closed 6 years ago

thegreyfellow commented 6 years ago

Can we see Slang adding JSON building like Ruby Jbuilder, in the future (feature suggestion)?

jeromegn commented 6 years ago

Probably if somebody wants to build it, but I'm not sure how great it would be :)

thegreyfellow commented 6 years ago

I think that it's always better to have views separated from controller code. but when you have a JSON API, you're views are JSON objects, we can use serializers and return an instance of it, but having a JSON templating DSL like Jbuilder would make it easier and flexible to customize you JSON views.

jeromegn commented 6 years ago

I agree about that separation.

In Crystal, using structs or classes with json mappings is probably the way to go. It's terse and flexible. You can also use JSON.build and provide a block.

jeromegn commented 6 years ago

I won't implement this for now, thanks for submitting an issue though :)

Feel free to re-open if you think this is a bad idea. If you could provide some example use cases and how it would look in the real world, it may help further your cause :)

mkhairi commented 6 years ago

Sometime we need to customize json output to use with third party plugin or with some condition. here the example approach of using jbuilder in rails to build json output for jquery-datatables.

json.draw params[:draw]
json.recordsTotal @employees.total_count
json.recordsFiltered @employees.total_count
json.set! :data do
  json.array!(@employees) do |employee|
    json.extract! employee, :id, :staff_no, :ic_number
    json.DT_RowId "row_#{employee.id}"
    json.checkbox '<input type="checkbox" class="select-checkbox"/>'
    json.fullname employee.information
    json.position employee.position_name
    json.department employee.department_name
    json.joined_date employee.joined_date.try(:strftime, "%d/%m/%Y")
    if (current_user.admin? or current_user.super_admin?)
      json.url default_action(show: {url: employee_cards_path(employee), remote: false},
                              edit: {url: edit_employee_path(employee), remote: true},
                              delete: {url: employee_path(employee), remote: true} )
    else
      json.url default_action(show: {url: employee_cards_path(employee), remote: false})
    end

    if params[:spotlight]
      json.url employee_path(employee)
    end

  end
end
table#dttb-employees.table.table-hover.responsive.no-wrap data-title="Employee List" data-server-side="true" data-processing="true" data-order='[[1, "asc"]]' data-rowgroup='department'  data-source="#{source_url}"  data-new="#{new_url}" data-show="js"
  thead
    tr
      th data-default-content="" data-class-name="control" data-orderable="false"
      th data-data="checkbox" data-orderable="false" data-visible="false" data-class-name="action" class="select-control skip-export" width="5px" 
        input type="checkbox" class="select-all-checkbox" 
      th data-data="fullname" width="100%" data-class-name="selectable all ellipsis-xs" = t :name
      th data-data="joined_date" data-orderable="false" width="100px" = t :joined_date
      th data-data="department" data-class-name="none" data-orderable="false" = t :department
      th data-data="position" data-class-name="none" data-orderable="false" = t :position
      th data-data="url" data-orderable="false" data-class-name="action" data-priority="1" data-searchable="false" class="skip-export"  width="100px"