Skip to content

Instantly share code, notes, and snippets.

@coleww
Last active August 29, 2015 14:02
Show Gist options
  • Save coleww/9561fb22a4cf7888a3a5 to your computer and use it in GitHub Desktop.
Save coleww/9561fb22a4cf7888a3a5 to your computer and use it in GitHub Desktop.
render html in sql
CREATE OR REPLACE FUNCTION render (table_name string, action string, ids string)
RETURNS string AS $rendered_template$
BEGIN
declare
temp_temp string;
rendered_template string;
SELECT
db_views.template AS template, db_views.variables AS variables
FROM
db_views
WHERE
db_views.table_name = table_name AND db_views.action = action
-- ok so this is ruby, but, you know, whatever you can run ruby inside postgres ok
--oh something like "ALL OF THEM" / built in pagination....
ids.split(",").each do |id|
SELECT
table_name.* AS model
FROM
table_name
WHERE
table_name.id = id
temp_temp = db_view.template
variables.split(",").each do |var|
temp_temp = temp_temp.replace("{{" + table_name + "." + var + "}}", model.var)
end
rendered_template = rendered_template + temp_temp
end
-- end of ruby
RETURN rendered_template;
END;
@coleww
Copy link
Author

coleww commented Jun 7, 2014

wait i can probably pass an array to sql

@coleww
Copy link
Author

coleww commented Jun 7, 2014

why did i think array was too much for sql, it is like the basis of computers

@coleww
Copy link
Author

coleww commented Jun 7, 2014

man it would be sick if sql directly consumed the params hash.

@coleww
Copy link
Author

coleww commented Jun 7, 2014

instead of helper functions or decorators or a service layer or whatever gibberish you write SQL.

@coleww
Copy link
Author

coleww commented Jun 7, 2014

OH GOOD GOD INLINE SCRIPTS! "/" returns a navbar with an inline script that ajaxes for the MODELS index and shoves it in the page and installs click handlers on the nav links.

@coleww
Copy link
Author

coleww commented Jun 7, 2014

WAIT EVEN WORSE!
write a plugin for backbone that compiles the frontend MVC you write into inline scripts that are stored as strings in yr template in the database

@coleww
Copy link
Author

coleww commented Jun 7, 2014

ok maybe a more realistic thing is to just put inline scripts in yr moustache views and let the jquery get carried along for the ride.

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