Skip to content

Commit

Permalink
Added am optional 'local-only' attribute to a r-reqired tag
Browse files Browse the repository at this point in the history
  • Loading branch information
RussBaz committed Apr 4, 2024
1 parent 464ff42 commit 90e3f81
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Optionally, you can save the result of the condition to a different variable usi
- **`<r-extend name="template-name" />`** (must be before any tag other than r-require) to wrap the current template into a default slot of the specified template
- **`<r-require label="optional-label" name="var-name" type="var-type" default="optional-default" />`** (must be before any tag other than r-extend) to define a variable that must be passed into the template from the caller
- **`<r-require label="optional-label" name="var-name" type="var-type" default="optional-default" mutable />`** or if you need to remap it to a mutable variable of the same name
- or you can add `local-only` attribute to prevent it from being required in outer scopes. You have to provide your own argument for such a parameter in such a case.
- **`<r-include name="template-name" />`** to include another template or
- **`<r-include name="template-name"> default slot </r-include>`** to include a template with a default slot provided
- **`<r-block> some data </r-block>`** to group some part of template, e.g. wrap some text with it and now you can apply control attributes to it.
Expand Down
4 changes: 3 additions & 1 deletion Resources/Pages/Components/hello-me.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Hello, <r-slot>Friend!</r-slot>
<r-require name="name" type="String" default='"no-name"' local-only />

Hello, <r-slot>Friend!</r-slot> [extra name: \(name)]
2 changes: 1 addition & 1 deletion Resources/Pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1>
<li r-for-every="context.heroes" r-with-item="hero" r-with-index>
<p class="base">
<r-set name="class" value=" rose" append />
<r-include name="components.hello-me">\(hero)</r-include>
<r-include name="components.hello-me" :name='"very sad"'>\(hero)</r-include>
</p>
<p>Index: \(index)</p>
</li>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/AST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public indirect enum AST {
case conditional(name: String?, check: String, type: ConditionType, contents: ASTStorage)
case loop(forEvery: String, name: String?, itemName: String, indexName: String, contents: ASTStorage)
case modifiers(applying: [AttributeModifier], tag: TagType)
case requirement(name: String, type: String, label: String?, value: String?, mutable: Bool)
case requirement(name: String, type: String, label: String?, value: String?, mutable: Bool, localOnly: Bool)
case eval(line: String)
case value(of: String, defaultValue: String?)
case assignment(name: String, line: String)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Core/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,9 @@ extension Parser {
let label = attributes.find("label")
let defaultValue = attributes.find("default")
let mutable = attributes.find("mutable") != nil
let localOnly = attributes.has("local-only")

ast.append(node: .requirement(name: name, type: type, label: label, value: defaultValue, mutable: mutable))
ast.append(node: .requirement(name: name, type: type, label: label, value: defaultValue, mutable: mutable, localOnly: localOnly))
}

func openSetTag(_ tag: AST.TagType, at depth: Int) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/SwiftCodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,8 @@ public final class SwiftCodeGenerator {
case .closingTag:
properties.append("// Error: Impossible tag type", at: indentation)
}
case let .requirement(name, type, label, value, mutable):
signatures.append(parameter: .init(type: type, name: name, label: label, defaultValue: value, canBeOverriden: false), to: properties.name)
case let .requirement(name, type, label, value, mutable, localOnly):
signatures.append(parameter: .init(type: type, name: name, label: label, defaultValue: value, canBeOverriden: false, localOnly: localOnly), to: properties.name)

if mutable {
properties.appendMutable(name: name)
Expand Down
5 changes: 4 additions & 1 deletion Sources/Core/SwiftPageSignatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public final class SwiftPageSignatures {
let label: String?
let defaultValue: String?
let canBeOverriden: Bool
let localOnly: Bool
}

public struct PageSignature {
Expand Down Expand Up @@ -157,7 +158,7 @@ extension SwiftPageSignatures {
guard let cached = resolved[i] else { continue }

for c in cached {
if !buffer.contains(where: { $0.name == c.name }) {
if !c.localOnly, !buffer.contains(where: { $0.name == c.name }) {
buffer.append(c)
}
}
Expand Down Expand Up @@ -197,6 +198,8 @@ extension SwiftPageSignatures.ParameterDef: LosslessStringConvertible {
public init?(_ description: String) {
let checkedDescription: String

localOnly = false

if description.starts(with: "?") {
canBeOverriden = true
checkedDescription = String(description.dropFirst())
Expand Down
13 changes: 9 additions & 4 deletions Sources/Example/pages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,25 @@ enum Pages {
// Own pages
enum HelloMe {
// Template: ./Components/hello-me.html
static func render(req: Request) -> String {
include(req: req).render()
static func render(req: Request, name: String = "no-name") -> String {
include(req: req, name: name).render()
}

static func include(req _: Request) -> SwiftLineStorage {
static func include(req _: Request, name: String = "no-name") -> SwiftLineStorage {
let lines = SwiftLineStorage()
lines.append("""
Hello,
""")
lines.declare(slot: "default") { lines in
lines.append("""
Friend!
""")
}
lines.append("""
[extra name: \(name)]
""")

return lines
}
Expand Down Expand Up @@ -240,7 +245,7 @@ enum Pages {
attributes = SwiftAttributeStorage.from(attributes: ["class": .string("base", wrapper: .double)])
attributes.append(to: "class", value: .string(" rose", wrapper: .double))
lines.append("<p\(attributes)>")
lines.include(Pages.Components.HelloMe.include(req: req)) { lines in
lines.include(Pages.Components.HelloMe.include(req: req, name: "very sad")) { lines in
lines.append("""
\(hero)
""")
Expand Down

0 comments on commit 90e3f81

Please sign in to comment.