I'm using Django with django-compressor and pySCSS to compile SASS to regular CSS.
In django-compressor there's a filter that can convert variables like {{ STATIC_URL }} to the actual value. Unfortunately that runs after the SASS precompiler.
When I put in that literal in the .sass file, it inserts whitespaces between the curly braces:
background-image: url('{{ STATIC_URL }}img/background-lines.png')
becomes
background-image:url(' { { STATIC_URL }}img/background-lines.png')
Is there a way to insert a literal string into SASS source files? Or a way to escape the curly braces so they're not changed?
Related
I have a code <span>{{ trans('lang.color.' . $bet->color) }}</span></div> which displays the bet amount for a specific color.
My lang file:
'color' => [
'red' => 'red',
'zero' => 'green',
'black' => 'black',
],
Which is responsible for the fact that if the bet was placed on red, the site will say: set to red.
How can I correctly display HTML code in color variables? For example, if i write 'red' => '<div style="font-color:#FF0000">red</div>' site does not convert the text to HTML, and writes the div with text. How to make read text file HTML?
My laravel version: 5.1.10.
Displaying Unescaped Data
By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
{!! trans('lang.color.' . $bet->color) !!}
Note: Be very careful when echoing content that is supplied by users of your application. Always use the double curly brace syntax to escape any HTML entities in the content.
I'm trying to place some whitespace at the start of a string, like so:
- sbSecId: 4
title: ' VideoJS'
link: /examples/video/instream/videojs/pb-ve-videojs.html
isLastSubSectionItem: 0
isHeader: 0
isSectionHeader: 0
sectionTitle:
subgroup: 1
this is for a site being generated by Jekyll. I'm using Liquid to make an array of the yml file, looping through the array and displaying the title key's value like so:
{{thisSubItem.title}}
despite having the key value in quotes the whitespace is being deleted. Is this a Jekyll thing? How can I get the whitespace to be retained?
This is not a Jekyll thing this html that strips unnecessary spaces.
Here you can use CSS rule
<span style="white-space: pre;">{{thisSubItem.title}}</span>
Or by replacing spaces by non-breaking spaces
{% assign preserved_ws = thisSubItem.title | replace: " ", " " %}
{{ preserved_ws }}
Nevertheless, if it's only a presentation matter, you must get rid of spaces and go with CSS margins.
Instead, you could leave the spaces out of the string and have it in the layout or wherever the value is being rendered. You could remove the whitespace from the string and wouldn't need to worry about remembering to keep the whitespace consistent.
{{thisSubItem.title}}
I have the follow code in Blade using a ternary operator:
<td>{{isset($arrTemp[$ccc->id]) ? "<a hfet='".url('/cc/'.$cc->id)."'>".count($arrTemp[$cc->id])."</a>": 'N/A'}}</td>
If it find somenthing for the array key $cc->id, should thisplay the value with the link atteched to it.
But the page is rendering <a hfet='http://my.test/cc/56526235'>4</a> the string itself.
What am I missing?
When you use {{ }} the output is automatically escaped to prevent XSS attacks. You can use {!! !!} instead, which will not escape the string.
Source: https://laravel.com/docs/5.4/blade#displaying-data
I am trying to sanitalize Solr search results, cause it has html tags inside:
ActionController::Base.helpers.sanitize( result_string )
It is easy to sanitalize not highlighted string like: I know <ul><li>ruby</li> <li>rails</li></ul>.
But when results is highlighted I have additional important tags inside - <em> and </em>:
I <em>know</em> <<em>ul</em>><<em>li</em>><em>ruby</em></<em>li</em>> <<em>li</em>><em>rails</em></<em>li</em>></<em>ul</em>>.
So, when I sanitalize string with nested html and highlighting tags, I get string with peaces of htmls tags. And it is bad :)
How can I sanitalize highlighted string with <em> tags inside to get correct result (string with <em> tags only)?
I found the way, but it's slow and not pretty:
string = 'I <em>know</em> <<em>ul</em>><<em>li</em>><em>ruby</em></<em>li</em>> <<em>li</em>><em>rails</em></<em>li</em>></<em>ul</em>>'
['p', 'ul', 'li', 'ol', 'span', 'b', 'br'].each do |tag|
string.gsub!( "<<em>#{tag}</em>>", '' )
string.gsub!( "</<em>#{tag}</em>>", '' )
end
string = ActionController::Base.helpers.sanitize string, tags: %w(em)
How can I optimize it or do it using some better solution?
to write some regex and remove html_tags, but keep <em> and </em> e.g.
Please help, thanks.
You could call gsub! to discard all tags but keep only tags that are independent, or that are not included in html tag.
result_string.gsub!(/(<\/?[^e][^m]>)|(<<em>\w*<\/em>>)|(<\/<em>\w*<\/em>>)/, '')
would do the trick
To explain:
# first group (<\/?[^e][^m]>)
# find all html tags that are not <em> or </em>
# second group (<<em>\w*<\/em>>)
# find all opening tags that have <em> </em> inside of them like:
# <<em>li</em>> or <<em>ul</em>>
# third group (<\/<em>\w*<\/em>>)
# find all closing tags that have <em> </em> inside of them:
# </<em>li</em>> or </<em>ul</em>>
# and gsub replaces all of this with empty string
I think you can use the sinitize:
Custom Use (only the mentioned tags and attributes are allowed, nothing else)
<%= sanitize #article.body, tags: %w(table tr td), attributes: %w(id class style) %>
So, something like that should work:
sanitize result_string, tags: %w(em)
With an additional parameter to sanitize, you can specify which tags are allowed.
In your example, try:
ActionController::Base.helpers.sanitize( result_string, tags: %w(em) )
It should do the trick
Is there a standard, or simple, way to define sass variables on the command line, or pass them from the environment? What I am doing is trying to create to different CSS files from a single SCSS source file. Each output file will be specialized for a certain target device and included from the HTML page with media queries.
Update: It looks like this may not be enough. The #if syntax doesn't actually allow conditional sass blocks, only conditional CSS blocks. This means even if I can define a variable it wouldn't allow me to do different processing in the file. Any ideas?
Use SASS custom functions. A complete working example below.
styles.sass:
$primary_color: getenv("SASS_VAR_PRIMARY_COLOR", white)
body
background: $primary_color
sass_functions.rb:
module Sass::Script::Functions
def getenv(name, default)
assert_type name, :String
value = ENV.fetch(name.value, nil)
if not value
return default
end
begin
Sass::Script::Parser.parse(value, 0, 0)
rescue
Sass::Script::String.new(value)
end
end
end
Command line:
SASS_VAR_PRIMARY_COLOR='#123456' sass -r ./sass_functions.rb styles.sass styles.css
If you use Compass, you may put module Sass::Script::Functions directly into config.rb and compile the styles with:
SASS_VAR_PRIMARY_COLOR='#123456' sass --compass styles.sass styles.css
The facility I needed is simply not in SASS. Even if you could define variables on the command-line the "if" facility is insufficient to actually do conditional blocks in the style sheet.
Instead I've resorted to the old stand-by of M4. I simply preprocess the SASS with M4, doing command-line options and full conditional blocks.
I couldn’t find a way to specify sass variables from the command line but you can mimic this behaviour by creating multiple specialized SCSS files that import a base SCSS file.
For example:
// base_style.scss
#debug "* Format = #{$format} *";
#if $format == 1
{
$body-font-family: "Palatino" !global;
}
#else if $format == 2
{
$body-font-family: "Arial" !global;
}
#else
{
#warn "Undefined format";
}
body
{
font-family: $body-font-family;
}
// style_1.scss
$format: 1;
#import "base_style.scss";
// style_2.scss
$format: 2;
#import "base_style.scss";
Then from the command line:
sass style_1.scss style_1.css
sass style_2.scss style_2.css
Note that you can also specify the rules for different target media in one single (S)CSS file with distinct media queries in it. In this case, you only need one source file.
I'm pretty sure I understand what you're saying. Sometimes it's good to group things together in a .scss file that would end up in conditional stylesheets so you can find them easier.
At the moment there's still not the ability to compile a sass stylesheet in to separate stylesheets without grouping all your conditional rules in to a partial.
However, using Paul Irish's suggestion to conditionally add a class name to the html tag: http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html class=""> <!--<![endif]-->
You could do something like this:
.something {
regular rules...
.ie8 & {
conditional rules...
}
}
That way you can still have organization as well as conditional styling. I hope this helps!