HTML in public folder with hooks - ruby

I have an HTML file (index.html) in the public folder.
These HTML has some "hooks" in it.
Like:
<div>{client_ssnumber}</div>
<div>{client_company}</div>
I have to retrieve this file and complete the information in the hooks using data obtained in a controllerĀ“s method, then display in the screen.
What is the rails way to do it?

You can't do this (without terrible terrible hacking), it's hard coded to look for public files before checking the router.
Anyway, Your client is wrong. Tell them to use "<%= ... %>" instead of "{ ... }" and move it into a view (you should move it into the view, so it's where you want it to be, then just tell them what the name of the file is).
There is no sensible reason to make up your own templating language. It will be buggier and slower, and it will add a lot of time to get the product out the door. Plus you'll then have to maintain that code. This is a solved problem, use ERB. If they really like that syntax, use Mustache which is pretty similar, and is an existing templating language.
If you can't get them to do this, you have much bigger problems than how to render this page.

Put them in a view, on app/views/ and use a template engine like erb or haml. You can then assign variables in a controller and use them in your view.

Related

Execute Ruby method on whole page in Middleman

On my Middleman-built website, I need to execute specific Ruby code on the contents of all the pages (templates).
For example, if I had following helper in my config.rb:
def myexample(text)
text.gsub("dog","cat")
end
And in my test.html.haml:
= myexample("Some text about a dog.")
My previewed and generated /test.html would read:
Some text about a cat.
However, I am using several different ways to output text that needs to be modified, most notably through HAML's :markdown filter, so I would prefer not to wrap everything in the = myexample("Text") helper.
I would like to be able to run Ruby code that would take contents of all the pages (preferably) or generated HTML output (if the first option is not possible) as an argument passed to such helper.
Ideally, this code would be run in both the development and build environments, but if that's not possible, build is enough.
Is it possible to do so?
PS. In my specific case, I use a shorthand notation to reference other pages and then I use a regular expression and eval() in order to replace them with relative links from data files.
ActionController::Base has the render_to_string method which will give you the normal HTML output from rendering a partial or page, but in string format. This would allow you to grab the rendered HTML and modify it before finally rendering it for real as an inline template.
In your controller:
rendered_html = render_to_string 'your_template_or_partial'
# do stuff to rendered_html
render inline: rendered_html.html_safe, layout: 'layouts/application'
The html_safe method makes sure Rails knows it's safe to render this as HTML. You do NOT want to do this if user input is being rendered and you have not sanitized it!!!!
If you don't want it to use a layout when rendering, just remove the :layout argument.

Localized partials in Middleman

In Middleman, is it possible to localize partials? I haven't been able to find any documentation for this. But I'm working on long pieces with complex markup, and it may be necessary to adjust the templates themselves for localization. I'd like to be able to narrow these sections down to partials so that I don't have to fall back on maintaining entirely separate templates when this happens. Is there a place I can put partials such that the partial command will look up a partial as partial_name.language.html.erb?
It might also be possible to dynamically construct partials argument if it's possible to tell what language I'm in within the ERb. So I could do something like <%= partial "my_piece/partials/#{current_lang}/tricky_section" %>, but I don't know if something like current_lang exists.
On the issue tracker, I found documentation of how I can use the second option I can do <%= partial "partials/my_piece/#{lang}/tricky_section" %>, or directly embed logic that otherwise keys off of lang.

Passing Markdown Content to Ruby Function With Jekyll/Liquid

I am trying to write a jekyll plugin that will take a normal markdown file and provide some extra functionality on top of it. In particular, I need to do some (not actually) fancy things with tables. I know you can write straight HTML into a markdown file, but there is a requirement that the content folks don't want to / can't edit HTML.
As an extra wrench in the works, the mobile layout has a UX requirement that I essentially have to render the table as a group of divs as opposed to a table.
My initial thought was to pass the {{page.content}} variable to a ruby function extending Liquid::Tag. From there I was planning on parsing the markdown file and either:
1. If normal non-table markdown, use as normal
2. If table markdown, look for custom identifier in markdown, do what needs to be done (e.g. add class, etc)
If I do something like this:
def render(context)
content = Liquid::Template.parse(#markup).render context
end
It renders the context as a normal markdown file. However, I want to break up the context variable and work with the pieces before rendering. I've tried a few different approaches that I've gotten from the jekyll docs and Stack Overflow and gotten nowhere.
Does anyone have any experience with this? I am heading down the right path? For what it's worth, Ruby/Jekyll/Liquid is fairly new to me, so if you think I may have missed something fairly basic and obvious then please let me know.
A markdown table tool for editors !
markdownify your table in http://www.tablesgenerator.com/markdown_tables
paste the markdown result in http://prose.io/
done
I don't know other way to simplify editor's work on Jekyll, but I'll be very interested in earing from your project. Good luck.

nl2br in JSF2 without violating MVC paradigm?

i'm trying to figure out how to most elegantly integrate something like PHP's nl2br() function into a current project done with JSF2/Spring. I could easily create getters using something like return text.replaceAll("\n","<br/>"); in my model classes, however that does seem like putting view related code where it does not belong. I have the same feeling about storing actual html content in my database.
I guess the cleanest solution would be using tags/EL for this, however i couldn't find something that seemed to do just that. How would you guys implement something like this? Thank you in advance, any hints are highly appreciated!
Use either CSS, assuming that the text doesn't contain any HTML
<div style="white-space: pre">#{bean.text}</div>
Or create a custom EL function and then display it unescaped (Facelets implicitly escapes HTML)
<div><h:outputText value="#{my:nl2br(bean.text)}" escape="false" /></div>
You should only make absolutely sure that it's free of XSS.
Well, in the first place JSF is a Web UI framework. So, anything that you expect to output to the user will end as HTML (with the only exception of javascript, though). So, I don't find it a grave violation of MVC (if any at all). Maybe you could even push the envelope and directly use <br/> inside the text, instead of replacing \n
In a more general sense, if you have different lines/paragraphs in your text, the more flexible/standard solution would be break your text in the different elements and let your presentation logic handle it. So, instead of a properties with
presentationPage.introductionText=Ipse Lorum ...sum.\nVini vidi vinci.
You would end with
presentationPage.introductionText.par1=Ipse Lorum ...sum.
presentationPage.introductionText.par2=vini vidi vinci.

mvc 3 partials and usage

We meet again stackpeople!
I have been using the last 2 days trying to find the answer I need.I can't seem to find a straight forward answer on WHEN to use partialviews I know you can use em for like login component and all the other fancy stuff
.
But what about the navigation bar ? I tried making the navigationbar with partials and Ajax.htmlactionlink but then the problem comes, I know since its just a partial my URL won't get rewritten which means on f5 it will always update my home/index since no URL was given . Is this cus I can't make my navi like that or just because im plain stupid ?:)
Partials should be used to avoid code duplication. Create one if you find yourself writing the same view code over and over again.
A navigation bar sounds more like something that should be in the layout. You can use Sections in Razor and ContentPlaceHolder for the webforms view engine if you want to let pages customize the layout.
I don't think the partials have anything to do with it(it shouldn't, partials are just a way to split up source files so you can reuse them later).
I'm guessing the problem lies in the use of AjaxhtmlActionlinks, why would you want to do an Ajax call to redirect the user?
Try using the normal #Html.ActionLink()

Resources