Last active
August 29, 2015 14:02
-
-
Save coleww/9561fb22a4cf7888a3a5 to your computer and use it in GitHub Desktop.
render html in sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
why did i think array was too much for sql, it is like the basis of computers
man it would be sick if sql directly consumed the params hash.
instead of helper functions or decorators or a service layer or whatever gibberish you write SQL.
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.
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
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
wait i can probably pass an array to sql