Module: YMDP::ApplicationView
- Extended by:
- ApplicationView
- Includes:
- Compressor
- Included in:
- ApplicationView, Compiler::Template::View, View
- Defined in:
- lib/ymdp/view/application_view.rb
Overview
Contains all the functions which are available from inside a view file, whether that view is HTML, JavaScript or CSS.
Instance Method Summary collapse
-
#english_languages ⇒ Object
Returns an array of country codes of English-speaking countries supported by the application, based on the language-specific folders located in “app/assets/yrb”.
-
#include_firebug_lite ⇒ Object
Renders a link to include Firebug Lite for debugging JavaScript in Internet Explorer.
-
#javascript_include(filename) ⇒ Object
Includes a JavaScript file in a view.
-
#render(params) ⇒ Object
Renders a partial into the current view.
-
#supported_languages ⇒ Object
Returns an array of the country codes of all languages supported by the application, which is determined by the language-specific folders in “app/assets/yrb”.
Instance Method Details
#english_languages ⇒ Object
Returns an array of country codes of English-speaking countries supported by the application, based on the language-specific folders located in “app/assets/yrb”.
Examples
english_languages
# => ["en-US", "en-AU", "en-SG", "en-MY"]
43 44 45 46 47 |
# File 'lib/ymdp/view/application_view.rb', line 43 def english_languages supported_languages.select do |lang| lang =~ /^en/ end end |
#include_firebug_lite ⇒ Object
Renders a link to include Firebug Lite for debugging JavaScript in Internet Explorer.
80 81 82 |
# File 'lib/ymdp/view/application_view.rb', line 80 def include_firebug_lite javascript_include "http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js" end |
#javascript_include(filename) ⇒ Object
Includes a JavaScript file in a view. If the filename is a full path, the JavaScript file will be linked as an external asset. If not, the file will linked as a local asset located in the YMDP application’s assets directory.
Local JavaScript Assets
javascript_include("application.js")
will produce:
<script src='/om/assets/3ifh3b2kjf_1/assets/javascripts/application.js'
type='text/javascript charset='utf-8'></script>
External JavaScript Assets
javascript_include("http://www.myserver.com/javascripts/application.js")
will produce:
<script src='http://www.myserver.com/javascripts/application.js' type='text/javascript
charset='utf-8'></script>
71 72 73 74 75 76 |
# File 'lib/ymdp/view/application_view.rb', line 71 def javascript_include(filename) unless filename =~ /^http/ filename = "#{@assets_directory}/javascripts/#{filename}" end "<script src='#{filename}' type='text/javascript' charset='utf-8'></script>" end |
#render(params) ⇒ Object
Renders a partial into the current view. HTML partial names must be preceded with an underscore.
Rendering an HTML partial
HTML partials are located in app/views. HTML view files can be Haml or ERB. Haml is recommended and takes preference. HTML partials are named _filename.html.haml or _filename.html.erb.
render :partial => 'sidebar'
will find app/views/_sidebar.html.haml or app/views/_sidebar.html.erb and render its contents into the current view.
Specify a full path to indicate a specific template.
render :partial => 'sidebar.html.erb'
will find app/views/_sidebar.html.erb' and render it even if app/views/_sidebar.html.haml exists.
render :partial => 'shared/sidebar'
will find app/views/shared/_sidebar.html.haml and render its contents into the current view.
Rendering a JavaScript partial
You can render a single JavaScript file or send an array to concatenate a set of JavaScript files together asone script block.
Rendering a single JavaScript partial
JavaScript partials are located in app/javascripts and are named filename.js
render :javascript => 'application'
will find app/javascripts/application.js and render its contents into the current view in an inline script block.
render :javascript => 'shared/sidebar'
will find app/javascripts/shared/sidebar.js and render its contents into the current view in an inline script block.
Rendering a stylesheet partial
Stylesheets are located at app/stylesheets and are named filename.css
Rendering multiple partials
Pass an array to render to combine multiple files into a single inline block. This is useful for compression and validation, as it allows a set of files to be compressed or validated in a single context.
render :javascript => ['application', 'flash', 'debug']
will combine the contents of app/javascripts/application.js, app/javascripts/application.js, and app/javascripts/application.js into a single script block in the current view.
Pass a :filename parameter to set the name of the combined file. Currently the combined file only exists on disc while it’s being compressed and/or validated, but in the future this may be expanded to save multiple files as a single external asset.
render :javascript = ['application', 'flash', 'debug'], :filename => 'javascripts'
Currently the :filename parameter is simply a convenience.
Multiple partials of any type can be rendered.
For example:
render :partial => ['header', 'footer', 'sidebar'], :filename => 'html_layouts'
will find app/views/_header.html.haml, app/views/_footer.html.haml, and app/views/_sidebar.html.haml and write them to a temporary file called tmp/html_layouts before rendering that file into the current view.
This feature is intended mainly for JavaScript and CSS.
For example:
render :stylesheet => ['application', 'colors'], :filename => 'styles'
will render app/stylesheets/application.css and app/stylesheets/colors.css as a single temporary file called tmp/styles before rendering that file into the current view.
If compression and validation options are turned on, the resulting temporary file will be compressed and/or validated before being rendered into the current view. This will result in a more efficient compression and a more effective validation.
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/ymdp/view/application_view.rb', line 176 def render(params) output = [] unless params.has_key?(:tags) params[:tags] = true end output << render_html_partial(params) output << render_javascript_partial(params) output << render_stylesheet_partial(params) output.flatten.join("\n") end |
#supported_languages ⇒ Object
Returns an array of the country codes of all languages supported by the application, which is determined by the language-specific folders in “app/assets/yrb”.
Examples
supported_languages
# => ["en-US", "de-DE", "es-ES", "es-MX"]
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ymdp/view/application_view.rb', line 21 def supported_languages dirs = Dir["#{BASE_PATH}/app/assets/yrb/*"].map do |path| filename = path.split("/").last filename end dirs.unshift(dirs.delete("en-US")) raise "Default YRB key en-US not found" unless dirs.include?("en-US") dirs end |