当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

angular-rails-templates

授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 闻人升
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Angular Rails Templates

Gem Version

Adds your HTML templates into Angular's $templateCache using Rails asset pipeline.

IMPORTANT: for Rails 4.2+ use version 1.0+ of this gem. For Rails 3 - 4.1 use version 0.x

Branch Build Status
master
0-x-stable

It removes the need for AJAX calls to retrieve the templates (or for you to manually set them into the DOM).

Usage

1. Add the Gem

In Gemfile

gem 'angular-rails-templates'

2. Include Templates in Rails Asset Pipeline

Then, in your application.js file, require angular-rails-templates and your templates:

//= require angularjs
// ...
//= require angular-rails-templates
//
// Templates in app/assets/javascript/templates
//= require_tree ./templates
// OR
// Templates in app/assets/templates (but see step 5)
//= require_tree ../templates

Make sure to require angular-rails-templates before you require your templates.

Name your templates like you would name any other Rails view. The .html part is required. If it is not present your views will not be added to angular's template cache.

foo.html
foo.html.erb
foo.html.haml
foo.html.slim

Angular Rails Templates will try to load support for the following markups if their gems are present:

Extension Required gem
.erb -
.str -
.haml haml
.slim slim
.md liquid, rdiscount, redcarpet, bluecloth, kramdown, maruku

See Advanced if you would like to use other markup languages.

3. Add a Dependency in your Angular Application Module

Your Angular module needs to depend on the templates module. (configurable, see Advanced Configuration)

angular.module('myApplication', ['templates']);

4. Use your Templates

No matter what the source file extension is, your template's url will be #{base_name}.html

For example:

main.html => main.html
widget.html.haml => widget.html
modals/confirm.html.slim => modals/confirm.html
modals/dialog.html.slim.erb.str => modals/dialog.html # don't do this

The templates can then be accessed via templateUrl as expected:

// Template: app/assets/templates/yourTemplate.html.haml
{
  templateUrl: 'yourTemplate.html'
}

Or anything else that uses $templateCache or $templateRequest

<div ng-include='"yourTemplate.html"'></div>

5. Avoid name collisions

If you have app/assets/javascript/user.js and app/assets/templates/user.html, the former one will actually hide the latter. This is due to how Rails asset pipeline sees asset files, both are served under /assets/user.js. Please use namespacing to combat this issue.

Advanced Configuration

Angular Rails Templates has some configuration options that can be set inside config/application.rb

Here are their default values:

# config/application.rb
config.angular_templates.module_name    = 'templates'
config.angular_templates.ignore_prefix  = %w(templates/)
config.angular_templates.inside_paths   = ['app/assets']
config.angular_templates.markups        = %w(erb str haml slim md)
config.angular_templates.extension      = 'html'

Configuration Option: module_name

This configures the module name that your templates will be placed into.It is used to generate javascript like:

angular.module("<%= module_name %>")...

Although it is not recommended, you can set module_name to the name of your main application module and remove require angular-rails-templates from your javascript manifest to have your templates directly injected into your app.

Configuration Option: ignore_prefix

If you place your templates in app/assets/templates this option is mostly useless.

ignore_prefix will be stripped from the beginning of the templateUrl it reports to angularjs.

Since the default ignore_prefix is [templates/], any templates placed under app/assets/javascripts/templates will automatically have short names. If your templates are not in this location, you will need to use the full path to the template.

You can set config.angular_templates.ignore_prefix to change the default ignore prefix. Default is [templates/].

// Templates in: app/assets/javascripts/templates (default)
// ignore_prefix: templates/ (default)
{
  templateUrl: 'yourTemplate.html'
}
// This won't work:
{
  templateUrl: 'templates/yourTemplate.html'
}
// Templates in: app/assets/javascripts/my_app/templates (custom)
// ignore_prefix: templates/ (default)
{
  templateUrl: 'my_app/templates/yourTemplate.html'
}

