hanami-view 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fa2f0c09d6500c146316de763a71a98afefdf03
4
- data.tar.gz: 7d96ea6c912cdd30fc2945c786c6a5d057cffc98
3
+ metadata.gz: 2814a76015d67720fc54b6aab3acb54cce7f65a4
4
+ data.tar.gz: db8a5cb5b152d93e837d7a3bddd7e755d85db277
5
5
  SHA512:
6
- metadata.gz: f818e7eb0e57bf05852b7f8a1b6fe1971ca87d9d20404815d179aa7202eb07c3627bc996e29136babff7d1b2072f531748b6401361e7d7e5a179ed2b7f25979a
7
- data.tar.gz: 675e51826977c6e4bf1872aa4350e95d68deb225833a8a821b66ac686062f5b3418f9a19918572c850f5d9746bbc10a9e401382da4c27df9fab5d587bb1a9592
6
+ metadata.gz: 2132c9ae9e477ee2f99bdd71e7596946bb4fa69d1d6dde525b325df11e2859bfe0186f5b972a0942c21acb5f4e1d7e64b2c7d84ca761600f2af29e78db7490d1
7
+ data.tar.gz: b9de16d5e18f760165b367b042b69a0a21a079d61d9b894512295f9fd5192cd65a5df9c4d243242a59352267fa818f04005d52f76c5cf918200c943740fde20c
data/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Hanami::View
2
2
  View layer for Hanami
3
3
 
4
+ ## v0.6.1 - 2016-02-05
5
+ ### Changed
6
+ - [Steve Hook] Preload partial templates in order to boost performances for partials rendering (2x faster)
7
+
8
+ ### Fixed
9
+ - [Luca Guidi] Disable Slim autoescape to use `Hanami::View`'s feature
10
+
4
11
  ## v0.6.0 - 2016-01-22
5
12
  ### Changed
6
13
  - [Luca Guidi] Renamed the project
data/hanami-view.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
  spec.required_ruby_version = '>= 2.0.0'
21
21
 
22
- spec.add_runtime_dependency 'tilt', '~> 2.0', '>= 2.0.1'
22
+ spec.add_runtime_dependency 'tilt', '~> 2.0', '>= 2.0.1'
23
23
  spec.add_runtime_dependency 'hanami-utils', '~> 0.7'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.5'
@@ -4,6 +4,7 @@ require 'hanami/utils/kernel'
4
4
  require 'hanami/utils/string'
5
5
  require 'hanami/utils/load_paths'
6
6
  require 'hanami/view/rendering/layout_finder'
7
+ require 'hanami/view/rendering/partial_templates_finder'
7
8
 
8
9
  module Hanami
9
10
  module View
@@ -39,6 +40,7 @@ module Hanami
39
40
  attr_reader :views
40
41
  attr_reader :layouts
41
42
  attr_reader :modules
43
+ attr_reader :partials
42
44
 
43
45
  # Return the original configuration of the framework instance associated
44
46
  # with the given class.
@@ -418,9 +420,39 @@ module Hanami
418
420
  def load!
419
421
  views.each { |v| v.__send__(:load!) }
420
422
  layouts.each { |l| l.__send__(:load!) }
423
+ load_partials!
421
424
  freeze
422
425
  end
423
426
 
427
+ # Load partials for each partial template file found under the
428
+ # given load paths
429
+ #
430
+ # @since x.x.x
431
+ # @api private
432
+ def load_partials!
433
+ Hanami::View::Rendering::PartialTemplatesFinder.new(self).find.each do |partial|
434
+ add_partial(partial)
435
+ end
436
+ end
437
+
438
+ # Load partials for each partial template file found under the
439
+ # given load paths
440
+ #
441
+ # @since x.x.x
442
+ # @api private
443
+ def find_partial(relative_partial_path, template_name, format)
444
+ partials_for_view = partials.has_key?(relative_partial_path) ? partials[relative_partial_path] : partials[template_name]
445
+ partials_for_view ? partials_for_view[format.to_sym] : nil
446
+ end
447
+
448
+ # Add a partial to the registry
449
+ #
450
+ # @since x.x.x
451
+ # @api private
452
+ def add_partial(partial)
453
+ @partials[partial.key][partial.format.to_sym] = partial.template
454
+ end
455
+
424
456
  # Reset all the values to the defaults
425
457
  #
426
458
  # @since 0.2.0
@@ -429,6 +461,7 @@ module Hanami
429
461
  root DEFAULT_ROOT
430
462
  default_encoding DEFAULT_ENCODING
431
463
 
464
+ @partials = Hash.new { |h, k| h[k] = Hash.new }
432
465
  @views = Set.new
433
466
  @layouts = Set.new
434
467
  @load_paths = Utils::LoadPaths.new(root)
@@ -0,0 +1,19 @@
1
+ module Hanami
2
+ module View
3
+ module Rendering
4
+ # @since x.x.x
5
+ # @api private
6
+ class PartialFile
7
+ attr_reader :key, :format, :template
8
+
9
+ # @since x.x.x
10
+ # @api private
11
+ def initialize(key, format, template)
12
+ @key = key
13
+ @format = format
14
+ @template = template
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -23,9 +23,10 @@ module Hanami
23
23
  PREFIX = '_'.freeze
