Do we need to add html extension to partials? - ruby-on-rails

Let we have a partial _form.html.slim
It seems that there is no difference for rails add you the html extension or not so we can remove the html extension. _form.slim will be the same partial and works well.
Is there any difference?

Common format handling
The scheme for a template is : <action_name>.<format>.<preprocessors>.
Adding format extension is a mean to constrain template on that filetype, but it's totally optional, just like preprocessor.
If for example you were to implement a foos/index.erb template, it could be used to render either http://host/foos, http://host/foos.json, http://host/foos.html or whatever mime type you use. Using foos/index.html.erb, you specify this template should only be used for html.
Formats the template may be use for with no format constraint depends on :
the use or not of #respond_to in your controller, forcing allowed formats
the default recognized formats
There are quite a bunch of default recognized mime types, as of now : html, text, js, css, ics, csv, png, jpg, gif, bmp, tiff, mpeg, xml, rss, atom, yaml, json, pdf, zip.
If you were to specify both a file name with format forced and a one without, the forced format template would take precedence for that given format (so, if you have foos/index.html and foos/index, the first one will be used to render html pages, while the second one will be used for any other format).
Note that since both format and preprocessor are optional, you could have just a foos/index file if you want. That would serve a static file (not preprocessed) for all default formats.
With partials
The exact same rules apply to partials. If you specify a format, the partial will only be used for that format. You may specify multiple partials having each their format, and you can omit format to use the same partial for all formats.

yes you may do it but that partial will be rendered as js and css as well. So if possible try to avoid it

I'm not sure in this case, but html is the content type and slim would be the template engine. The filename.content_type.template_engine format separates it out to be processed. It may have worked for you to remove the html extension, but I believe it to be best practice to keep the extensions in place.

Related

Most efficient way of doing static HTML in multiple languages

So I would need to make static HTML pages in multiple languages and now I'm looking what way it would best to do. HTML of the pages stays same, as does images. Basically only text content changes from localisation to another. Page structure is something like this:
en/
../index.html (main/home page)
../catalogue.html
../video.html
../examples.html
de/
../index.html (main/home page)
../catalogue.html
../video.html
../examples.html
So layout (html, css and images) are same on all pages. Just text content changes. There are about 10 different languages. What tool would you use for the injecting text (from json file?) to each template and automatically building needed folders & files. Grunt + mustache?
This is pretty simple so I don't really want to use any CMS for this. For sass etc I will use Grunt already.
If you are going the grunt way here are some thoughts:
Take a look at grunt-dom-munger, it's designed to manipulate html's using standard selectors. You can replace text, elements, append new ones or remove existing, whatever you need.
If you also need to copy files from here to there after transforming them you may want to use grunt-contrib-copy.
Moreover, do not forget that the Gruntfile is plain javascript so you can write your own custom functions to do whatever manipulations or operations you may need.
Hope this helps you get started...

In Rails, can you get a file contents without rendering it?

In a model, I want to get a file's contents without rendering it. Say the file is a .erb file. I want to store its contents in a database, and then later on, I'll evaluate the string so that it replaces the variables in the .erb file with actual values.
Is there a method like render_to_string that doesn't actually evaluate the .erb part?
I'll ignore any reason you would want to do something like this. Ok so, you're looking to read files, this is done with plain Ruby:
File.read 'path/to/file'
That's how you read any file in Ruby. For a view in Rails you'd have to specify the path:
File.read Rails.root.join('app/views/some_view_dir/your_view_file.erb')
Just replace some_view_dir/your_view_file.erb with your actual view.
Reading files this way gets just the raw content, and you can do this with any file type.

Rails why is .css.scss and .html.haml necessary?

Surely Sprockets has enough info if a file is named x.scss and y.haml to assume that by default it should be rendered as css and html (the same applies with CoffeeScript)?
This is used to indicate clearly you want some kind of file.
This may be obvious most of the time so you can skip it.
Actually, this is really needed when you serve different types of views: .pdf, .html for instance. In this case, you have to use .pdf.haml, .html.haml
Same for coffee and scss, I never use .js.coffee, always use .coffee directly.

Odt file, shown with all styles

I'm using odt file as some kind of template and Libre Office as tool to create this template. It usually works fine except one thing.
Let assume our odt file has a paragraph of text.
There is my text.
XML file may or may not look (seems random) like this (messy, not very good thing for for parsing or as a template):
<text:p text:style-name="P7">There is</text:p><text:p text:style-name="P7"> my text<text:p text:style-name="P7">.</text:p></text:p>
Sometimes it's (again seems random) like this (expected result, makes sense after all):
<text:p text:style-name="P7">There is my text.</text:p>
Is there any way to get rid superfluous xml tags? Or at least can user see a raw document in LibreOffice/OpenOffice to manually remove redundancy?
The key is to provide easy tool for a user, to detect and fix artefacts like this.
Have you tried Ctrl-M? If all formatting is defined in styles and style formatting is not manually overridden, it should not disturb the formatting but should remove redundant tags.
A tedious user process would be to cut and paste-special as text and apply style again.
Finally, a macro would definitely do the trick.

Allowing includegraphics in LaTeX to include files with arbitrary filenames

I have some graphic files with some rather long filenames which includes several periods. includegraphics interprets the first of these as the beginning of the file extension, which makes it impossible for it to guess the proper graphics extension. A typical error message is
LaTeX Error: Unknown graphics extension: .9332.1dwc_kpl_h.log.png
One solution is to rename all files, but they are generated by another program, and I would rather use the naming scheme from there. Is there a way to tell graphics what the image format is, such that the extension will be ignored?
You can also hide the dots from LaTeX by putting extra curly braces in:
\includegraphics{{my.file.with.dots.in.it}.png}
(from https://tex.stackexchange.com/questions/10574/includegraphics-dots-in-filename)
Use the grffile package.

Resources