How can I include .less file in Zend Framework 2? - zend-framework2

I can include .less file in such way:
<link rel="stylesheet/less" type="text/css" href="<?= $this->basePath('backoffice/less/master.less') ?>">
but how can I do this using ZF2, like have done with .css file
->prependStylesheet($this->basePath('backoffice/css/style.css'))

To create this link
<link rel="stylesheet/less" type="text/css" href="/backoffice/less/master.less">
You should be able to do:
->prependStylesheet(array(
'rel' => 'stylesheet/less',
'type' => 'text/css',
'href' => $this->basePath('backoffice/less/master.less')
))
Not tested - only theoretically by looking into code.

Related

application_helper.rb not refreshing after restart server Rails

I have an Rails app using i18n. It's fine until I try to modify the application_helper.rb there is a part that stands:
def language_css(language)
case language
when 'en'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/en.css">'
when 'es-MX'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/es.css">'
when 'fr'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/fr.css">'
when 'jp'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/jp.css">'
when 'ch'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/ch.css">'
when 'ar'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/ar.css">'
default
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/es.css">'
end
end
I tried to change default "es.css" for "en.css" but I see no changes. The es.css file is still the default css file, not en.css.
So I even tried CtrlC and rails s, several times, no luck.
There is no problem with reloading, your case statement is simply not doing what you think. default here is a method call, not a keyword. You’re looking for else. Properly indented, this becomes clear:
case language
when 'en'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/en.css">'
# Skipping some cases because they’re not needed for the example
when 'ar'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/ar.css">'
default
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/es.css">'
end
As we can see, the second and third lines of the when 'ar' case are never reached, because it always returns. You instead want else:
case language
when 'en'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/en.css">'
# Skipping some cases because they’re not needed for the example
when 'ar'
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/ar.css">'
else
return raw '<link rel="stylesheet" type="text/css" href="/assets/stylesheets/es.css">'
end
Your code can ultimately be written more simply as:
def language_css(language)
css_file = {
'en' => 'en.css',
'es-MX' => 'es.css',
'fr' => 'fr.css',
'jp' => 'jp.css',
'ch' => 'ch.css',
'ar' => 'ar.css',
}.fetch(language, 'es.css')
raw %(<link rel="stylesheet" type="text/css" href="/assets/stylesheets/#{css_file}">)
end
I am wondering if you just have a bug in your code you're missing. I'd rewrite your method entirely.
def language_css(language)
asset = language.downcase
return raw stylesheet_link_tag(asset) if Rails.application.assets.find_asset("#{asset}.css").present?
raw stylesheet_link_tag('es')
end
You'll need to rename es.css to es-mx.css.

Using jquery-ui-rails datepicker in an Ruby on Rails website. datepicker method not defined

I got this problem writing the website with Ruby on Rails.
bundle show
* jquery-ui-rails (4.0.1)
In app/assets/javascripts/application.js
//= require jquery.ui.datepicker
In app/assets/stylesheets/application.css
*= require jquery.ui.datepicker
In app/views/layouts/application.html.erb
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
And then in somepage.html.erb, I got
<script type="text/javascript">
$(function(){
$("#startdate").datepicker();
$("#enddate").datepicker();
});
</script>
When running it, Chrome says that
Uncaught TypeError: Object [object Object] has no method 'datepicker'
I suppose that resource not being referred properly is the cause because the problem could be fixed by adding the follows into app/views/layouts/application.html.erb
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
These could be found at http://jqueryui.com/datepicker/. I saw demos having working datepickers without these codes. But I do not understand why it is not working in mine. Anyone got any suggestions? like something to add other than those I mentioned above?
It probably because of either of the following:
1) You are including multiple javascript files in your application.html.erb which is leading to havoc when put altogether.
2) You are using some other javascript file that also using $ just like jquery is using.
Having two same symbols is the possible cause of getting no method 'datepicker' for the jquery.
The alternative is to replace all the occurences of $ with jQuery
OR
Just wrap your jquery code insode a block like the following:
jQuery(function($){
//all jQuery code which uses $ should be here.
});

Using asset pipeline outside of ERB

Is there a way to use the rails asset pipeline outside of erg? When I call stylesheet_link_tag(), I get a normal /stylesheets/ link instead of an /assets/ like I'd expect. I suspect that the stache gem just needs to register something with the asset pipeline, but I'm not sure what.
I'm using this gem: https://github.com/agoragames/stache
The code I'm using:
module Layouts
class Application < ::Stache::View
include ActionView::Helpers::AssetTagHelper::StylesheetTagHelpers
def title
'foobar'
end
def stylesheets
[
[stylesheet_link_tag('reset', :media => 'all')]
]
end
def javascripts
end
end
end
It's generating:
<link href="/stylesheets/reset.css" media="all" rel="stylesheet" type="text/css" />
It should be generating (it does this in erb templates):
<link href="/assets/reset.css?body=1" media="all" rel="stylesheet" type="text/css" />
Using rails 3.2.3.
Try
def stylesheets
[
[stylesheet_link_tag("#{ActionController::Base.helpers.asset_path('reset.css')}", :media => 'all')]
]
end
also read https://stackoverflow.com/a/9341764/643500
The proper solution is to remove the:
include ActionView::Helpers::AssetTagHelper::StylesheetTagHelpers
line at the top.

Hpricot search all the tags under one specific namespace

For example I have the following code:
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><io:content part="title" /></title>
<link rel="icon" href="/document/7e9f29e2-cdee-4f85-ba25-132fa867aa90/latest" type="image/x-icon" />
<n1:content description="Standard CSS" uuid="d069071c-3534-4945-9fb6-2d7be35a165e" />
<n1:term>Content Development</n1:term>
</head>
This XHTML snippet is not strictly legal because there is no namespace declared before so I cannot use Nokogiri which has better namespace support.
I want to do a single search that can find both the node <n1:content> and <n1:term> and all the tags under 'n1' namespace.
How to achieve that? Thanks!
It looks like Hpricot does not handle namespaces that fully.
You can select if you know the element regardless of prefix:
doc.search("title")
=> #<Hpricot::Elements[{elem <title> {emptyelem <io:content part="title">} </title>}]>
... but this is not what you asked.
Here's my hack workaround: find all namespace elements using regex first, then search for those using Hpricot:
elems = doc.to_s.scan(/<\s*(n1:\w+)/).uniq.join("|")
=> "n1:content|n1:term"
doc.search(elems)
=> #<Hpricot::Elements[{emptyelem <n1:content description="Standard CSS" uuid="d069071c-3534-4945-9fb6-2d7be35a165e">}, {elem <n1:term> "Content Development" </n1:term>}]>

Rails ActionController::UnknownAction for favicon

I have following line in my app/views/layouts/application.html.erb
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
This somehow causes following error message int he logs and the favicon is not loaded. This happens only for certain paths not for the whole application.
ActionController::UnknownAction (No action responded to images. Actions:
How to get rid of it ?
Use the favicon_link_tag helper instead:
<%= favicon_link_tag "/favicon.ico" %>
Just make sure your favicon.ico file is in the public folder... and do not forget the forward slash.
Believe me.. try prefixing images with a slash and this should work
<link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon" />

Resources