Skip to content

Commit

Permalink
Merge pull request #3 from mattdb/import_template
Browse files Browse the repository at this point in the history
Import template
  • Loading branch information
palkan committed Aug 16, 2023
2 parents d1cdeea + f67f131 commit f3e5328
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Add `import_template` command.

## 0.1.4 (2023-04-11)

- Fix publishing to RailsBytes.
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ file "config/anycable.yml", ERB.new(

**NOTE:** By default, we assume that partials are stored next to the template's entry-point. Partials may have the "_" prefix and ".rb" or ".tt" suffixes.

Ruby Bytes can also import a sub-template into your template. Simply use the `#import_template` helper in your template:

```erb
# subtemplate/_partial.rb
say "Hello from subtemplate's partial!"
# subtemplate/subtemplate.rb
<%= include "partial" %>
# template.rb
<%= import_template("subtemplate/subtemplate.rb")>
```

Note that the full filename must be passed to the `#import_template` helper, including any file extension, unlike the `#include` helper. All templates within the subtemplate directory will consider that to be their root directory.


### Compiling templates

You can compile a template by using the `rbytes` executable:
Expand Down
4 changes: 4 additions & 0 deletions lib/ruby_bytes/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def include(path, indent: 0)
indented(render(File.read(resolve_path(path))), indent)
end

def import_template(path, indent: 0)
indented(self.class.new(File.join(root, path)).render, indent)
end

private

PATH_CANDIDATES = [
Expand Down
16 changes: 16 additions & 0 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ def test_include_partials
end
end

class IncludeTemplateTest < RubyBytes::TestCase
root File.join(__dir__, "templates/imports_template")

template <<~RUBY
<%= include "importer" %>
RUBY

def test_included_template
run_generator do |output|
assert_line_printed output, "Hello from the Root"
assert_line_printed output, "Hello from A template"
assert_line_printed output, "Hello from A partial"
end
end
end

class PromptTest < RubyBytes::TestCase
template <<~RUBY
if yes?("Mochtest du etwas trinken?")
Expand Down
1 change: 1 addition & 0 deletions test/templates/imports_template/example_a/_a.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say "Hello from A partial!"
3 changes: 3 additions & 0 deletions test/templates/imports_template/example_a/example_a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
say "Hello from A template"

<%= include "a.rb" %>
3 changes: 3 additions & 0 deletions test/templates/imports_template/importer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
say "Hello from the Root"

<%= import_template "example_a/example_a.rb" %>

0 comments on commit f3e5328

Please sign in to comment.