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

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