24
24
 
25
25
  # Find a template for a partial. Initially it will look for the
26
- # partial template under the directory of the parent directory
27
- # view template, if not found it will search recursivly from
28
- # the view root.
26
+ # partial template in the framework configuration where it may
27
+ # already be cached. Failing that it will look under the
28
+ # directory of the parent directory view template, if not found
29
+ # it will search recursively from the view root.
29
30
  #
30
31
  # @return [Hanami::View::Template] the requested template
31
32
  #
@@ -34,14 +35,18 @@ module Hanami
34
35
  # @since 0.4.3
35
36
  # @api private
36
37
  def find
37
- if path = partial_template_under_view_path
38
- View::Template.new(path, @view.configuration.default_encoding)
39
- else
40
- super
41
- end
38
+ Hanami::View::Configuration.for(@view).
39
+ find_partial(relative_partial_path, template_name, format)
42
40
  end
43
41
 
44
42
  protected
43
+
44
+ # @since x.x.x
45
+ # @api private
46
+ def relative_partial_path
47
+ [view_template_dir, template_name].join(separator)
48
+ end
49
+
45
50
  # @since 0.4.3
46
51
  # @api private
47
52
  def partial_template_under_view_path
@@ -0,0 +1,70 @@
1
+ require 'hanami/view/template'
2
+ require 'hanami/view/rendering/partial_file'
3
+
4
+ module Hanami
5
+ module View
6
+ module Rendering
7
+ # Find partial templates in the file system
8
+ #
9
+ # @api private
10
+ # @since x.x.x
11
+ #
12
+ # @see View::Template
13
+ class PartialTemplatesFinder
14
+ # Search pattern for partial file names
15
+ #
16
+ # @api private
17
+ # @since x.x.x
18
+ PARTIAL_PATTERN = '_*'.freeze
19
+
20
+ # @api private
21
+ # @since x.x.x
22
+ PARTIAL_PARTS_SEPARATOR = '.'.freeze
23
+
24
+ attr_reader :configuration
25
+
26
+ # Initializes a new PartialTemplatesFinder
27
+ #
28
+ # @param configuration [Configuration] the configuration object
29
+ #
30
+ # @since x.x.x
31
+ def initialize(configuration)
32
+ @configuration = configuration
33
+ end
34
+
35
+ # Find partials under the given path
36
+ #
37
+ # @return [Array] array of PartialFinder objects
38
+ #
39
+ # @since x.x.x
40
+ def find
41
+ _find_partials(configuration.root).map do |template|
42
+ partial_path, partial_base_name = Pathname(template).relative_path_from(configuration.root).split
43
+ partial_base_parts = partial_base_name.to_s.split(PARTIAL_PARTS_SEPARATOR)
44
+
45
+ PartialFile.new(
46
+ "#{partial_path}#{::File::SEPARATOR}#{partial_base_parts[0]}",
47
+ partial_base_parts[1],
48
+ View::Template.new(template, configuration.default_encoding)
49
+ )
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # Find partial template file paths
56
+ #
57
+ # @param path [String] the path under which we should search for partials
58
+ #
59
+ # @return [Array] an array of strings for each matching partial template file found
60
+ #
61
+ # @since x.x.x
62
+ # @api private
63
+ def _find_partials(path)
64
+ Dir.glob("#{ [path, TemplatesFinder::RECURSIVE, PARTIAL_PATTERN].join(::File::SEPARATOR) }.#{TemplatesFinder::FORMAT}.#{TemplatesFinder::ENGINES}")
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
@@ -7,7 +7,9 @@ module Hanami
7
7
  # @since 0.1.0
8
8
  class Template
9
9
  def initialize(template, encoding = Encoding::UTF_8)
10
- @_template = Tilt.new(template, nil, default_encoding: encoding)
10
+ # NOTE disable_escape: true is for Slim compatibility
11
+ # See https://github.com/hanami/assets/issues/36
12
+ @_template = Tilt.new(template, nil, default_encoding: encoding, disable_escape: true)
11
13
  end
12
14
 
13
15
  # Returns the format that the template handles.
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.6.0'.freeze
6
+ VERSION = '0.6.1'.freeze
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-01-22 00:00:00.000000000 Z
13
+ date: 2016-02-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: tilt
@@ -117,7 +117,9 @@ files:
117
117
  - lib/hanami/view/rendering/null_layout.rb
118
118
  - lib/hanami/view/rendering/null_template.rb
119
119
  - lib/hanami/view/rendering/partial.rb
120
+ - lib/hanami/view/rendering/partial_file.rb
120
121
  - lib/hanami/view/rendering/partial_finder.rb
122
+ - lib/hanami/view/rendering/partial_templates_finder.rb
121
123
  - lib/hanami/view/rendering/registry.rb
122
124
  - lib/hanami/view/rendering/scope.rb
123
125
  - lib/hanami/view/rendering/template.rb