I have built an Angular library containing a styles.css file with some custom CSS. When I include the library in a test application in the same repository it works fine:
library-repo/
├── projects/
│ ├── custom-library/
├── src/
│ ├── app/
│ ├── app.component.html
However, once I publish the library and import it in a separate application, the custom styles are not applied. How can I bundle the styles.css with the library so that the library components are styled consistently across all host application?
Put the styles you want in the styles file at this path
/src/styles.scss
I really like eslint for es6 projects. Previously I've used it for new projects. Now I want to add it to a legacy project.
Fixing all pre-existing lint issues in one go is too much effort. Can I configure eslint (in .eslintrc.js) to only check files where I've explicitly enabled it with /* eslint-enable */ or similar?
ESLint has no default-disabled state that can be toggled by a file comment. You might be able to use .eslintignore for this purpose, however. You can ignore everything and then gradually whitelist files as you migrate them by using ! to un-ignore individual files. For example:
.
├── .eslintignore
├── .eslintrc.js
├── package.json
├── node_modules
│ └── ...
├── src
│ ├── index.js
│ └── module
│ └── foo.js
└── yarn.lock
Then your .eslintignore could look something like this:
# Start by ignoring everything by default
src/**/*.js
# Enable linting just for some files
!src/module/foo.js
In this case, src/index.js would be ignored, but it would lint src/module/foo.js.
I am using Sprockets with Sinatra, as suggested in Sinatra's page docs, but I can't make it work.
When I go to localhost:4567, the page loads correctly but with no styles. If I go to localhost:4567/assets/app.css, I get a not found error. I wonder what I am missing or what is wrong in the way I am using Sprockets?
This is my folder structure:
├── assets
│ ├── css
│ │ ├── app.css
│ │ ├── base.css
│ │ └── normalize.css
├── bin
│ └── app
├── lib
│ ├── app_assets.rb
│ └── main.rb
├── spec
│ ├── spec_helper.rb
│ └── main_spec.rb
├── views
│ └── index.erb
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── .rspec
└── .ruby-version
The contents of app.css are:
//= require normalize
//= require base
The contents of app_assets.rb are:
module AppAssets
def self.environment root_path
environment = Sprockets::Environment.new root_path
environment.append_path './assets/css/'
environment
# get assets
get '/assets/*' do
env['PATH_INFO'].sub!('/assets', '')
settings.environment.call(env)
end
end
end
The contents of lib/main.rb are:
require 'sinatra'
require 'sprockets'
require 'app_assets'
class Main < Sinatra::Base
set :views, "#{settings.root}/../views"
get '/' do
erb :index
end
end
The file views/index.erb contains the line:
<link rel="stylesheet" href="assets/app.css">
And the contents of bin/app are:
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'sinatra'
require 'sprockets'
require 'app_assets'
require 'main'
Main.run!
Which I run typing:
$ bin/app
Any help would be appreciated, I'm sure I made something wrong but I can't see what. Can anybody spot it?
The app_assets.rb file is the problem here. When you require this file inside another file, the methods you define inside this module are not automatically included. You need to explicitly include AppAssets wherever you need the self.environment method to exist.
The second issue here is that self.environment is not equivalent to settings.environment. If I understand correctly, what you're trying to do is define the asset routing whenever the module gets included. To achieve this one way is to use the included hook for modules. This hook gets run every time you include a module inside a context. If you use that, the code in app_assets.rb turns to:
module AppAssets
def self.included(klass)
environment = Sprockets::Environment.new klass.settings.root
# note the change to path. Since the file where this gets included
# is inside a sub-folder, we need to traverse to one level above.
environment.append_path '../assets/css/'
klass.set :environment, environment
klass.get '/assets/*' do
env['PATH_INFO'].sub!('/assets', '')
klass.settings.environment.call(env)
end
end
end
The klass argument to this hook is the class into which this module is included. In our case this is the Sinatra class you've described in main.rb. That file looks like:
class Main < Sinatra::Base
include AppAssets
# Same as what you have
end
There's a Sinatra Recipes article about using Sprockets with Sinatra: http://recipes.sinatrarb.com/p/asset_management/sprockets?#article
I made an app using the ionic framework and would like to make it run on meteor.
The App is build on top of the sidemenu template you can create tying ionic start myApp sidemenu.
Here is the Git of the port or just
> git clone https://github.com/Xample/sidemenu-meteor
> cd sidemenu-meteor/
> meteor
How to do so ?
Create both projects:
ionic start ionicProject sidemenu
meteor create meteorProject
Reorganise the files:
All the important files within a ionicframework comes into a www folder start to reorganise them to fit with the meteor good practices.
In the meteor root folder, create a client, server, css, and public folder
Copy the ionicProject/www/css files into the meteorProject/css
Copy all the js files, the main index.html and the templates files into the meteorProject/client folder
All the other files which only needs to be served (images, audio, documents) must be put into the meteorProject/public folder
The ionicProject/www/lib will be replaced by a meteor package. Do not include it.
The meteorProject/server folder will remain empty
Now we need to ensure meteor will load the app.js file before the other one.
Create the meteorProject/client/lib folder
Move the app.js file into that one
You should have the following structure:
├── client
│ ├── controllers.js
│ ├── index.html
│ ├── lib
│ │ └── app.js
│ └── templates
│ ├── browse.html
│ ├── login.html
│ ├── menu.html
│ ├── playlist.html
│ ├── playlists.html
│ └── search.html
├── css
│ └── style.css
├── public
│ └── img
│ └── ionic.png
└── server
Import the meteor packages:
meteor add urigo:ionic
That one will include other dependent packages below:
added mquandalle:bower at version 0.1.11
added urigo:ionic at version 0.0.6
added urigo:angular at version 0.4.8
added urigo:angular-ui-router at version 0.6.1
added tinytest at version 1.0.3
bower package allows to use the bower package manager. Well basically it is a tool to allow easily include other packages using a description file (which might be used by angular but not packaged for meteor yet) you will likely be using it for installing ngCordova. No need for this sidemenu port btw
ionic correspond to all the files we did not copied from the ionicProject/www/lib folder. Those are now included by default into your meteor project.
angular is basically the same as for ionic. Angular is now included into your meteor project as well + the 'angular-meteor' which will be useful for bootstrapping the app.
angular-ui-router same story but for the router. This will be mandatory to handle the url and forward them to the right page. It's a little bit like the Iron router but angular way and compatible
tinytest is added per default, no real need for the port
Edit and port the files
index.html
- On meteor the header and the body are parsed and generated back by the framework. In short, meteor packs everything and generate the script and style files for you. Conversely if you wanted to just include a css or a js file through a or tag within the header / body, they will be dismissed by meteor. This is the reason why we are using packages instead of adding our script by ourselves. This to say, that most of the content of the index.html is now useless and needs to be removed. Even the is not allowed by meteor because it will generate it for you as well…. Moreover, no attributes are allowed in the body. This might be problematic for bootstrapping our project with angularJS. The html files looks like this now:
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>sidemenu-meteor</title>
</head>
<body>
<ion-nav-view></ion-nav-view>
</body>
The templates files:
All the .html files not being into the server, public or private folder are loaded and packed into one big html by meteor. As meteor will search for and pack all the .html it will load but not include all the one within a <template> tag. All the files into the meteorProject/client/templates must be edited and encapsulated within a <template> tag bearing the name attribute such that we can easily find it back later on. For example the template browse.html will be packed such as:
<template name="browse.html">
... browse html file content ...
</template>
Repeat this step for all the templates.
app.js
Open the file and add the manual bootstrap for angularJS, in this manner you manually add the ng-app tag on top of the document.
Meteor.startup(function ()
{
angular.bootstrap(document, ['starter']);
});
Note: You need to do it once everything is loaded, for this reason we are doing it within the Meteor.startup function caller.
Add the 'angular-meteor' package to your application, this will change the Angular delimiters to [[ and ]] instead of the regular conflicting with meteor's handlebars {{ and }}
angular.module('starter', ['angular-meteor','ionic', 'starter.controllers'])
Replace the router's templateUrl path url references to something we can use with the loaded by meteor templates. Remember, we are not storing the templates into the meteorProject/public folder, therefore we cannot load them through templateUrl:'someUrl' you might do it but I do not recommend
templateUrl: "templates/menu.html", becomes template:UiRouter.template('menu.html'),
Repeat this step for all the states in the state provider.
controller.js
just replace the template dependency of the modal. From:
$ionicModal.fromTemplateUrl('templates/login.html', {
to
$ionicModal.fromTemplateUrl('login.html', {
This is again to ensure the template is found correctly. Note that for some reason we have been able to load the templateUrl using the template name. It's still a mystery to me, probably a meteor package port have added this sugar…
playlists.html (but possibly other files)
Edit all the files and replace all the {{ occurrences to [[ and }} to ]]
Basically in this example you will only have to edit playlists.html
Last step
At this stage you should be able to run the ionic sidemenu project under meteor. There is only one thing missing. As you can remember, we changed the delimiters {{}} -> [[]]. Unfortunately, some of the ionic directives are using the regular {{}} delimiters and expect them to be functional. Therefore while adding a <ion-item href="myPath"> this is compiled to something like <a href={{$href()}}> so… now if you click on a menu, the href will be wrong and you will not be redirected to the right page… To fix this, a workaround is to embed the <ion-item> within your own <a href="myRef"> tag. I'm still looking for a better solution…. Still to do so, just refactor all your ion-item such that:
<ion-item nav-clear menu-close href="#/app/search">
becomes encapsulated in
<a href="/#/app/search">
<ion-item nav-clear menu-close >
Search
</ion-item>
</a>
Dependencies
Last thing, meteor will try to minify your javascript during the deployment, doing so you might break the angular code if it is not using the array notation. Just refactor all your methods putting your methods into an array. Read the guide for more information. An alternative is to avoid meteor to minify the code deploying using --debug meteor deploy --debug your-project.meteor.com
To get this tutorial:
> git clone https://github.com/Xample/sidemenu-meteor
> cd sidemenu-meteor/
> meteor
I'm new to Django, and am trying to set up a really simple Django app.
Now, I'm up to chapter 5 in the Django online book : http://www.djangobook.com/en/2.0/chapter05/
All I want to do now, before I start trying databases, is to add in some simple CS and J to the app as is.
So the question is, how do I do this? I only have one app, and I only want a main.css in a css folder, and a main.js in a js folder.
I checked out the https://docs.djangoproject.com/en/1.3/howto/static-files/#staticfiles-in-templates page, but after reading and reading, there didn't seem to be a whole lot to work with in terms of examples.
How do I import the stylesheets/css (is there a helper like CakePHP?), where do I put the CSS and JS files, and do I need to configure the static thingy?
UPDATE: the link : Render CSS in Django does not help much. Mainly in that it didn't work for me, but also I especially don't like the "if debug" part. What happens when I move to prod? Why would I have my media folder defined differently for prod and local dev anyway? Shouldn't it be relative, or even in the same spot?
If you follow django's guidelines, you can simplify your life greatly.
In your sample code, inside your application directory, create a folder called static. Inside this folder, place your css files.
Example:
$ django-admin.py startproject myproject
$ cd myproject
myproject$ python manage.py startapp myapp
myproject$ mkdir myapp/static
myproject$ cd myapp/static
myproject/myapp/static$ nano style.css
In your templates:
<link rel="stylesheet" href="{{ STATIC_URL }}style.css" />
Make sure you add myapp to the INSTALLED_APPS list in settings.py. Now when you use the built-in development server, your style sheet will be rendered correctly.
Django searches for a static directory inside installed applications by default, and with current versions of django, static files are enabled by default.
The Django example has the path my_app/static/my_app/myimage.jpg which
is a little confusing if your app and project have the same name.
This is recommended because when you run collectstatic to gather all your static files, files with the same name will be overwritten. If you have a file called myimage.jpg in another application, it will be overwritten. Giving the application name inside the static directory will prevent this, because the exact directory structure will be replicated inside your STATIC_ROOT directory.
A simple example to illustrate the point. If you have a django project with two apps, like this:
.
├── assets
├── manage.py
├── myapp
│ ├── __init__.py
│ ├── models.py
│ ├── static
│ │ └── myapp
│ │ └── test.txt
│ ├── tests.py
│ └── views.py
├── myproj
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ └── wsgi.py
└── otherapp
├── __init__.py
├── models.py
├── static
│ └── otherapp
│ └── test.txt
├── tests.py
└── views.py
assets is your STATIC_ROOT. Now when you run collectstatic:
.
├── assets
│ ├── myapp
│ │ └── test.txt
│ └── otherapp
│ └── test.txt
├── manage.py
├── myapp
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── models.py
│ ├── static
│ │ └── myapp
│ │ └── test.txt
│ ├── tests.py
│ └── views.py
├── myproj
│ ├── __init__.py
│ ├── __init__.pyc
│ ├── settings.py
│ ├── settings.pyc
│ ├── urls.py
│ └── wsgi.py
└── otherapp
├── __init__.py
├── __init__.pyc
├── models.py
├── static
│ └── otherapp
│ └── test.txt
├── tests.py
└── views.py
You see it is creating the directories as well. In your templates you would now refer to each file with its "namespace" of the app: {{ STATIC_URL }}/myapp/test.txt
The recommended approach has changed again (from at least Django 1.5 I think).
At the top of your template put:
{% load staticfiles %}
Then, using the same directory structure (myapp/static/myapp/style.css):
<link rel="stylesheet" href="{% static 'myapp/style.css' %}" />
Source
For the examples you pointed at to work, your static files need to be in a location accessible to Django's built-in staticfiles app.
There are a couple steps to make this happen:
First, within your project directory (ie beside your manage.py file), you'll need to create a directory to hold your static files. Call it "static_files".
Next, you'll need to let Django know to look in that directory, by specifying it in the list of STATICFILES_DIRS within your settings.py file.
Something like this:
STATICFILES_DIRS = [
'/full/path/to/your/project/static_files/',
]
Within that static_files directory, you can create whatever structure you want, so that is where your css and js directories could go.
After that, you should be able to use the {{ STATIC_URL }} tag in your templates to get access to the base URL of your static files.
So, say, for example, you create project/static_files/css/base.css, you would use it in your template like so:
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/css/base.css" />
Hope that helps!
Edit
With the default settings for STATICFILES_FINDERS, Django should automatically serve up any files from directories listed in your STATICFILES_DIRS -- see the docs for details.
If this doesn't work, some things to check:
Have you edited your STATICFILES_FINDERS setting to something other than the default?
Is django.contrib.staticfiles in your list of INSTALLED_APPS in settings.py?
Are you using Django's built-in server (python manage.py runserver)?
Do you have DEBUG = True in your settings.py? If not, you'll need to either set it to True or use the insecure option (python manage.py runserver --insecure). When going to production, check out the collectstatic command.
finally i got after many days here is how should be done
1> you need your static file in your app along with in your project
2> here is setting.py
if DEBUG:
STATIC_ROOT = "/PycharmProjects/don/admin/shopping/static/css"
STATICFILES_DIRS =(
#os.path.join(os.path.dirname(BASE_DIR),"static","static"),
os.path.join(BASE_DIR, "static"),
)
3>views.py
from django.views.generic import TemplateView
class HomeView(TemplateView):
template_name = 'index.html'
4> template file
{% load staticfiles %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
5> urls.py
from shopping.views import HomeView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', HomeView.as_view())
]
if settings.DEBUG:
urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
6> python manage.py collectstatic
have fun :)
The handling of static files has changed significantly (for the better) in Django 1.3. In particular is the flexible difference between static files (think code required CSS/JS/images) and media files (think images uploaded by users in the use of you application). The staticfiles app handles what you are asking for.
Put django.contrib.staticfiles in your settings.py INSTALLED_APPS. Then create a static directory inside your app directory - so project/app/static/. Within that static directory organize your support files as you like (css/, js/, images/, icons/, etc). staticfiles by default will look in the static/ directory for every app listed in INSTALLED_APPS. Not just your own, but all the Django apps and third-party apps. (sidenote: the Admin in Django 1.4 moves to this paradigm while in 1.3 it still uses the ADMIN_MEDIA_PREFIX).
The staticfiles app knows about all those static/ directories in all the apps but it needs to collect all that content in order to serve it. In development, when using manage.py runserver it is handled for you. Django will run your site and automatically deliver all the static content from all the static sources. (Sources, as mentioned in another answer, are set in the STATICFILES_FINDERS setting.) When not using runserver, use manage.py collectstatic to gather all the static files into the folder defined in STATIC_ROOT. Keep this directory empty. It is a collection destination. Of course your web server will need to be configured to serve this directory.
Now there is one more piece - the view. This is where the STATIC_URL comes into play. When referring to static files in your templates, the easiest way is to use {{ STATIC_URL }}. Django by default makes that variable available to any view using the RequestContext. Do something like this - <link rel="stylesheet" href="{{ STATIC_URL }}/css/main.css" />.
For more information and more ways to refer to STATIC_URL in your templates, check out this answer to a similar question.
Here's a hacky workaround for views.py when debug in settings.py is False
def my_style(request):
my_file = open('path to arbitrary css file', 'r')
response = HttpResponse(content=my_file.read())
response['Content-Type'] = 'text/css'
return response
and here's the urls.py
urlpatterns = [
path('hello', views.hello_world, name='hello_world'),
path('static/my_style/', views.my_style, name='my_style'),
]
And here's the insert for the html file
<link rel="stylesheet" type="text/css" href="static/my_style">
Though this is clearly not how Django was meant to be used, and I'm guessing this could have some security implications.