<commit_before># Lotus::View

A View layer for [Lotus](http://lotusrb.org).

It's based on a **separation between views and templates**.

A _view_ is an object that encapsulates the presentation logic of a page.
A _template_ is a file that defines the semantic and visual elements of a page.
In order to show a result to an user, a template must be _rendered_ by a view.

Keeping things separated helps to declutter templates and models from presentation logic.
Also, since views are objects, they are easily testable.
If you ever used [Mustache](http://mustache.github.io/), you are already aware of the advantages.

Like all the other Lotus compontents, it can be used as a standalone framework or within a full Lotus application.

## Status

[![Gem Version](http://img.shields.io/gem/v/lotus-view.svg)](https://badge.fury.io/rb/lotus-view)
[![Build Status](http://img.shields.io/travis/lotus/view/master.svg)](https://travis-ci.org/lotus/view?branch=master)
[![Coverage](http://img.shields.io/coveralls/lotus/view/master.svg)](https://coveralls.io/r/lotus/view)
[![Code Climate](http://img.shields.io/codeclimate/github/lotus/view.svg)](https://codeclimate.com/github/lotus/view)
[![Dependencies](http://img.shields.io/gemnasium/lotus/view.svg)](https://gemnasium.com/lotus/view)
[![Inline docs](http://inch-ci.org/github/lotus/view.svg?branch=master)](http://inch-ci.org/github/lotus/view)

## Contact

* Home page: http://lotusrb.org
* Mailing List: http://lotusrb.org/mailing-list
* API Doc: http://rdoc.info/gems/lotus-view
* Bugs/Issues: https://github.com/lotus/view/issues
* Support: http://stackoverflow.com/questions/tagged/lotus-ruby
* Chat: https://gitter.im/lotus/chat

## Rubies

__Lotus::View__ supports Ruby (MRI) 2+

## Installation

Add this line to your application's Gemfile:

    gem 'lotus-view'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install lotus-view

## Usage

### Conventions

  * Templates are searched under `Lotus::View.configuration.root`, set this value according to your app structure (eg. `"app/templates"`).
  * A view will look for a template with a file name that is composed by its full class name (eg. `"articles/index"`).
  * A template must have two concatenated extensions: one for the format one for the engine (eg. `".html.erb"`).
  * The framework must be loaded before rendering the first time: `Lotus::View.load!`.

### Views

A simple view looks like this:

```ruby
require 'lotus/view'

module Articles
  class Index
    include Lotus::View
  end
end
```

Suppose that we want to render a list of `articles`:

```ruby
require 'lotus/view'

module Articles
  class Index
    include Lotus::View
  end
end

Lotus::View.configure do
  root 'app/templates'
end

Lotus::View.load!

path     = Lotus::View.configuration.root.join('articles/index.html.erb')
template = Lotus::View::Template.new(path)
articles = ArticleRepository.all

Articles::Index.new(template, articles: articles).render
```

While this code is working fine, it's inefficient and verbose, because we are loading a template from the filesystem for each rendering attempt.
Also, this is strictly related to the HTML format, what if we want to manage other formats?

```ruby
require 'lotus/view'

module Articles
  class Index
    include Lotus::View
  end

  class AtomIndex < Index
    format :atom
  end
end

Lotus::View.configure do
  root 'app/templates'
end

Lotus::View.load!

articles = ArticleRepository.all

Articles::Index.render(format: :html, articles: articles)
  # => This will use Articles::Index
  #    and "articles/index.html.erb"

Articles::Index.render(format: :atom, articles: articles)
  # => This will use Articles::AtomIndex
  #    and "articles/index.atom.erb"

Articles::Index.render(format: :xml, articles: articles)
  # => This will raise a Lotus::View::MissingTemplateError
```

### Locals

All the objects passed in the context are called _locals_, they are available both in the view and in the template:

```ruby
require 'lotus/view'

module Articles
  class Show
    include Lotus::View

    def authors
      article.map(&:author).join ', '
    end
  end
end
```
<commit_msg>fix typo in README.md<commit_after># Lotus::View

A View layer for [Lotus](http://lotusrb.org).

It's based on a **separation between views and templates**.

A _view_ is an object that encapsulates the presentation logic of a page.
A _template_ is a file that defines the semantic and visual elements of a page.
In order to show a result to an user, a template must be _rendered_ by a view.

Keeping things separated helps to declutter templates and models from presentation logic.
Also, since views are objects, they are easily testable.
If you ever used [Mustache](http://mustache.github.io/), you are already aware of the advantages.

Like all the other Lotus compontents, it can be used as a standalone framework or within a full Lotus application.

## Status

[![Gem Version](http://img.shields.io/gem/v/lotus-view.svg)](https://badge.fury.io/rb/lotus-view)
[![Build Status](http://img.shields.io/travis/lotus/view/master.svg)](https://travis-ci.org/lotus/view?branch=master)
[![Coverage](http://img.shields.io/coveralls/lotus/view/master.svg)](https://coveralls.io/r/lotus/view)
[![Code Climate](http://img.shields.io/codeclimate/github/lotus/view.svg)](https://codeclimate.com/github/lotus/view)
[![Dependencies](http://img.shields.io/gemnasium/lotus/view.svg)](https://gemnasium.com/lotus/view)
[![Inline docs](http://inch-ci.org/github/lotus/view.svg?branch=master)](http://inch-ci.org/github/lotus/view)

## Contact

* Home page: http://lotusrb.org
* Mailing List: http://lotusrb.org/mailing-list
* API Doc: http://rdoc.info/gems/lotus-view
* Bugs/Issues: https://github.com/lotus/view/issues
* Support: http://stackoverflow.com/questions/tagged/lotus-ruby
* Chat: https://gitter.im/lotus/chat

## Rubies

__Lotus::View__ supports Ruby (MRI) 2+

## Installation

Add this line to your application's Gemfile:

    gem 'lotus-view'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install lotus-view

## Usage

### Conventions

  * Templates are searched under `Lotus::View.configuration.root`, set this value according to your app structure (eg. `"app/templates"`).
  * A view will look for a template with a file name that is composed by its full class name (eg. `"articles/index"`).
  * A template must have two concatenated extensions: one for the format one for the engine (eg. `".html.erb"`).
  * The framework must be loaded before rendering the first time: `Lotus::View.load!`.

### Views

A simple view looks like this:

```ruby
require 'lotus/view'

module Articles
  class Index
    include Lotus::View
  end
end
```

Suppose that we want to render a list of `articles`:

```ruby
require 'lotus/view'

module Articles
  class Index
    include Lotus::View
  end
end

Lotus::View.configure do
  root 'app/templates'
end

Lotus::View.load!

path     = Lotus::View.configuration.root.join('articles/index.html.erb')
template = Lotus::View::Template.new(path)
articles = ArticleRepository.all

Articles::Index.new(template, articles: articles).render
```

While this code is working fine, it's inefficient and verbose, because we are loading a template from the filesystem for each rendering attempt.
Also, this is strictly related to the HTML format, what if we want to manage other formats?

```ruby
require 'lotus/view'

module Articles
  class Index
    include Lotus::View
  end

  class AtomIndex < Index
    format :atom
  end
end

Lotus::View.configure do
  root 'app/templates'
end

Lotus::View.load!

articles = ArticleRepository.all

Articles::Index.render(format: :html, articles: articles)
  # => This will use Articles::Index
  #    and "articles/index.html.erb"

Articles::Index.render(format: :atom, articles: articles)
  # => This will use Articles::AtomIndex
  #    and "articles/index.atom.erb"

Articles::Index.render(format: :xml, articles: articles)
  # => This will raise a Lotus::View::MissingTemplateError
```

### Locals

All the objects passed in the context are called _locals_, they are available both in the view and in the template:

```ruby
require 'lotus/view'

module Articles
  class Show
    include Lotus::View

    def authors
      articles.map(&:author).join ', '
    end
  end
end
```

{'fraction_non_alphanumeric': 0.12321192451015747, 'fraction_numerical': 0.00024041351123933166, 'mean_word_length': 4.235997482693518, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1929050', 'n_tokens_mistral': 2742, 'n_tokens_neox': 2613, 'n_words': 843}