NutView uses special swift-like syntax in .nut.html files which provide great readability for swift developers.
Directory structure
NutView use two important directories.
First (default name: “Nuts“) contains another three subdirectories with .nut.html files (Views, Layouts, Subviews). In theese three directories you can add another directories or add and edit .nut.html files.
Second (defualt name: “Fruits“) contains generated files from your .nut.html files. (Don’t change content of this directory)
Contains page specific content. For example if we have blog and we want to have page with certain post our view will contain only post information and not layout, header, footer etc…
Nuts/Subview
Contains reusable parts of page. For example header or footer.
Nuts/Layout
This is actualy our web layout. You can refer to layout from Views which pin View content to Layout at the place where is \View()
Commands
Commands starts with *\* symbol. You can escape \ symbol with \\
Name
Syntax
Semantic
Expression
\(<expression>)
Evaluates expression in parentheses and escapes html characters
If expression is true run commands in given block otherwise run else if else block if exists
If let Else if let
\if let <variableName> = <expression> { \} else if let <variableName> = <expression> {
If expression is not nil store result in variableName and run commands in given block otherwise run else if else block if exists
Subview
\Subview(<expression>)
Add content of given subview at position of this command. Note:name is using dot notation so instead of MySubviewSubdirectory/Mysubview.nut.html write MySubviewSubdirectory.Mysubview.
For
\for <variable> in <Array> \for (<key>, <value>) in <Dictionary>
Iterates over array([Any]) or dictionary([String: Any])
Evaluates expression and print date in given format. If format is not set, NutView use default date format specified in NutConfig.dateDefaultFormat: String { set get }
Layout
\Layout(<expression>)
Reffer View to Layout. name is using dot notation so instead of MyLayoutSubdirectory/MyLayout.nut.html write MyLayoutSubdirectory.MyLayout.
Title
\Title(<expression>)
Set <title><\title> header of html document
Head
\Head(<expression>)
Add expression result inside html head tag
Body
\Body(<expression>)
Add expression result at the end of html body tag
View
\View()
Indicates where to place View
Note: For evaluating expressions we use Evaluation
Implicit variables
Some variables are implicit so you don’t have to send them as data.
view - This variable contains View name
Examples
Sources/main.swift
import NutView
let indexView = View(name: "Index") // We don't have to write .nut.html
let indexContent = try indexView.getContent()
print(indexContent) // prints generated content of index
struct PostData {
let title: String,
let body: String,
let isNew: Bool
}
let postData = PostData(title: "Squirrels", body: "Squirrels are best!", isNew: false)
let postView = try View(name: "Posts.Post", with: postData) // note we write . instead of /
let postContent = try postView.getContent()
print(postContent) // prints generated content of page with given data
let postContentData = try postView.present() // Data representation of view
NutView
Amazing template language for web development used n Swift Squirrel web framework (see: Swift Squirrel)
Installing
Add NutView as dependency in your Package.swift
And in source add import line
Usage
NutView uses special swift-like syntax in .nut.html files which provide great readability for swift developers.
Directory structure
NutView use two important directories.
You can change this directories with
Nuts/Views
Contains page specific content. For example if we have blog and we want to have page with certain post our view will contain only post information and not layout, header, footer etc…
Nuts/Subview
Contains reusable parts of page. For example header or footer.
Nuts/Layout
This is actualy our web layout. You can refer to layout from Views which pin View content to Layout at the place where is
\View()Commands
Commands starts with *\* symbol. You can escape \ symbol with \\
\(<expression>)\RawValue(<expression>)\}Else if
Else
\if <expression> {\} else if <expression> {\} else {truerun commands in given block otherwise run else if else block if existsElse if let
\if let <variableName> = <expression> {\} else if let <variableName> = <expression> {nilstore result invariableNameand run commands in given block otherwise run else if else block if exists\Subview(<expression>)nameis using dot notation so instead ofMySubviewSubdirectory/Mysubview.nut.htmlwriteMySubviewSubdirectory.Mysubview.\for <variable> in <Array>\for (<key>, <value>) in <Dictionary>[Any]) or dictionary([String: Any])\Date(<expression>)\Date(<expression>, fromat: <expression>)formatis not set, NutView use default date format specified inNutConfig.dateDefaultFormat: String { set get }\Layout(<expression>)nameis using dot notation so instead ofMyLayoutSubdirectory/MyLayout.nut.htmlwriteMyLayoutSubdirectory.MyLayout.\Title(<expression>)<title><\title>header of html document\Head(<expression>)\Body(<expression>)\View()Implicit variables
Some variables are implicit so you don’t have to send them as data.
view- This variable contains View nameExamples
Sources/main.swift
For more examples check Examples or Squirrel docs -> Views
Contributing
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-featureAuthors
See also CONTRIBUTORS to list of contributors who participated in this project.
License
This project is licensed under the Apache License Version 2.0 - see the LICENSE file for details