// ignore_prefix: my_app/templates/ (custom)
{
  templateUrl: 'yourTemplate.html'
}

Configuration Option: inside_paths

Templates only from paths matched by inside_paths will be used. By default anything under app/assets can be templates. This option is useful if you are using this gem inside an engine. Also useful if you DON'T want some files to be processed by this gem (see issue #88)

Configuration Option: markups

Any markup that Tilt supports can be used, but you may need to add a gem to your Gemfile. See Tilt for a list of the supported markups and required libraries.

# Gemfile
gem "asciidoctor"
gem "radius"
gem "creole"
gem "tilt-handlebars"

# config/application.rb
config.angular_templates.markups.push 'asciidoc', 'radius', 'wiki', 'hbs'

If you would like to use a non-standard extension or you would like to use a custom template, you just need to tell Tilt about it.

# config/initializers/angular_rails_templates.rb
Tilt.register Tilt::HamlTemplate, 'nghaml'

# config/application.rb
config.angular_templates.markups.push 'nghaml'

Note: You would still need to use foo.html.nghaml

Configuration Option: extension

By default this gem looks only at templates with .html suffix, eg. foo.html or foo.html.haml. This extension allows you to change that to another extension

License

MIT License. Copyright 2017 pitr

Authors & contributors

 相关资料
  • 我目前正在尝试创建一个小型web应用程序,Angular 2作为前端,Rails 4作为后端-我的后端只是一个API,而我的前端只是发送请求。 我今天在试图提交帖子请求时遇到了CSRF令牌真实性错误-如何将这些CSRF标题添加到我的Angular 2标题中?

  • 我正在处理的当前场景要求我在具有rails后端的角度应用程序中集成SAML或单点登录。首先,我能够向我的IdP发送一个请求,并收到一个XML形式的响应,我使用nokogiri进行解析,并且我有一些凭据,可以在数据库中创建用户。但问题是,我无法在用户创建后登录并保持他们的登录状态。 我在前端实现了ng token auth,在后端设计了\u token\u auth。我已经使用omniauth sa

  • Ruby on Rails 是一个用于开发数据库驱动的网络应用程序的完整框架。Rails基于MVC(模型- 视图- 控制器)设计模式。从视图中的Ajax应用,到控制器中的访问请求和反馈,到封装数据库的模型,Rails 为你提供一个纯Ruby的开发环境。发布网站时,你只需要一个数据库和一个网络服务器即可。 Ruby On Rails是一个用于编写网络应用程序的软件包。它基于一种计算机软件语言Ruby

  • cPanel的这个接口允许您创建和安装Ruby on Rails应用程序。 如果您开发了Ruby on Rails应用程序,则可以使用此界面将其部署到服务器。 要创建Ruby on Rails应用程序,请按照下列步骤操作 - Step 1 - 单击cPanel Home的Software Section下的Ruby on Rails。 Step 2 - 在Ruby on Rails接口中,您将找到

  • Ruby on Rails 惊人的活跃度主要是因为本身采用了新颖的技术和好的时机。但技术优势随着时间的推移而减弱;纵然有好的时机,时间久了影响也会逐渐减少。Rails 为何不仅能继续存在,还依然具有影响力并带领社区继续前进呢?需要更进一步地解释。我提议以下这篇内容,这是我们的基本信条,也是饱受争议的信条。 这篇信条,在过去 10 年里不断演进,是 Rails 主要的支柱,同时也是基本的支柱。我不自

  • 本文向大家介绍Ruby on Rails 从Rails 4.2升级到Rails 5.0,包括了Ruby on Rails 从Rails 4.2升级到Rails 5.0的使用技巧和注意事项,需要的朋友参考一下 示例 注意:升级Rails应用程序之前,请务必确保将代码保存在版本控制系统(例如Git)上。 要从Rails 4.2升级到Rails 5.0,必须使用Ruby 2.2.2或更高版本。如果需要,